In this post, a program to search, insert, and delete operations in an unsorted array is discussed.
Search Operation:
In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element.
Coding implementation of the search operation:
C++
// C++ program to implement linear
// search in unsorted array
#include <bits/stdc++.h>
usingnamespacestd;
// Function to implement search operation
intfindElement(intarr[], intn, intkey)
{
inti;
for(i = 0; i < n; i++)
if(arr[i] == key)
returni;
// If the key is not found
return-1;
}
// Driver's Code
intmain()
{
intarr[] = { 12, 34, 10, 6, 40 };
intn = sizeof(arr) / sizeof(arr[0]);
// Using a last element as search element
intkey = 40;
// Function call
intposition = findElement(arr, n, key);
if(position == -1)
cout << "Element not found";
else
cout << "Element Found at Position: "
<< position + 1;
return0;
}
// This code is contributed
// by Akanksha Rai
C
// C program to implement linear
// search in unsorted array
#include <stdio.h>
// Function to implement search operation
intfindElement(intarr[], intn, intkey)
{
inti;
for(i = 0; i < n; i++)
if(arr[i] == key)
returni;
// If the key is not found
return-1;
}
// Driver's Code
intmain()
{
intarr[] = { 12, 34, 10, 6, 40 };
intn = sizeof(arr) / sizeof(arr[0]);
// Using a last element as search element
intkey = 40;
// Function call
intposition = findElement(arr, n, key);
if(position == -1)
printf("Element not found");
else
printf("Element Found at Position: %d",
position + 1);
return0;
}
Java
// Java program to implement linear
// search in unsorted arrays
classMain {
// Function to implement
// search operation
staticintfindElement(intarr[], intn, intkey)
{
for(inti = 0; i < n; i++)
if(arr[i] == key)
returni;
// If the key is not found
return-1;
}
// Driver's Code
publicstaticvoidmain(String args[])
{
intarr[] = { 12, 34, 10, 6, 40};
intn = arr.length;
// Using a last element as search element
intkey = 40;
// Function call
intposition = findElement(arr, n, key);
if(position == -1)
System.out.println("Element not found");
else
System.out.println("Element Found at Position: "
+ (position + 1));
}
}
Python3
# Python program for searching in
# unsorted array
deffindElement(arr, n, key):
fori inrange(n):
if(arr[i] ==key):
returni
# If the key is not found
return-1
# Driver's code
if__name__ =='__main__':
arr =[12, 34, 10, 6, 40]
key =40
n =len(arr)
# search operation
index =findElement(arr, n, key)
ifindex !=-1:
print("Element Found at position: "+str(index +1))
else:
print("Element not found")
# Thanks to Aditi Sharma for contributing
# this code
C#
// C# program to implement linear
// search in unsorted arrays
usingSystem;
classmain {
// Function to implement
// search operation
staticintfindElement(int[] arr, intn, intkey)
{
for(inti = 0; i < n; i++)
if(arr[i] == key)
returni;
// If the key is not found
return-1;
}
// Driver Code
publicstaticvoidMain()
{
int[] arr = { 12, 34, 10, 6, 40 };
intn = arr.Length;
// Using a last element as
// search element
intkey = 40;
intposition = findElement(arr, n, key);
if(position == -1)
Console.WriteLine("Element not found");
else
Console.WriteLine("Element Found at Position: "
+ (position + 1));
}
}
// This code is contributed by vt_m.
Javascript
// Javascript program to implement linear
// search in unsorted array
// Function to implement search operation
functionfindElement( arr, n, key)
{
let i;
for(i = 0; i < n; i++)
if(arr[i] == key)
returni;
return-1;
}
// Driver program
let arr = [12, 34, 10, 6, 40];
let n = arr.length;
// Using a last element as search element
let key = 40;
let position = findElement(arr, n, key);
if(position == - 1)
document.write("Element not found");
else
document.write("Element Found at Position: "
+ (position + 1));
PHP
<?php
// PHP program to implement linear
// search in unsorted array
// Function to implement
// search operation
functionfindElement($arr, $n, $key)
{
$i;
for($i= 0; $i< $n; $i++)
if($arr[$i] == $key)
return$i;
// If the key is not found
return-1;
}
// Driver Code
$arr= array(12, 34, 10, 6, 40);
$n= sizeof($arr);
// Using a last element
// as search element
$key= 40;
$position= findElement($arr, $n, $key);
if($position== - 1)
echo("Element not found");
else
echo("Element Found at Position: ". ($position+ 1));
// This code is contributed by Ajit.
?>
Output
Element Found at Position: 5
Time Complexity: O(N) Auxiliary Space: O(1)
Insert Operation:
1. Insert at the end:
In an unsorted array, the insert operation is faster as compared to a sorted array because we don’t have to care about the position at which the element is to be placed.
Coding implementation of inserting an element at the end:
// Function to insert element at a specific position
functioninsertElement(&$arr, $n, $x, $pos) {
// shift elements to the right which are on the right side of pos
for($i= $n- 1; $i>= $pos; $i--) {
$arr[$i+ 1] = $arr[$i];
}
// insert the new element at the specified position
$arr[$pos] = $x;
}
// Driver's code
$arr= array(2, 4, 1, 8, 5);
$n= 5;
echo"Before insertion : ";
for($i= 0; $i< $n; $i++) {
echo$arr[$i] . " ";
}
echo"\n";
$x= 10;
$pos= 2;
// Function call
insertElement($arr, $n, $x, $pos);
$n++;
echo"After insertion : ";
for($i= 0; $i< $n; $i++) {
echo$arr[$i] . " ";
}
?>
Output
Before insertion : 2 4 1 8 5
After insertion : 2 4 10 1 8 5
Time complexity: O(N) Auxiliary Space: O(1)
Delete Operation:
In the delete operation, the element to be deleted is searched using the linear search, and then the delete operation is performed followed by shifting the elements.
C++
// C++ program to implement delete operation in a
// unsorted array
#include <iostream>
usingnamespacestd;
// To search a key to be deleted
intfindElement(intarr[], intn, intkey);
// Function to delete an element
intdeleteElement(intarr[], intn, intkey)
{
// Find position of element to be deleted
intpos = findElement(arr, n, key);
if(pos == -1) {
cout << "Element not found";
returnn;
}
// Deleting element
inti;
for(i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
returnn - 1;
}
// Function to implement search operation
intfindElement(intarr[], intn, intkey)
{
inti;
for(i = 0; i < n; i++)
if(arr[i] == key)
returni;
return-1;
}
// Driver's code
intmain()
{
inti;
intarr[] = { 10, 50, 30, 40, 20 };
intn = sizeof(arr) / sizeof(arr[0]);
intkey = 30;
cout << "Array before deletion\n";
for(i = 0; i < n; i++)
cout << arr[i] << " ";
// Function call
n = deleteElement(arr, n, key);
cout << "\n\nArray after deletion\n";
for(i = 0; i < n; i++)
cout << arr[i] << " ";
return0;
}
// This code is contributed by shubhamsingh10
C
// C program to implement delete operation in a
// unsorted array
#include <stdio.h>
// To search a key to be deleted
intfindElement(intarr[], intn, intkey);
// Function to delete an element
intdeleteElement(intarr[], intn, intkey)
{
// Find position of element to be deleted
intpos = findElement(arr, n, key);
if(pos == -1) {
printf("Element not found");
returnn;
}
// Deleting element
inti;
for(i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
returnn - 1;
}
// Function to implement search operation
intfindElement(intarr[], intn, intkey)
{
inti;
for(i = 0; i < n; i++)
if(arr[i] == key)
returni;
return-1;
}
// Driver's code
intmain()
{
inti;
intarr[] = { 10, 50, 30, 40, 20 };
intn = sizeof(arr) / sizeof(arr[0]);
intkey = 30;
printf("Array before deletion\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
// Function call
n = deleteElement(arr, n, key);
printf("\nArray after deletion\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return0;
}
Java
// Java program to implement delete
// operation in an unsorted array
classMain {
// function to search a key to
// be deleted
staticintfindElement(intarr[], intn, intkey)
{
inti;
for(i = 0; i < n; i++)
if(arr[i] == key)
returni;
return-1;
}
// Function to delete an element
staticintdeleteElement(intarr[], intn, intkey)
{
// Find position of element to be
// deleted
intpos = findElement(arr, n, key);
if(pos == -1) {
System.out.println("Element not found");
returnn;
}
// Deleting element
inti;
for(i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
returnn - 1;
}
// Driver's Code
publicstaticvoidmain(String args[])
{
inti;
intarr[] = { 10, 50, 30, 40, 20};
intn = arr.length;
intkey = 30;
System.out.println("Array before deletion");
for(i = 0; i < n; i++)
System.out.print(arr[i] + " ");
// Function call
n = deleteElement(arr, n, key);
System.out.println("\n\nArray after deletion");
for(i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
Python3
# Python program to delete an element
# from an unsorted array
# Driver's code
if__name__ =='__main__':
# Declaring array and key to delete
arr =[10, 50, 30, 40, 20]
key =30
print("Array before deletion:")
print(arr)
# deletes key if found in the array
# otherwise shows error not in list
arr.remove(key)
print("Array after deletion")
print(arr)
# This code is contributed by Aditi Sharma.
C#
// C# program to implement delete
// operation in an unsorted array
usingSystem;
classmain {
// Function to search a
// key to be deleted
staticintfindElement(int[] arr, intn, intkey)
{
inti;
for(i = 0; i < n; i++)
if(arr[i] == key)
returni;
return-1;
}
// Function to delete an element
staticintdeleteElement(int[] arr, intn, intkey)
{
// Find position of element
// to be deleted
intpos = findElement(arr, n, key);
if(pos == -1) {
Console.WriteLine("Element not found");
returnn;
}
// Deleting element
inti;
for(i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
returnn - 1;
}
// Driver Code
publicstaticvoidMain()
{
inti;
int[] arr = { 10, 50, 30, 40, 20 };
intn = arr.Length;
intkey = 30;
Console.Write("Array before deletion ");
for(i = 0; i < n; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
// Function call
n = deleteElement(arr, n, key);
Console.Write("Array after deletion ");
for(i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by vt_m.
Javascript
// Java script program to implement delete
// operation in an unsorted array
// function to search a key to
// be deleted
functionfindElement(arr,n,key)
{
let i;
for(i = 0; i < n; i++)
if(arr[i] == key)
returni;
return-1;
}
// Function to delete an element
functiondeleteElement(arr,n,key)
{
// Find position of element to be
// deleted
let pos = findElement(arr, n, key);
if(pos == -1)
{
document.write("Element not found");
returnn;
}
// Deleting element
let i;
for(i = pos; i< n - 1; i++)
arr[i] = arr[i + 1];
returnn - 1;
}
// Driver Code
let i;
let arr = [10, 50, 30, 40, 20];
let n = arr.length;
let key = 30;
document.write("Array before deletion<br>");
for(i=0; i<n; i++)
document.write(arr[i] + " ");
n = deleteElement(arr, n, key);
document.write("<br><br>Array after deletion<br>");
for(i=0; i<n; i++)
document.write(arr[i]+" ");
// This code is contributed by sravan kumar Gottumukkala
PHP
<?php
// PHP program to implement delete
// operation in an unsorted array
// To search a key to be deleted
functionfindElement(&$arr, $n, $key)
{
for($i= 0; $i< $n; $i++)
if($arr[$i] == $key)
return$i;
return-1;
}
// Function to delete an element
functiondeleteElement(&$arr, $n, $key)
{
// Find position of element to
// be deleted
$pos= findElement($arr, $n, $key);
if($pos== -1)
{
echo"Element not found";
return$n;
}
// Deleting element
for($i= $pos; $i< $n- 1; $i++)
$arr[$i] = $arr[$i+ 1];
return$n- 1;
}
// Driver code
$arr= array(10, 50, 30, 40, 20);
$n= count($arr);
$key= 30;
echo"Array before deletion\n";
for($i= 0; $i< $n; $i++)
echo$arr[$i] . " ";
// Function call
$n= deleteElement($arr, $n, $key);
echo"\nArray after deletion\n";
for($i= 0; $i< $n; $i++)
echo$arr[$i] . " ";
// This code is contributed by
// Rajput-Ji
?>
Output
Array before deletion
10 50 30 40 20
Array after deletion
10 50 40 20
Time Complexity: O(N) Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!