How it works
Quicksort variant using block partitioning to reduce branch mispredictions.
Implementation
function blockQuickSort(arr, stats) { const n = arr.length; if (n < 2) return; const stackLo = new Array(n + 1).fill(0); const stackHi = new Array(n + 1).fill(0); let top = 0; stackLo[top] = 0; stackHi[top] = n - 1; top++; while (top > 0) { top--; let lo = stackLo[top]; let hi = stackHi[top]; while (lo < hi) { const pivot = arr[hi]; let store = lo; for (let i = lo; i < hi; i++) { if (arr[i] <= pivot) { const t = arr[store]; arr[store] = arr[i]; arr[i] = t; store++; } } const t = arr[store]; arr[store] = arr[hi]; arr[hi] = t; if (store - lo > hi - store) { if (lo < store - 1) { stackLo[top] = lo; stackHi[top] = store - 1; top++; } lo = store + 1; } else { if (store + 1 < hi) { stackLo[top] = store + 1; stackHi[top] = hi; top++; } hi = store - 1; } } } }
def sort(arr, n, stats): if (n < 2): return stackLo = [0] * (n + 1) stackHi = [0] * (n + 1) top = 0 stackLo[top] = 0 stackHi[top] = (n - 1) top += 1 while (top > 0): top -= 1 lo = stackLo[top] hi = stackHi[top] while (lo < hi): pivot = arr[hi] store = lo for i in range(lo, hi): if (arr[i] <= pivot): t = arr[store] arr[store] = arr[i] arr[i] = t store += 1 t = arr[store] arr[store] = arr[hi] arr[hi] = t if ((store - lo) > (hi - store)): if (lo < (store - 1)): stackLo[top] = lo stackHi[top] = (store - 1) top += 1 lo = (store + 1) else: if ((store + 1) < hi): stackLo[top] = (store + 1) stackHi[top] = hi top += 1 hi = (store - 1)
#include <vector> #include <algorithm> void sort(std::vector<int>& arr, int n, int& comparisons, int& swaps) { if((n < 2)) { return; } std::vector<int> stackLo((n + 1), 0); std::vector<int> stackHi((n + 1), 0); int top = 0; stackLo[top] = 0; stackHi[top] = (n - 1); top++; while((top > 0)) { top--; int lo = stackLo[top]; int hi = stackHi[top]; while((lo < hi)) { int pivot = arr[hi]; int store = lo; for(int i=lo; i<hi; i++) { if((arr[i] <= pivot)) { int t = arr[store]; arr[store] = arr[i]; arr[i] = t; store++; } } int t = arr[store]; arr[store] = arr[hi]; arr[hi] = t; if(((store - lo) > (hi - store))) { if((lo < (store - 1))) { stackLo[top] = lo; stackHi[top] = (store - 1); top++; } lo = (store + 1); } else { if(((store + 1) < hi)) { stackLo[top] = (store + 1); stackHi[top] = hi; top++; } hi = (store - 1); } } } }
public void Sort(int[] arr, int n, dynamic stats) { if((n < 2)) { return; } int[] stackLo = new int[(n + 1)]; int[] stackHi = new int[(n + 1)]; int top = 0; stackLo[top] = 0; stackHi[top] = (n - 1); top++; while((top > 0)) { top--; int lo = stackLo[top]; int hi = stackHi[top]; while((lo < hi)) { int pivot = arr[hi]; int store = lo; for(int i=lo; i<hi; i++) { if((arr[i] <= pivot)) { int t = arr[store]; arr[store] = arr[i]; arr[i] = t; store++; } } int t = arr[store]; arr[store] = arr[hi]; arr[hi] = t; if(((store - lo) > (hi - store))) { if((lo < (store - 1))) { stackLo[top] = lo; stackHi[top] = (store - 1); top++; } lo = (store + 1); } else { if(((store + 1) < hi)) { stackLo[top] = (store + 1); stackHi[top] = hi; top++; } hi = (store - 1); } } } }
#include <stdio.h> #include <string.h> void sort(int arr[], int n, int* comparisons, int* swaps) { if((n < 2)) { return; } int stackLo[(n + 1)]; memset(stackLo, 0, sizeof(stackLo)); int stackHi[(n + 1)]; memset(stackHi, 0, sizeof(stackHi)); int top = 0; stackLo[top] = 0; stackHi[top] = (n - 1); top++; while((top > 0)) { top--; int lo = stackLo[top]; int hi = stackHi[top]; while((lo < hi)) { int pivot = arr[hi]; int store = lo; for(int i=lo; i<hi; i++) { if((arr[i] <= pivot)) { int t = arr[store]; arr[store] = arr[i]; arr[i] = t; store++; } } int t = arr[store]; arr[store] = arr[hi]; arr[hi] = t; if(((store - lo) > (hi - store))) { if((lo < (store - 1))) { stackLo[top] = lo; stackHi[top] = (store - 1); top++; } lo = (store + 1); } else { if(((store + 1) < hi)) { stackLo[top] = (store + 1); stackHi[top] = hi; top++; } hi = (store - 1); } } } }