Insertion Sort

Properties

  • Stable
  • O(1) extra space
  • O(n2) comparisons and swaps
  • Adaptive: O(n) time when nearly sorted
  • Very low overhead

Discussion

Although it is one of the elementary sorting algorithms with O(n2) worst-case time, insertion sort is the algorithm of choice either when the data is nearly sorted (because it is adaptive) or when the problem size is small (because it has low overhead).
For these reasons, and because it is also stable, insertion sort is often used as the recursive base case (when the problem size is small) for higher overhead divide-and-conquer sorting algorithms, such as merge sort or quick sort.

Program C Code

//Insertion//
void insertion(int * gelendizi, int dizi_boyut)
{
    int i,index,temp;
    for (i = 1 ; i < dizi_boyut; i++)
    {
        index = i;
        while ( index > 0 && gelendizi[index] < gelendizi[index-1])
        {
            temp = gelendizi[index];
            gelendizi[index]   = gelendizi[index-1];
            gelendizi[index-1] = temp;
            index--;
        }
    }
}

Hiç yorum yok:

Yorum Gönder