Professional Documents
Culture Documents
Search, Insert and Delete in A Sorted Array - GeeksforGeeks
Search, Insert and Delete in A Sorted Array - GeeksforGeeks
{
if (endIndex > startIndex)
return -1;
Search Operation
In a sorted array, the search operation can be performed by using binary search.
C
// C program to implement binary search in sorted array
#include<stdio.h>
n = sizeof(arr)/sizeof(arr[0]);
key =10;
Java
// Java program to implement binary
// search in a sorted array
class Main
{
// function to implement
// binary search
static int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
System.out.println("Index: " +
binarySearch(arr,0, n, key) );
}
}
Run on IDE
Python3
# python 3 program to implement
# binary search in sorted array
if (key == arr[int(mid)]):
return mid ▲
Output:
Index: 5
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 to be placed.
C
// C program to implement insert operation in
// an sorted array.
#include<stdio.h>
int i;
for (i=n-1; ( i >= 0 && arr[i] > key); i--)
arr[i+1] = arr[i];
arr[i+1] = key;
return (n+1);
}
// Inserting key
n = insertSorted(arr, n, key, capacity);
return 0;
}
Run on IDE
Java
// Java program to insert an
// element in a sorted array
class Main
{
// Inserts a key in arr[] of given
// capacity. n is current size of arr[].
// This function returns n+1 if insertion
// is successful, else n.
static int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity)
return n;
int i;
for (i=n-1; (i >= 0 && arr[i] > key); i--)
arr[i+1] = arr[i];
arr[i+1] = key;
return (n+1);
}
// Inserting key
n = insertSorted(arr, n, key, capacity);
Output:
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 26 40 50 70
Delete Operation
In delete operation, the element to be deleted is searched using binary search and then delete operation is
performed followed by shifting the elements.
C
// C program to implement delete operation in a
// sorted array
#include<stdio.h>
if (pos==-1)
{
printf("Element not found");
return n;
}
// Deleting element
int i;
for (i=pos; i<n; i++)
arr[i] = arr[i+1];
return n-1;
}
// Driver code
int main() ▲
{
int i;
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr)/sizeof(arr[0]);
int key = 30;
n = deleteElement(arr, n, key);
Java
// Java program to delete an
// element from a sorted array
class Main
{
// binary search
static int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high)/2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid -1), key);
}
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;
}
int i;
int arr[] = {10, 20, 30, 40, 50};
▲
int n = arr.length;
int key = 30;
n = deleteElement(arr, n, key);
Output:
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help
other Geeks. ▲
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Recommended Posts:
Given an array A[] and a number x, check for pair in A[] with sum as x
Search, insert and delete in an unsorted array
XOR of all subarray XORs | Set 1
Write a program to reverse an array or string
Majority Element
Replace two consecutive equal values with one greater
Count smaller elements on right side using Set in C++ STL
Maximum height of triangular arrangement of array values
Making elements of two arrays same with minimum increment/decrement
Maximum equlibrium sum in an array
(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