HTML Preparation code:
x
 
1
<script>
2
  function countingSort(arr) {
3
    let n = arr.length;
4
    let k = Math.max(...arr);
5
    let curr;
6
    
7
    // Create a temporary array prefilled with 0 
8
    // as the same length of max(arr) + 1
9
    const temp = new Array(k + 1).fill(0);
10
11
    // Count the frequency of each element in the input array and store it in temp array
12
    for(let i = 0; i < n; i++){
13
        curr = arr[i];
14
        temp[curr]++;
15
    }
16
17
    // Update the count based on the previous index
18
    for(let i = 1; i <= k; i++){
19
        // Updating elements of count array 
20
        temp[i] = temp[i] + temp[i - 1];  
21
    }
22
23
    const outputArr = new Array(n).fill(0);
24
25
    for(let i = n - 1; i >= 0; i--) {
26
        // Add elements from array A to array B
27
        t = arr[i];
28
        outputArr[temp[curr] - 1] = t;  
29
30
        // De-crement the count value by 1
31
        temp[curr] = temp[curr] - 1;        
32
    }
33
  
34
    return outputArr;
35
    }
36
37
  function swap(ary, a, b) {
38
      var t = ary[a];
39
      ary[a] = ary[b];
40
      ary[b] = t;
41
  }
42
  
43
  // Built-in with comparison function (default sorting is "dictionary-style")
44
  function builtin_sort(ary) {
45
      return ary.sort(function(a, b) {
46
          return a - b;
47
      });
48
  }
49
  
50
  // binary insertion sort
51
  function binaryInsertionSort(array) {
52
    let i, j, x, low, mid, high,loc;
53
54
    for (i = 1; i < 11; i++) {
55
        j = i - 1
56
        x = array[i]
57
58
        low = 0
59
        high = j
60
        while (high > low) {
61
            mid = Math.floor((low + high) / 2)
62
            if (x == array[mid]) {
63
                low = mid + 1
64
                break
65
            }
66
            if (x > array[mid]) {
67
                low = mid + 1
68
                continue
69
            }
70
            high = mid - 1
71
        }
72
73
        loc = x > array[low] ? (low + 1) : low
74
75
        while (j >= loc) {
76
            array[j + 1] = array[j]
77
            j--
78
        }
79
        array[j+1] = x
80
    }
81
    return array
82
}
83
  
84
  // Bubble Sort
85
  function bubble_sort(ary) {
86
      var a, b;
87
      for (a = 0; a < ary.length; a++) {
88
          for (b = a + 1; b < ary.length; b++) {
89
              if (ary[a] > ary[b]) {
90
                  swap(ary, a, b);
91
              }
92
          }
93
      }
94
  
95
      return ary;
96
  }
97
  //Insertion sort
98
  function insertion_sort(ary) {
99
        for(var i=1,l=ary.length;i<l;i++) {
100
            var value = ary[i];
101
            for(var j=i - 1;j>=0;j--) {
102
                if(ary[j] <= value)
103
                    break;
104
                ary[j+1] = ary[j];
105
            }
106
            ary[j+1] = value;
107
        }
108
        return ary;
109
  }
110
  
111
  // Naive (but understandable) quicksort (memory hog)
112
  function naive_quicksort(ary) {
113
      if (ary.length <= 1) {
114
          return ary;
115
      }
116
  
117
      var less = [],
118
          greater = [],
119
          pivot = ary.pop();
120
  
121
      for (var i = 0; i < ary.length; i++) {
122
          if (ary[i] < pivot) {
123
              less.push(ary[i]);
124
          } else {
125
              greater.push(ary[i]);
126
          }
127
      }
128
  
129
      less = naive_quicksort(less);
130
      greater = naive_quicksort(greater);
131
  
132
      return less.concat(pivot, greater);
133
  }
134
  
135
  // In place quicksort
136
  function inplace_quicksort_partition(ary, start, end, pivotIndex) {
137
      var i = start, j = end;
138
      var pivot = ary[pivotIndex];
139
      
140
      while(true) {
141
          while(ary[i] < pivot) {i++};
142
          j--;
143
          while(pivot < ary[j]) {j--};
144
          if(!(i<j)) {
145
              return i;
146
          }
147
          swap(ary,i,j);
148
          i++;
149
     }
150
  }
