How it works
Selection sort adapted for duplicates.
Implementation
function bingoSort(arr, stats) { const n = arr.length; let max = n - 1; let nextValue = arr[max]; for (let i = max - 1; i >= 0; i--) { if (arr[i] > nextValue) nextValue = arr[i]; } while (max > 0 && arr[max] === nextValue) { max--; } while (max > 0) { let value = nextValue; nextValue = arr[max]; for (let i = max - 1; i >= 0; i--) { compare(i, max); if (arr[i] === value) { [arr[i], arr[max]] = [arr[max], arr[i]]; swap(i, max); max--; } else if (arr[i] > nextValue) { nextValue = arr[i]; } } while (max > 0 && arr[max] === nextValue) { max--; } } }
def bingoSort(arr, stats): max = (n - 1) nextValue = arr[max] for i in range((max - 1), (0 - 1), -1): if (arr[i] > nextValue): nextValue = arr[i] while ((max > 0) and (arr[max] == nextValue)): max -= 1 while (max > 0): value = nextValue nextValue = arr[max] for i in range((max - 1), (0 - 1), -1): compare(i, max) if (arr[i] == value): arr[i], arr[max] = arr[max], arr[i] swap(i, max) max -= 1 elif (arr[i] > nextValue): nextValue = arr[i] while ((max > 0) and (arr[max] == nextValue)): max -= 1
#include <vector> #include <algorithm> void sort(std::vector<int>& arr, int n, int& comparisons, int& swaps) { int max = (n - 1); int nextValue = arr[max]; for(int i=(max - 1); i>(0 - 1); i--) { if((arr[i] > nextValue)) { nextValue = arr[i]; } } while(((max > 0) && (arr[max] == nextValue))) { max--; } while((max > 0)) { int value = nextValue; nextValue = arr[max]; for(int i=(max - 1); i>(0 - 1); i--) { compare(i, max); if((arr[i] == value)) { std::swap(arr[i], arr[max]); swap(i, max); max--; } else if((arr[i] > nextValue)) { nextValue = arr[i]; } } while(((max > 0) && (arr[max] == nextValue))) { max--; } } }
public void Sort(int[] arr, int n, dynamic stats) { int max = (n - 1); int nextValue = arr[max]; for(int i=(max - 1); i>(0 - 1); i--) { if((arr[i] > nextValue)) { nextValue = arr[i]; } } while(((max > 0) && (arr[max] == nextValue))) { max--; } while((max > 0)) { int value = nextValue; nextValue = arr[max]; for(int i=(max - 1); i>(0 - 1); i--) { compare(i, max); if((arr[i] == value)) { { int _t = arr[i]; arr[i] = arr[max]; arr[max] = _t; } swap(i, max); max--; } else if((arr[i] > nextValue)) { nextValue = arr[i]; } } while(((max > 0) && (arr[max] == nextValue))) { max--; } } }
#include <stdio.h> void sort(int arr[], int n, int* comparisons, int* swaps) { int max = (n - 1); int nextValue = arr[max]; for(int i=(max - 1); i>(0 - 1); i--) { if((arr[i] > nextValue)) { nextValue = arr[i]; } } while(((max > 0) && (arr[max] == nextValue))) { max--; } while((max > 0)) { int value = nextValue; nextValue = arr[max]; for(int i=(max - 1); i>(0 - 1); i--) { compare(i, max); if((arr[i] == value)) { { int _t = arr[i]; arr[i] = arr[max]; arr[max] = _t; } swap(i, max); max--; } else if((arr[i] > nextValue)) { nextValue = arr[i]; } } while(((max > 0) && (arr[max] == nextValue))) { max--; } } }