nezvers

Introspective sort

Sep 2nd, 2021 (edited)
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. //with adjustments - https://www.programmingalgorithms.com/algorithm/intro-sort/c/
  2. #include "math.h"
  3.  
  4. void InsertionSort(int* data, int count) {
  5.     for (int i = 1; i < count; ++i){
  6.         int j = i;
  7.         while (j > 0){
  8.             if (data[j - 1] > data[j]){
  9.                 int temp = data[j];
  10.                 data[j] = data[j - 1];
  11.                 data[j - 1] = temp;
  12.  
  13.                 --j;
  14.             }
  15.             else{
  16.                 break;
  17.             }
  18.         }
  19.     }
  20. }
  21.  
  22. void MaxHeapify(int* data, int heapSize, int index) {
  23.     int left = (index + 1) * 2 - 1;
  24.     int right = (index + 1) * 2;
  25.     int largest = 0;
  26.  
  27.     if (left < heapSize && data[left] > data[index]){
  28.         largest = left;
  29.     }
  30.     else{
  31.         largest = index;
  32.     }
  33.  
  34.     if (right < heapSize && data[right] > data[largest]){
  35.         largest = right;
  36.     }
  37.  
  38.     if (largest != index){
  39.         int temp = data[index];
  40.         data[index] = data[largest];
  41.         data[largest] = temp;
  42.  
  43.         MaxHeapify(data, heapSize, largest);
  44.     }
  45. }
  46.  
  47. void HeapSort(int* data, int count) {
  48.     int heapSize = count;
  49.  
  50.     for (int p = (heapSize - 1) / 2; p >= 0; --p){
  51.         MaxHeapify(data, heapSize, p);
  52.     }
  53.  
  54.     for (int i = count - 1; i > 0; --i) {
  55.         int temp = data[i];
  56.         data[i] = data[0];
  57.         data[0] = temp;
  58.  
  59.         --heapSize;
  60.         MaxHeapify(data, heapSize, 0);
  61.     }
  62. }
  63.  
  64. int Partition(int* data, int left, int right) {
  65.     int pivot = data[right];
  66.     int temp;
  67.     int i = left;
  68.  
  69.     for (int j = left; j < right; ++j){
  70.         if (data[j] <= pivot){
  71.             temp = data[j];
  72.             data[j] = data[i];
  73.             data[i] = temp;
  74.             i++;
  75.         }
  76.     }
  77.  
  78.     data[right] = data[i];
  79.     data[i] = pivot;
  80.  
  81.     return i;
  82. }
  83.  
  84. void QuickSortRecursive(int* data, int left, int right){
  85.     if (left < right){
  86.         int q = Partition(data, left, right);
  87.         QuickSortRecursive(data, left, q - 1);
  88.         QuickSortRecursive(data, q + 1, right);
  89.     }
  90. }
  91.  
  92. void IntroSort(int* data, int count) {
  93.     int partitionSize = Partition(data, 0, count - 1);
  94.  
  95.     if (partitionSize < 16) {
  96.         InsertionSort(data, count);
  97.     }
  98.     else if (partitionSize >(2 * log(count))){
  99.         HeapSort(data, count);
  100.     }
  101.     else{
  102.         QuickSortRecursive(data, 0, count - 1);
  103.     }
  104. }
Add Comment
Please, Sign In to add comment