151
  
152
  function inplace_quicksort(ary, start, end) {
153
      if (start < end - 1) {
154
          var pivotIndex = Math.round((start + end) / 2);
155
  
156
          pivotIndex = inplace_quicksort_partition(ary, start, end, pivotIndex);
157
  
158
          inplace_quicksort(ary, start, pivotIndex - 1);
159
          inplace_quicksort(ary, pivotIndex + 1, end);
160
      }
161
  
162
      return ary;
163
  }
164
  
165
  // Heap sort
166
  function heapSort(ary) {
167
      heapify(ary);
168
  
169
      for (var end = ary.length - 1; end > 0; end--) {
170
          swap(ary, end, 0);
171
          shiftDown(ary, 0, end - 1);
172
      }
173
  
174
      return ary;
175
  }
176
  
177
  function heapify(ary) {
178
      for (var start = (ary.length >> 1) - 1; start >= 0; start--) {
179
          shiftDown(ary, start, ary.length - 1);
180
      }
181
  }
182
  
183
  function shiftDown(ary, start, end) {
184
      var root = start,
185
          child, s;
186
  
187
      while (root * 2 + 1 <= end) {
188
          child = root * 2 + 1;
189
          s = root;
190
  
191
          if (ary[s] < ary[child]) {
192
              s = child;
193
          }
194
  
195
          if (child + 1 <= end && ary[s] < ary[child + 1]) {
196
              s = child + 1;
197
          }
198
  
199
          if (s !== root) {
200
              swap(ary, root, s);
201
              root = s;
202
          } else {
203
              return;
204
          }
205
      }
206
  }
207
  
208
  // Merge sort
209
  function merge_sort(ary) {
210
      if (ary.length <= 1) {
211
          return ary;
212
      }
213
  
214
      var m = ary.length >> 1;
215
  
216
      var left = ary.slice(0, m),
217
          right = ary.slice(m);
218
  
219
      return merge(merge_sort(left), merge_sort(right));
220
  }
221
  
222
  function merge(left, right) {
223
      var result = [],
224
          li = 0, ri = 0;
225
  
226
      while (li < left.length || ri < right.length) {
227
          if (li < left.length && ri < right.length) {
228
              if (left[li] <= right[ri]) {
229
                  result.push(left[li]);
230
                  li++;
231
              } else {
232
                  result.push(right[ri]);
233
                  ri++;
234
              }
235
          } else if (li < left.length) {
236
              result.push(left[li]);
237
              li++;
238
          } else if (ri < right.length) {
239
              result.push(right[ri]);
240
              ri++;
241
          }
242
      }
243
  
244
      return result;
245
  }
246
  
247
  // Shell sort
248
  function shell_sort(ary) {
249
      var inc = Math.round(ary.length / 2),
250
          i, j, t;
251
  
252
      while (inc > 0) {
253
          for (i = inc; i < ary.length; i++) {
254
              t = ary[i];
255
              j = i;
256
              while (j >= inc && ary[j - inc] > t) {
257
                  ary[j] = ary[j - inc];
258
                  j -= inc;
259
              }
260
              ary[j] = t;
261
          }
262
          inc = Math.round(inc / 2.2);
263
      }
264
  
265
      return ary;
266
  }
267
  //Comb Sort (Basically bubblesort with a small modification, but heaps faster)
