Data Structures

Search, Insert and Delete in a sorted array

In this post search, insert and delete operation in a sorted array is discussed. Time Complexity:

  • Search: O(Log n) [Using Binary Search]
  • Insert: O(n) [In worst case all elements may have to be moved]
  • Delete: O(n) [In worst case all elements may have to be moved]

Search Operation
In a sorted array, the search operation can be performed by using binary search.

// C program to implement binary search in sorted array
#include<stdio.h>

int binarySearch(int arr[], int low, int high, int key)
{
   if (high < low)
       return -1;
   int mid = (low + high)/2;  /*low + (high - low)/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);
}

/* Driver program to check above functions */
int main()
{
   // Let us search 3 in below array
   int arr[] = {5, 6, 7, 8, 9, 10};
    int n,key;

   n = sizeof(arr)/sizeof(arr[0]);
   key =10;

   printf("Index: %d\n", binarySearch(arr,0, n, key) );
   return 0;
}


Insert Operation

In an unsorted array the insert operation is faster as compared to sorted array because we doesn't have to care about the position at which the element to be placed.

// C program to implement insert operation in
// an sorted array.
#include<stdio.h>

// 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.
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; (arr[i] > key && i >= 0); i--)
       arr[i+1] = arr[i];

    arr[i+1] = key;

    return (n+1);
}

/* Driver program to test above function */
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("\nBefore Insertion: ");
    for (i=0; i<n; i++)
        printf("%d  ", arr[i]);

    // Inserting key
    n = insertSorted(arr, n, key, capacity);

    printf("\nAfter Insertion: ");
    for (i=0; i<n; i++)
        printf("%d  ",arr[i]);

    return 0;
}


Delete Operation
In delete operation the element to be deleted is searched using the binary search and then delete operation is performed followed by shifting the element.

// C rogram to implement delete operation in a
// sorted array
#include<stdio.h>

// To search a ley to be deleted
int binarySearch(int arr[], int low, int high, int key);

/* Function to delete an element */
int deleteElement(int arr[], int n, int key)
{
    // Find position of element to be deleted
    int pos = binarySearch(arr, 0, n-1, key);

    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;
}

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);
}

// Driver code
int main()
{
    int i;
    int arr[] = {10, 20, 30, 40, 50};

    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("\n\nArray after deletion\n");
    for (i=0; i<n; i++)
      printf("%d  ", arr[i]);
}

Recent Comments

Feedback

For any feedback,email us at feedback@geeksforgeeks.org

Report a Bug

For any report or bug,email us at support@geeksforgeeks.org