Why insertion sort works better than quick-sort for small data?

In summary, insertion sort is recommended for small numbers of elements (around 30-40) because its worst case time complexity is n^2, which is better than quicksort's nlogn worst case. However, for larger numbers of elements, quicksort's nlogn complexity is more efficient. Merge sort can also be used, but its complexity is nlogn + n, which is not as efficient as quicksort. The number of elements moved in each pass of merge sort is n, and the number of passes is log2(n), resulting in a total of nlog2(n) moves. The worst case number of compares in merge sort is nlog2(n) - n + 1. The constants in front
  • #1
Avichal
295
0
I have seen in books that when number of elements is small ~ 30-40 insertion sort is recommended. Why is that? The worst case of insertion sort is n2 whereas for quick-sort it is nlogn.

nlogn beats n2 for all values of n isn't it? Then why?
 
Technology news on Phys.org
  • #2
Avichal said:
nlogn beats n2 for all values of n isn't it?

Each step of quicksort normally requires more "work" than a step of insertion sort. Compare 10000 n log n with 10 n2. Of course, this is an exaggeration, but it illustrates the basic idea.
 
  • #3
Merge sort is much easier for me so let's talk compare merge-sort with insertions sort.
In worst case insertion sort will be 1 + 2 + 3 + ... + n which is n.(n+1) / 2 iterations/steps.
There is not worst case in merge sort. Each step (that is dividing the array by two) takes n iterations (that is merging). This happens logn or logn + 1 times. Thus total iterations/steps is n.logn or n.logn + n

Where does the constant in front comes from?
 
  • #4
Avichal said:
Merge sort is much easier for me so let's talk compare merge-sort with insertions sort.
Using a bottom up merge sort is easier to explain. In a top down merge sort, no merging takes place until sub-array's of size 1 are produced by recursively dividing the array by 2 (or creating a tree of indexes on the stack).

The initial condition of a merge sort considers an array of size n as n arrays of size 1 (an array of size 1 can be considered sorted) and the first pass merges those n arrays of size 1 into a secondary buffer producing n/2 sorted arrays of size 2. The next pass merges the n/2 sorted arrays of size 2 back into the primary buffer producing n/4 sorted arrays of size 4. This merge process is repeated until there is 1 sorted array. The number of elements moved on each pass is n, and the number of passes is log2(n), so the number of moves is n log2(n). The worst case number of compares is n log2(n) - n + 1. This is explained next:

On the first pass each pair of arrays of size 1 are merged taking 1 compare, so that's n/2 compares. On the second pass, the worst case number of compares for each pair of arrays of size 2 is 3, so that's 3n/4 compares. For the third pass, 7n/8. For the final log2(n) pass, each pair of arrays of size n/2 are merged taking n-1 compares in the worst case, for a total of (n-1) n / n = n - 1. To calculate the sum, subtract 1x the number of compares from 2x the number of compares:

Code:
  2x # compares = 1n/1 + 3n/2 + 7n/4 + ... 
- 1x # compares =        1n/2 + 3n/4 + ... + (n-1)
------------------------------------------------
  1x # compares =    n +    n +    n + ... - (n-1) = n log2(n) - n + 1
 
Last edited:
  • #5
rcgldr said:
Using a bottom up merge sort is easier to explain. In a top down merge sort, no merging takes place until sub-array's of size 1 are produced by recursively dividing the array by 2 (or creating a tree of indexes on the stack).

The initial condition of a merge sort considers an array of size n as n arrays of size 1 (an array of size 1 can be considered sorted) and the first pass merges those n arrays of size 1 into a secondary buffer producing n/2 sorted arrays of size 2. The next pass merges the n/2 sorted arrays of size 2 back into the primary buffer producing n/4 sorted arrays of size 4. This merge process is repeated until there is 1 sorted array. The number of elements moved on each pass is n, and the number of passes is log2(n), so the number of moves is n log2(n). The worst case number of compares is n log2(n) - n + 1.

Thanks for the explanation.
jtbell said:
Each step of quicksort normally requires more "work" than a step of insertion sort. Compare 10000 n log n with 10 n2. Of course, this is an exaggeration, but it illustrates the basic idea.
Anyways where do the constants 10000 and 10 come up? As explained above the number of steps/comparisons is n log2n ... no big constants in it
 
  • #6
Avichal said:
Thanks for the explanation.
I updated my previous post to explain how the worst case number of compares are calclated.

Avichal said:
Anyways where do the constants 10000 and 10 come up?
That was an exagerration. The issue is the number of temporary variables, like indexes, and size of array, and how often they have to be updated in order to implement a sort algorithm. So the overhead per comparason, swap, or move may be higher for the "better" algorithms, making them slower if the number of elements in an array is very small.
 
  • #7
rcgldr said:
That was an exagerration. The issue is the number of temporary variables, like indexes, and size of array, and how often they have to be updated in order to implement a sort algorithm. So the overhead per comparason, swap, or move may be higher for the "better" algorithms, making them slower if the number of elements in an array is very small.

Yeah I knew they were an exaggeration but couldn't see how any constants could rise. I totally forgot about overhead time taken to divide the array into two, calling other functions, swapping etc.
I was just looking at the comparisons!
Anyways thank you for clearing the doubt
 

1. Why is insertion sort better than quick-sort for small data?

Insertion sort is better than quick-sort for small data because it has a time complexity of O(n) for a nearly sorted list, while quick-sort has a worst-case time complexity of O(n^2). This means that insertion sort is more efficient for small data sets because it requires fewer comparisons and swaps.

2. What is the main difference between insertion sort and quick-sort?

The main difference between insertion sort and quick-sort is their sorting algorithms. Insertion sort works by taking an element from the unsorted list and inserting it into its correct position in the sorted list. Quick-sort, on the other hand, works by selecting a pivot element and partitioning the list into two sub-lists based on the pivot, then recursively sorting each sub-list.

3. Can insertion sort ever be better than quick-sort for large data sets?

No, insertion sort is not better than quick-sort for large data sets. While insertion sort may be more efficient for small data sets, it has a time complexity of O(n^2) for large data sets, making it much slower than quick-sort, which has a time complexity of O(nlogn).

4. Why does insertion sort have a better space complexity than quick-sort?

Insertion sort has a better space complexity than quick-sort because it does not require any additional space for auxiliary arrays or recursion. It sorts the list in-place, meaning it only requires the same amount of space as the original list. Quick-sort, on the other hand, requires additional space for auxiliary arrays and recursion, making its space complexity worse.

5. Can insertion sort and quick-sort be combined to create a more efficient sorting algorithm?

Yes, insertion sort and quick-sort can be combined to create a more efficient sorting algorithm. This is known as hybrid sorting, where insertion sort is used for smaller sub-lists and quick-sort is used for larger sub-lists. This takes advantage of insertion sort's efficiency for small data sets and quick-sort's efficiency for large data sets, resulting in a more efficient algorithm overall.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
11
Views
988
  • Programming and Computer Science
Replies
1
Views
2K
Replies
4
Views
985
  • Precalculus Mathematics Homework Help
Replies
12
Views
2K
  • Programming and Computer Science
Replies
17
Views
7K
  • Programming and Computer Science
Replies
3
Views
4K
Replies
3
Views
933
Back
Top