268
function comb_sort(ary) {
269
    var gap = ary.length;
270
    while(true) {
271
        gap = newgap(gap);
272
        var swapped = false;
273
        for(var i=0,l=ary.length;i<l;i++) {
274
            var j = i + gap;
275
            if(ary[i] < ary[j]) {
276
                swap(ary,i,j);
277
                swapped = true;
278
            }
279
        }
280
        if(gap == 1 && !swapped)
281
             break;
282
    }
283
    return ary;   
284
}
285
function newgap(gap) {
286
    gap = ((gap * 10) / 13) | 0;
287
    if(gap == 9 || gap == 10)
288
        gap = 11;
289
    if(gap < 1)
290
         gap = 1;
291
    return gap;
292
}
293
//faster quicksort using a stack to eliminate recursion, sorting inplace to reduce memory usage, and using insertion sort for small partition sizes
294
function fast_quicksort(ary) {
295
    var stack = [];
296
    var entry = [0,ary.length,2 * Math.floor(Math.log(ary.length)/Math.log(2))];
297
    stack.push(entry);
298
    while(stack.length > 0) {
299
        entry = stack.pop();
300
        var start = entry[0];
301
        var end = entry[1];
302
        var depth = entry[2];
303
        if(depth == 0) {
304
         ary = shell_sort_bound(ary,start,end);
305
         continue;
306
        }
307
        depth--;
308
        var pivot = Math.round((start + end) / 2);
309
            
310
        var pivotNewIndex = inplace_quicksort_partition(ary,start,end, pivot);
311
        if(end - pivotNewIndex > 16) {
312
            entry = [pivotNewIndex,end,depth];
313
            stack.push(entry);
314
        }
315
        if(pivotNewIndex - start > 16) {
316
            entry = [start,pivotNewIndex,depth];
317
            stack.push(entry);
318
        }
319
    }
320
    ary = insertion_sort(ary);
321
    return ary;
322
}
323
function shell_sort_bound(ary,start,end) {
324
      var inc = Math.round((start + end)/2),
325
          i, j, t;
326
  
327
      while (inc >= start) {
328
          for (i = inc; i < end; i++) {
329
              t = ary[i];
330
              j = i;
331
              while (j >= inc && ary[j - inc] > t) {
332
                  ary[j] = ary[j - inc];
333
                  j -= inc;
334
              }
335
              ary[j] = t;
336
          }
337
          inc = Math.round(inc / 2.2);
338
      }
339
  
340
      return ary;
341
  }
342
343
function mSort(list) {
344
                if (list.length < 14) {
345
                    return insertion_sort(list);
346
                }
347
                
348
                var half = Math.floor(list.length / 2);
349
                var a = mSort(list.slice(0, half));
350
                var b = mSort(list.slice(half, list.length));
351
                var aCount = 0;
352
                var bCount = 0;
353
                var returnList = [];
354
                
355
                while (true) {
356
                    if (a[aCount] <= b[bCount]) {
357
                        returnList.push(a[aCount]);
358
                        aCount++;
359
                        if (aCount === a.length) {
360
                            returnList = returnList.concat(b.slice(bCount, b.length));
361
                            break;
362
                        }
363
                    }
364
                    else {
365
                        returnList.push(b[bCount]);
366
                        bCount++;
367
                        if (bCount === b.length) {
368
                            returnList = returnList.concat(a.slice(aCount, a.length));
369
                            break;
370
                        }
371
                    }
372
                }
373
                return returnList;
374
            }
375
  
376
  function bubbleSort(items) {
377
  var length = items.length;
378
  for (var i = 0; i < length; i++) { //Number of passes
379
    for (var j = 0; j < (length - i - 1); j++) { //Notice that j < (length - i)
380
      //Compare the adjacent positions
381
      if(items[j] > items[j+1]) {
382
        //Swap the numbers
383
        var tmp = items[j];  //Temporary variable to hold the current number
384
        items[j] = items[j+1]; //Replace current number with adjacent number
385
        items[j+1] = tmp; //Replace adjacent number with current number
386
      }
387
    }        
388
  }
389
}
390
  
391
  https://stackoverflow.com/a/38979903/8769328
