0

Why do most people use insertion sort for sub-arrays that have less elements than n to optimise quick sort? I wrote an insertion sort function and shell sort function and called them with few random arrays that have 10, 50, 100 elements. It seemed that shell sort is faster (I only measured the time with clock(); I don't know if it's a good way). If it's faster than insertion sort, why don't more people use shell sort? Do I have a mistake in insertion sort function?

My Code :

double insertionSort(int *a, int size)
{
    clock_t start, end;
    int i, temp, pos;

    start = clock();

    for (i = 1; i < size; i++) {
        temp = a[i];
        pos = i - 1;

        while (pos >= 0 && a[pos] > temp) {
            a[pos + 1] = a[pos];
            pos--;
        }

        a[pos + 1] = temp;

     }

     end = clock();

     return (double)(end - start) / CLOCKS_PER_SEC * 1000;
}

double shellSort(int *a, int size)
{
    int gaps[8] = {701, 301, 132, 57, 23, 10, 4, 1};
    int i, k , temp, pos;
    clock_t start, end;

    start = clock();

    for (i = 0; i < 8; i++)
        for (k = gaps[i]; k < size; k++) {
            temp = a[k];
            pos = k - gaps[i];
            while (pos >= 0 && a[pos] > temp) {
                a[pos + gaps[i]] = a[pos];
                pos -= gaps[i];
            }
            a[pos + gaps[i]] = temp;
        }

    end = clock();

    return (double)(end - start) / CLOCKS_PER_SEC * 1000;
}

Output for 5 :

Insertion Sort : Sorted in 0.002000 milliseconds
Shell Sort : Sorted in 0.001000 milliseconds

Output for 10 :

Insertion Sort : Sorted in 0.004000 milliseconds
Shell Sort : Sorted in 0.001000 milliseconds

Output for 50 :

Insertion Sort : Sorted in 0.007000 milliseconds
Shell Sort : Sorted in 0.007000 milliseconds

Output for 100 :

Insertion Sort : Sorted in 0.015000 milliseconds
Shell Sort : Sorted in 0.014000 milliseconds

Output for 200 :

Insertion Sort : Sorted in 0.040000 milliseconds
Shell Sort : Sorted in 0.028000 milliseconds
yunusaydin
  • 347
  • 2
  • 13
  • possible duplicate of [Is there ever a good reason to use Insertion Sort?](http://stackoverflow.com/questions/736920/is-there-ever-a-good-reason-to-use-insertion-sort) – Alexei Levenkov Dec 30 '13 at 08:53
  • @AlexeiLevenkov I have already read that question, nobody there hasn't mentioned shell sort. That's why I asked this. :) – yunusaydin Dec 30 '13 at 09:08
  • 1
    Try a linked list implementation of insertion sort. It is a lot faster if you don't have to shift everything down. Sorting is all to do with how many comparisons and exchanges are required. It depends entirely on your data. You can even get a bubblesort running faster than a quicksort or shell-metzner if only one item is out of place. – cup Dec 30 '13 at 10:17
  • 1
    "Why are we using insertion sort for sub-arrays that have less elements than n to optimise quick sort? " -- Who is "we"? If you're asking why some particular library version of quicksort uses insertion sort, then best ask it authors. (Odds are that your results are specific to your machine and test methodology, and odds are that's not accurate.) The best version of quicksort is introsort, which uses heapsort rather than insertion sort or shell sort, and switches to it based on the stack depth (the log of the number of elements). – Jim Balter Aug 27 '14 at 18:17

1 Answers1

0

Check THis Link

enter link description here

Sorting Algorithm Worst-case time Average-case time

Insertion O(n^2) O(n^2)

Quick Sort O(n^2) O(nlogn)

AVIK DUTTA
  • 736
  • 6
  • 23