Professional Documents
Culture Documents
Search, Insert and Delete in An Unsorted Array - GeeksforGeeks PDF
Search, Insert and Delete in An Unsorted Array - GeeksforGeeks PDF
{
for (int i = 0; i < m_length; ++i)
{
if (m_arr[i] == key)
return
Customtrue;
Search
}
Geeks Classes Login
return false;
}
Write an Article
Search Operation
In an unsorted array, the search operation can be performed by linear traversal from the first element to the
last element.
C
// C program to implement linear
// search in unsorted array
#include<stdio.h>
return -1;
}
// Driver Code ▲
int main()
{
int arr[] = {12, 34, 10, 6, 40};
int n = sizeof(arr) / sizeof(arr[0]);
// Using a last element as search element
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;
}
Run on IDE
Java
// Java program to implement linear
// search in unsorted arrays
class Main
{
// Function to implement
// search operation
static int findElement(int arr[], int n,
int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
// Driver Code
public static void main(String args[])
{
int arr[] = {12, 34, 10, 6, 40};
int n = arr.length;
if (position == - 1)
System.out.println("Element not found");
else
System.out.println("Element Found at Position: "
+ (position + 1));
}
}
Run on IDE
Python
# Python program for searching in
# unsorted array
#search operation
index = findElement(arr, n, key)
if index != -1:
print ("element found at position: " + str(index + 1 ))
else:
print ("element not found")
C#
// C# program to implement linear
// search in unsorted arrays
using System;
class main
{
// Function to implement
// search operation
static int findElement(int []arr, int n,
int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
// Driver Code
public static void Main()
{
int []arr = {12, 34, 10, 6, 40};
int n = arr.Length;
if (position == - 1)
Console.WriteLine("Element not found");
else
Console.WriteLine("Element Found at Position: "
+ (position+1));
}
}
▲
PHP
<?php
// PHP program to implement linear
// search in unsorted array
// Function to implement
// search operation
function findElement($arr, $n, $key)
{
$i;
for ($i = 0; $i < $n; $i++)
if ($arr[$i] == $key)
return $i;
return -1;
}
// Driver Code
$arr = array(12, 34, 10, 6, 40);
$n = sizeof($arr);
if ($position == - 1)
echo("Element not found");
else
echo("Element Found at Position: " . ($position + 1));
Output:
Insert Operation
In an unsorted array, the insert operation is faster as compared to sorted array because we don’t have to
care about the position at which the element is to be placed.
C
// C program to implement insert
// operation in an unsorted array.
#include<stdio.h>
arr[n] = key;
return (n + 1);
}
// Driver Code
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;
// Inserting key
n = insertSorted(arr, n, key, capacity);
return 0;
}
Run on IDE
Java
// Java program to implement insert
// operation in an unsorted array.
class Main
{
// Function to insert a given key in
// the array. This function returns n+1
// if insertion is successful, else n.
static int insertSorted(int arr[], int n,
int key,
int capacity)
{
arr[n] = key;
return (n + 1);
}
// Driver Code
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;
// Inserting key
n = insertSorted(arr, n, key, capacity);
Python
# Python program for inserting
# an element in an unsorted array
C#
// C# program to implement insert
// operation in an unsorted array.
using System;
class main
{
arr[n] = key;
return (n + 1);
}
// Driver Code
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;
// Inserting key
n = insertSorted(arr, n, key, capacity);
}
}
Output:
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26
Delete Operation
In delete operation, the element to be deleted is searched using the linear search and then delete
operation is performed followed by shifting the elements.
C
▲
// C program to implement delete operation in a
// unsorted array
#include<stdio.h>
// To search a key to be deleted
int findElement(int arr[], int n,
int key);
if (pos == - 1)
{
printf("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
return - 1;
}
// Driver code
int main()
{
int i;
int arr[] = {10, 50, 30, 40, 20};
n = deleteElement(arr, n, key);
return 0;
}
Run on IDE
Java
// Java program to implement delete ▲
// operation in an unsorted array
class Main
{
// function to search a key to
// be deleted
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;
}
if (pos == -1)
{
System.out.println("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i< n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// Driver Code
public static void main(String args[])
{
int i;
int arr[] = {10, 50, 30, 40, 20};
int n = arr.length;
int key = 30;
n = deleteElement(arr, n, key);
Python
# Python program to delete an element
# from an unsorted array
C#
// C# program to implement delete
// operation in an unsorted array
using System;
class main
{
// Function to search a
// key to be deleted
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;
}
if (pos == - 1)
{
Console.WriteLine("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// Driver Code
public static void Main()
{
int i;
int []arr = {10, 50, 30, 40, 20};
▲
int n = arr.Length;
int key = 30;
n = deleteElement(arr, n, key);
Output:
Time complexities:
Search: O(n)
Insert: O(1)
Delete: O(n)
▲
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above
Arrays Searching Login to Improve this Article
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Recommended Posts:
(Login to Rate)
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
▲
710-B, Advant Navis Business Park,
Sector-142, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org
COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials
PRACTICE CONTRIBUTE
Company-wise Write an Article
Topic-wise GBlog
Contests Videos
Subjective Questions