Note that there are some explanatory texts on larger screens.

plurals
  1. POPoint at which insertion sort beats quicksort
    primarykey
    data
    text
    <p>I'm writing a quicksort that switches to insertion sort when the length of the array being sorted is small. I've run some tests and from what I see, when the array being sorted is 200 elements or less, it is faster to switch to insertion sort. This seems really large, I was expecting something closer to 20. Is 200 reasonable or is one of my implementations way off? I used the basic Wikipedia algorithms.</p> <pre><code>void quicksort(int list[asize], int left, int right) { int pivotindex; int newpivotindex; if (left &lt; right) { if ((right-left)&lt;=200) { insertionsort(list,left,right); } } else { pivotindex = r_rand(left,right); newpivotindex = partition(list, left, right, pivotindex); quicksort(list, left, newpivotindex-1); quicksort(list, newpivotindex+1, right); } return; } int partition(int* list,int left, int right, int pivotindex) { int pivotvalue = list[pivotindex]; int temp; int i; int storeindex; temp=list[pivotindex]; list[pivotindex]=list[right]; list[right]=temp; storeindex=left; for (i=left;i&lt;right;i++) { if (list[i] &lt; pivotvalue) { temp=list[i]; list[i]=list[storeindex]; list[storeindex]=temp; storeindex++; } } temp=list[storeindex]; list[storeindex]=list[right]; list[right]=temp; return storeindex; } void insertionsort(int* list, int left, int right) { int i; int item; int ihole; for (i=left;i&lt;=right;i++) { item=list[i]; ihole=i; while (ihole &gt; 0 &amp;&amp; list[ihole-1] &gt; item) { list[ihole]=list[ihole-1]; ihole--; } list[ihole]=item; } return; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload