Insertion sort

 


Insertion sort:

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heap sort, or merge sort

 

 




This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be 'inserted in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort.

The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2), where n is the number of items.

Basic Operation:

Ø  First we take an unsorted array for our example.








 Ã˜  Insertion sort compares the first two elements.

 







It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub list.

 


  Insertion sort moves ahead and compares 33 with 27.


Ø  Finds that 33 is not in the correct position.



Ø  It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted after swapping.



Ø  By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.

 


Ø  These values are not in a sorted order.


Ø  However, swapping makes 27 and 10 unsorted.



Hence, we swap them too.


Ø  Again we find 14 and 10 in an unsorted order.



 Ã˜  We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.






Program:

#include <bits/stdc++.h>

using namespace std;

// Function for Insertion sort

void insertionSort(int arr[], int n)

{  int i, key, j;

    for (i = 1; i < n; i++)

    {

        key = arr[i];

        j = i - 1;

        // Move elements of arr[0..i-1] that are greater than key

        // to one position ahead of their current position

        while (j >= 0 && arr[j] > key)

        {

            arr[j + 1] = arr[j];

            j = j - 1;

        }

        arr[j + 1] = key;

    }

}

// Function to print an array

void printArray(int arr[], int size)

{

    int i;

    for (i = 0; i < size; i++)

    {

        cout << arr[i] << " ";

    }

    cout << endl;

}

// Driver program

int main()

{

    int arr[] = {64, 25, 12, 22, 11};

    int n = sizeof(arr) / sizeof(arr[0]);

    // Function Call

    insertionSort(arr, n);

    cout << "Sorted array: \n";

    printArray(arr, n);

    return 0;}

Output:



Share this:

ABOUT THE AUTHOR

Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible

0 comments:

Post a Comment