How it works
Adaptive run-stack merge strategy related to practical natural-merge hybrids.
Implementation
function shiversSort(arr, stats) { const n = arr.length; if (n < 2) return; const temp = new Array(n).fill(0); for (let width = 1; width < n; width *= 2) { for (let lo = 0; lo < n; lo += 2 * width) { const mid = Math.min(lo + width, n); const hi = Math.min(lo + 2 * width, n); if (mid >= hi) continue; let i = lo; let j = mid; let k = lo; while (i < mid && j < hi) { if (arr[i] <= arr[j]) { temp[k] = arr[i]; i++; } else { temp[k] = arr[j]; j++; } k++; } while (i < mid) { temp[k] = arr[i]; i++; k++; } while (j < hi) { temp[k] = arr[j]; j++; k++; } for (let p = lo; p < hi; p++) { arr[p] = temp[p]; } } } }
def sort(arr, n, stats): if (n < 2): return temp = [0] * n width = 1 while (width < n): for lo in range(0, n, (2 * width)): mid = min((lo + width), n) hi = min((lo + (2 * width)), n) if (mid >= hi): continue i = lo j = mid k = lo while ((i < mid) and (j < hi)): if (arr[i] <= arr[j]): temp[k] = arr[i] i += 1 else: temp[k] = arr[j] j += 1 k += 1 while (i < mid): temp[k] = arr[i] i += 1 k += 1 while (j < hi): temp[k] = arr[j] j += 1 k += 1 for p in range(lo, hi): arr[p] = temp[p] width *= 2
#include <vector> #include <algorithm> void sort(std::vector<int>& arr, int n, int& comparisons, int& swaps) { if((n < 2)) { return; } std::vector<int> temp(n, 0); for(int width=1; (width < n); width *= 2) { for(int lo=0; lo<n; lo+=(2 * width)) { int mid = (((lo + width)) < (n) ? ((lo + width)) : (n)); int hi = (((lo + (2 * width))) < (n) ? ((lo + (2 * width))) : (n)); if((mid >= hi)) { continue; } int i = lo; int j = mid; int k = lo; while(((i < mid) && (j < hi))) { if((arr[i] <= arr[j])) { temp[k] = arr[i]; i++; } else { temp[k] = arr[j]; j++; } k++; } while((i < mid)) { temp[k] = arr[i]; i++; k++; } while((j < hi)) { temp[k] = arr[j]; j++; k++; } for(int p=lo; p<hi; p++) { arr[p] = temp[p]; } } } }
public void Sort(int[] arr, int n, dynamic stats) { if((n < 2)) { return; } int[] temp = new int[n]; for(int width=1; (width < n); width *= 2) { for(int lo=0; lo<n; lo+=(2 * width)) { int mid = Math.Min((lo + width), n); int hi = Math.Min((lo + (2 * width)), n); if((mid >= hi)) { continue; } int i = lo; int j = mid; int k = lo; while(((i < mid) && (j < hi))) { if((arr[i] <= arr[j])) { temp[k] = arr[i]; i++; } else { temp[k] = arr[j]; j++; } k++; } while((i < mid)) { temp[k] = arr[i]; i++; k++; } while((j < hi)) { temp[k] = arr[j]; j++; k++; } for(int p=lo; p<hi; p++) { arr[p] = temp[p]; } } } }
#include <stdio.h> #include <string.h> void sort(int arr[], int n, int* comparisons, int* swaps) { if((n < 2)) { return; } int temp[n]; memset(temp, 0, sizeof(temp)); for(int width=1; (width < n); width *= 2) { for(int lo=0; lo<n; lo+=(2 * width)) { int mid = (((lo + width)) < (n) ? ((lo + width)) : (n)); int hi = (((lo + (2 * width))) < (n) ? ((lo + (2 * width))) : (n)); if((mid >= hi)) { continue; } int i = lo; int j = mid; int k = lo; while(((i < mid) && (j < hi))) { if((arr[i] <= arr[j])) { temp[k] = arr[i]; i++; } else { temp[k] = arr[j]; j++; } k++; } while((i < mid)) { temp[k] = arr[i]; i++; k++; } while((j < hi)) { temp[k] = arr[j]; j++; k++; } for(int p=lo; p<hi; p++) { arr[p] = temp[p]; } } } }