392
  function radixBucketSort (arr) {
393
    var idx1, idx2, idx3, len1, len2, radix, radixKey;
394
    var radices = {}, buckets = {}, num, curr;
395
    var currLen, radixStr, currBucket;
396
397
    len1 = arr.length;
398
    len2 = 10;  // radix sort uses ten buckets
399
400
    // find the relevant radices to process for efficiency        
401
    for (idx1 = 0;idx1 < len1;idx1++) {
402
      radices[arr[idx1].toString().length] = 0;
403
    }
404
405
    // loop for each radix. For each radix we put all the items
406
    // in buckets, and then pull them out of the buckets.
407
    for (radix in radices) {          
408
      // put each array item in a bucket based on its radix value
409
      len1 = arr.length;
410
      for (idx1 = 0;idx1 < len1;idx1++) {
411
        curr = arr[idx1];
412
        // item length is used to find its current radix value
413
        currLen = curr.toString().length;
414
        // only put the item in a radix bucket if the item
415
        // key is as long as the radix
416
        if (currLen >= radix) {
417
          // radix starts from beginning of key, so need to
418
          // adjust to get redix values from start of stringified key
419
          radixKey = curr.toString()[currLen - radix];
420
          // create the bucket if it does not already exist
421
          if (!buckets.hasOwnProperty(radixKey)) {
422
            buckets[radixKey] = [];
423
          }
424
          // put the array value in the bucket
425
          buckets[radixKey].push(curr);          
426
        } else {
427
          if (!buckets.hasOwnProperty('0')) {
428
            buckets['0'] = [];
429
          }
430
          buckets['0'].push(curr);          
431
        }
432
      }
433
      // for current radix, items are in buckets, now put them
434
      // back in the array based on their buckets
435
      // this index moves us through the array as we insert items
436
      idx1 = 0;
437
      // go through all the buckets
438
      for (idx2 = 0;idx2 < len2;idx2++) {
439
        // only process buckets with items
440
        if (buckets[idx2] != null) {
441
          currBucket = buckets[idx2];
442
          // insert all bucket items into array
443
          len1 = currBucket.length;
444
          for (idx3 = 0;idx3 < len1;idx3++) {
445
            arr[idx1++] = currBucket[idx3];
446
          }
447
        }
448
      }
449
      buckets = {};
450
    }
451
  }
452
</script>
Script Preparation code:
 
var input = [];
for (var i = 0; i < 11; i++) {
  input[i] = Math.round(Math.random() * 100);
}
Tests:
  • Built-in Sort

     
    builtin_sort(input.slice(0));
  • Naive Quicksort

     
    naive_quicksort(input.slice(0));
  • Inplace Quicksort

     
    inplace_quicksort(input.slice(0), 0, input.length);
  • Heap Sort

     
    heapSort(input.slice(0));
  • Merge Sort

     
    merge_sort(input.slice(0));
  • Shell Sort

     
    shell_sort(input.slice(0));
  • Comb Sort

     
    comb_sort(input.slice(0));
  • Insertion Sort

     
    insertion_sort(input.slice(0));
  • Fast QuickSort

     
    fast_quicksort(input.slice(0));
  • mSort

     
    mSort(input.slice(0));
  • Radix Bucket Sort

     
    radixBucketSort(input.slice(0)) 
  • Bubble Sort

     
    bubbleSort(input.slice(0));
  • Counting Sort

     
    countingSort(input.slice(0))
  • Binary insertion sort

     
    binaryInsertionSort(input.slice(0))
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Built-in Sort
    Naive Quicksort
    Inplace Quicksort
    Heap Sort
    Merge Sort
    Shell Sort
    Comb Sort
    Insertion Sort
    Fast QuickSort
    mSort
    Radix Bucket Sort
    Bubble Sort
    Counting Sort
    Binary insertion sort

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36
Chrome 100 on Windows
View result in a separate tab
Test name Executions per second
Built-in Sort 1225358.8 Ops/sec
Naive Quicksort 377748.1 Ops/sec
Inplace Quicksort 2105051.5 Ops/sec
Heap Sort 2804407.0 Ops/sec
Merge Sort 709370.3 Ops/sec
Shell Sort 2476747.5 Ops/sec
Comb Sort 2199452.0 Ops/sec
Insertion Sort 3159426.5 Ops/sec
Fast QuickSort 2632909.0 Ops/sec
mSort 3126346.2 Ops/sec
Radix Bucket Sort 381655.7 Ops/sec
Bubble Sort 2953959.2 Ops/sec
Counting Sort 366113.9 Ops/sec
Binary insertion sort 2568222.8 Ops/sec