Search, Insert, and Delete in an Unsorted Array | Array Operations
Last Updated :
02 Nov, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int findElement( int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main()
{
int arr[] = { 12, 34, 10, 6, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 40;
int position = findElement(arr, n, key);
if (position == -1)
cout << "Element not found" ;
else
cout << "Element Found at Position: "
<< position + 1;
return 0;
}
|
C
#include <stdio.h>
int findElement( int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main()
{
int arr[] = { 12, 34, 10, 6, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 40;
int position = findElement(arr, n, key);
if (position == -1)
printf ( "Element not found" );
else
printf ( "Element Found at Position: %d" ,
position + 1);
return 0;
}
|
Java
class Main {
static int findElement( int arr[], int n, int key)
{
for ( int i = 0 ; i < n; i++)
if (arr[i] == key)
return i;
return - 1 ;
}
public static void main(String args[])
{
int arr[] = { 12 , 34 , 10 , 6 , 40 };
int n = arr.length;
int key = 40 ;
int position = findElement(arr, n, key);
if (position == - 1 )
System.out.println( "Element not found" );
else
System.out.println( "Element Found at Position: "
+ (position + 1 ));
}
}
|
Python3
def findElement(arr, n, key):
for i in range (n):
if (arr[i] = = key):
return i
return - 1
if __name__ = = '__main__' :
arr = [ 12 , 34 , 10 , 6 , 40 ]
key = 40
n = len (arr)
index = findElement(arr, n, key)
if index ! = - 1 :
print ( "Element Found at position: " + str (index + 1 ))
else :
print ( "Element not found" )
|
C#
using System;
class main {
static int findElement( int [] arr, int n, int key)
{
for ( int i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
public static void Main()
{
int [] arr = { 12, 34, 10, 6, 40 };
int n = arr.Length;
int key = 40;
int position = findElement(arr, n, key);
if (position == -1)
Console.WriteLine( "Element not found" );
else
Console.WriteLine( "Element Found at Position: "
+ (position + 1));
}
}
|
Javascript
function findElement( arr, n, key)
{
let i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
let arr = [12, 34, 10, 6, 40];
let n = arr.length;
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
function findElement( $arr , $n , $key )
{
$i ;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] == $key )
return $i ;
return -1;
}
$arr = array (12, 34, 10, 6, 40);
$n = sizeof( $arr );
$key = 40;
$position = findElement( $arr , $n , $key );
if ( $position == - 1)
echo ( "Element not found" );
else
echo ( "Element Found at Position: " . ( $position + 1));
?>
|
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:
C++
#include <iostream>
using namespace std;
int insertSorted( int arr[], int n, int key, int capacity)
{
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
}
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof (arr) / sizeof (arr[0]);
int n = 6;
int i, key = 26;
cout << "Before Insertion: " ;
for (i = 0; i < n; i++)
cout << arr[i] << " " ;
n = insertSorted(arr, n, key, capacity);
cout << "\nAfter Insertion: " ;
for (i = 0; i < n; i++)
cout << arr[i] << " " ;
return 0;
}
|
C
#include <stdio.h>
int insertSorted( int arr[], int n, int key, int capacity)
{
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
}
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof (arr) / sizeof (arr[0]);
int n = 6;
int i, key = 26;
printf ( "\n Before Insertion: " );
for (i = 0; i < n; i++)
printf ( "%d " , arr[i]);
n = insertSorted(arr, n, key, capacity);
printf ( "\n After Insertion: " );
for (i = 0; i < n; i++)
printf ( "%d " , arr[i]);
return 0;
}
|
Java
class Main {
static int insertSorted( int arr[], int n, int key,
int capacity)
{
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1 );
}
public static void main(String[] args)
{
int [] arr = new int [ 20 ];
arr[ 0 ] = 12 ;
arr[ 1 ] = 16 ;
arr[ 2 ] = 20 ;
arr[ 3 ] = 40 ;
arr[ 4 ] = 50 ;
arr[ 5 ] = 70 ;
int capacity = 20 ;
int n = 6 ;
int i, key = 26 ;
System.out.print( "Before Insertion: " );
for (i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
n = insertSorted(arr, n, key, capacity);
System.out.print( "\n After Insertion: " );
for (i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
}
|
Python3
def insert(arr, element):
arr.append(element)
if __name__ = = '__main__' :
arr = [ 12 , 16 , 20 , 40 , 50 , 70 ]
key = 26
print ( "Before Inserting: " )
print (arr)
insert(arr, key)
print ( "After Inserting: " )
print (arr)
|
C#
using System;
class main {
static int insertSorted( int [] arr, int n, int key,
int capacity)
{
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
}
public static void Main()
{
int [] arr = new int [20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = 20;
int n = 6;
int i, key = 26;
Console.Write( "Before Insertion: " );
for (i = 0; i < n; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
n = insertSorted(arr, n, key, capacity);
Console.Write( "After Insertion: " );
for (i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
function insertSorted(arr, n, key, capacity)
{
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
}
let arr = new Array(20);
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
let capacity = 20;
let n = 6;
let i, key = 26;
document.write( "Before Insertion: " );
for (i = 0; i < n; i++)
document.write(arr[i]+ " " );
document.write( "</br>" );
n = insertSorted(arr, n, key, capacity);
document.write( "After Insertion: " );
for (i = 0; i < n; i++)
document.write(arr[i]+ " " );
|
PHP
<?php
function insertSorted(& $arr , $n , $key ,
$capacity )
{
if ( $n >= $capacity )
return $n ;
array_push ( $arr , $key );
return ( $n + 1);
}
$arr = array (12, 16, 20, 40, 50, 70);
$capacity = 20;
$n = 6;
$key = 26;
echo "Before Insertion: " ;
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
$n = insertSorted( $arr , $n ,
$key , $capacity );
echo "\nAfter Insertion: " ;
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
?>
|
Output
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Insert at any position
Insert operation in an array at any position can be performed by shifting elements to the right, which are on the right side of the required position
Coding implementation of inserting an element at any position:
C++
#include <bits/stdc++.h>
using namespace std;
void insertElement( int arr[], int n, int x, int pos)
{
for ( int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
int main()
{
int arr[15] = { 2, 4, 1, 8, 5 };
int n = 5;
cout<< "Before insertion : " ;
for ( int i = 0; i < n; i++)
cout<<arr[i]<< " " ;
cout<<endl;
int x = 10, pos = 2;
insertElement(arr, n, x, pos);
n++;
cout<< "After insertion : " ;
for ( int i = 0; i < n; i++)
cout<<arr[i]<< " " ;
return 0;
}
|
C
#include <stdio.h>
void insertElement( int arr[], int n, int x, int pos)
{
for ( int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
int main()
{
int arr[15] = { 2, 4, 1, 8, 5 };
int n = 5;
printf ( "Before insertion : " );
for ( int i = 0; i < n; i++)
printf ( "%d " , arr[i]);
printf ( "\n" );
int x = 10, pos = 2;
insertElement(arr, n, x, pos);
n++;
printf ( "After insertion : " );
for ( int i = 0; i < n; i++)
printf ( "%d " , arr[i]);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void insertElement( int arr[], int n, int x,
int pos)
{
for ( int i = n - 1 ; i >= pos; i--)
arr[i + 1 ] = arr[i];
arr[pos] = x;
}
public static void main(String[] args)
{
int arr[] = new int [ 15 ];
arr[ 0 ] = 2 ;
arr[ 1 ] = 4 ;
arr[ 2 ] = 1 ;
arr[ 3 ] = 8 ;
arr[ 4 ] = 5 ;
int n = 5 ;
int x = 10 , pos = 2 ;
System.out.print( "Before Insertion: " );
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
insertElement(arr, n, x, pos);
n += 1 ;
System.out.print( "\n\nAfter Insertion: " );
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
}
|
Python3
def insertElement(arr, n, x, pos) :
for i in range (n - 1 ,pos - 1 , - 1 ) :
arr[i + 1 ] = arr[i]
arr[pos] = x
if __name__ = = '__main__' :
arr = [ 2 , 4 , 1 , 8 , 5 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ]
n = 5
print ( "Before insertion : " )
for i in range ( 0 ,n) :
print (arr[i],end = ' ' )
print ( "\n" )
x = 10 ;
pos = 2 ;
insertElement(arr, n, x, pos);
n + = 1
print ( "After insertion : " )
for i in range ( 0 ,n) :
print (arr[i],end = ' ' )
|
C#
using System;
class main {
static void insertElement( int [] arr, int n, int x,
int pos)
{
for ( int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
public static void Main()
{
int [] arr = new int [20];
arr[0] = 2;
arr[1] = 4;
arr[2] = 1;
arr[3] = 8;
arr[4] = 5;
int x = 10;
int n = 5;
int pos = 2;
Console.Write( "Before Insertion: " );
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
insertElement(arr, n, x, pos);
n += 1;
Console.Write( "\n\nAfter Insertion: " );
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
function insertElement(arr, n, x, pos)
{
var i = n - 1;
for (i; i >= pos; i--)
{
arr[i + 1] = arr[i];
}
arr[pos] = x;
}
var arr = Array(15).fill(0);
arr[0] = 2;
arr[1] = 4;
arr[2] = 1;
arr[3] = 8;
arr[4] = 5;
var n = 5;
var x = 10;
var pos = 2;
console.log( "Before Insertion: " );
var i = 0;
for (i; i < n; i++)
{
console.log(arr[i] + " " );
}
insertElement(arr, n, x, pos);
n += 1;
console.log( "\n\nAfter Insertion: " );
i = 0;
for (i; i < n; i++)
{
console.log(arr[i] + " " );
}
|
PHP
<?php
function insertElement(& $arr , $n , $x , $pos ) {
for ( $i = $n - 1; $i >= $pos ; $i --) {
$arr [ $i + 1] = $arr [ $i ];
}
$arr [ $pos ] = $x ;
}
$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;
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++
#include <iostream>
using namespace std;
int findElement( int arr[], int n, int key);
int deleteElement( int arr[], int n, int key)
{
int pos = findElement(arr, n, key);
if (pos == -1) {
cout << "Element not found" ;
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int findElement( int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main()
{
int i;
int arr[] = { 10, 50, 30, 40, 20 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 30;
cout << "Array before deletion\n" ;
for (i = 0; i < n; i++)
cout << arr[i] << " " ;
n = deleteElement(arr, n, key);
cout << "\n\nArray after deletion\n" ;
for (i = 0; i < n; i++)
cout << arr[i] << " " ;
return 0;
}
|
C
#include <stdio.h>
int findElement( int arr[], int n, int key);
int deleteElement( int arr[], int n, int key)
{
int pos = findElement(arr, n, key);
if (pos == -1) {
printf ( "Element not found" );
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int findElement( int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main()
{
int i;
int arr[] = { 10, 50, 30, 40, 20 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 30;
printf ( "Array before deletion\n" );
for (i = 0; i < n; i++)
printf ( "%d " , arr[i]);
n = deleteElement(arr, n, key);
printf ( "\nArray after deletion\n" );
for (i = 0; i < n; i++)
printf ( "%d " , arr[i]);
return 0;
}
|
Java
class Main {
static int findElement( int arr[], int n, int key)
{
int i;
for (i = 0 ; i < n; i++)
if (arr[i] == key)
return i;
return - 1 ;
}
static int deleteElement( int arr[], int n, int key)
{
int pos = findElement(arr, n, key);
if (pos == - 1 ) {
System.out.println( "Element not found" );
return n;
}
int i;
for (i = pos; i < n - 1 ; i++)
arr[i] = arr[i + 1 ];
return n - 1 ;
}
public static void main(String args[])
{
int i;
int arr[] = { 10 , 50 , 30 , 40 , 20 };
int n = arr.length;
int key = 30 ;
System.out.println( "Array before deletion" );
for (i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
n = deleteElement(arr, n, key);
System.out.println( "\n\nArray after deletion" );
for (i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
}
|
Python3
if __name__ = = '__main__' :
arr = [ 10 , 50 , 30 , 40 , 20 ]
key = 30
print ( "Array before deletion:" )
print (arr)
arr.remove(key)
print ( "Array after deletion" )
print (arr)
|
C#
using System;
class main {
static int findElement( int [] arr, int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
static int deleteElement( int [] arr, int n, int key)
{
int pos = findElement(arr, n, key);
if (pos == -1) {
Console.WriteLine( "Element not found" );
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
public static void Main()
{
int i;
int [] arr = { 10, 50, 30, 40, 20 };
int n = arr.Length;
int key = 30;
Console.Write( "Array before deletion " );
for (i = 0; i < n; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
n = deleteElement(arr, n, key);
Console.Write( "Array after deletion " );
for (i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
function findElement(arr,n,key)
{
let i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
function deleteElement(arr,n,key)
{
let pos = findElement(arr, n, key);
if (pos == -1)
{
document.write( "Element not found" );
return n;
}
let i;
for (i = pos; i< n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
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]+ " " );
|
PHP
<?php
function findElement(& $arr , $n , $key )
{
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] == $key )
return $i ;
return -1;
}
function deleteElement(& $arr , $n , $key )
{
$pos = findElement( $arr , $n , $key );
if ( $pos == -1)
{
echo "Element not found" ;
return $n ;
}
for ( $i = $pos ; $i < $n - 1; $i ++)
$arr [ $i ] = $arr [ $i + 1];
return $n - 1;
}
$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 ] . " " ;
$n = deleteElement( $arr , $n , $key );
echo "\nArray after deletion\n" ;
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
?>
|
Output
Array before deletion
10 50 30 40 20
Array after deletion
10 50 40 20
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...