HICONT

Algorithms_sort

Sep 29th, 2023 (edited)
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.27 KB | None | 0 0
  1. void Make_3_Heap(int64_t* arr, int n, int i)
  2. {
  3.     int root = i;
  4.  
  5.     int l = 2 * i + 1;
  6.     int m = 2 * i + 2;
  7.     int r = 2 * i + 3;
  8.  
  9.     if (l < n && arr[l] > arr[root])
  10.         root = l;
  11.     if (m < n && arr[m] > arr[root])
  12.         root = m;
  13.     if (r < n && arr[r] > arr[root])
  14.         root = r;
  15.  
  16.     if (root != i)
  17.     {
  18.         swap(arr[i], arr[root]);
  19.         Make_3_Heap(arr, n, root);
  20.     }
  21. }
  22.  
  23. void Sort_3_Heap(int64_t* arr, int n)
  24. {
  25.     for (int i = n / 2 - 1; i >= 0; i--)
  26.         Make_3_Heap(arr, n, i);
  27.  
  28.     for (int i = n - 1; i >= 0; i--)
  29.     {
  30.         swap(arr[0], arr[i]);
  31.  
  32.         Make_3_Heap(arr, i, 0);
  33.     }
  34. }
  35.  
  36. int Divide(int *b, int il, int j)
  37. {
  38.     int pivot = b[j];
  39.  
  40.     int pIndex = il;
  41.  
  42.  
  43.     for (int i = il; i < j; i++)
  44.     {
  45.         if (b[i] <= pivot)
  46.         {
  47.             swap(b[i], b[pIndex]);
  48.             pIndex++;
  49.         }
  50.     }
  51.  
  52.     swap(b[pIndex], b[j]);
  53.  
  54.     return pIndex;
  55. }
  56.  
  57. void QuickSort(int* b, int i, int j)
  58. {
  59.     if (i >= j)
  60.     {
  61.         return;
  62.     }
  63.     int pivot = Divide(b, i, j);
  64.     QuickSort(b, i, pivot - 1); QuickSort(b, pivot + 1, j);
  65. }
  66.  
  67. class DHeap
  68. {
  69. private:
  70.     int d = 3;
  71.     int64_t* heap_pointer;
  72.     int64_t heap_size;
  73. public:
  74.     void heapify(int64_t* arr_point, int64_t arr_size)
  75.     {
  76.         heap_size = arr_size;
  77.         heap_pointer = new int64_t[heap_size];
  78.         for (int i = 0; i < heap_size; i++)
  79.         {
  80.             heap_pointer[i] = 0;
  81.         }
  82.         for (int i = 0; i < heap_size; i++)
  83.         {
  84.             insert(arr_point[i], i);
  85.         }
  86.     }
  87.     void insert(int64_t value, int i)
  88.     {
  89.         heap_pointer[i] = value;
  90.         bubble_up(i);
  91.     }
  92.     int bubble_up(int index)
  93.     {
  94.         int parent_index = (index - 1) / d;
  95.         if (index == 0 || heap_pointer[parent_index] >= heap_pointer[index])
  96.         {
  97.             return index;
  98.         }
  99.         else
  100.         {
  101.             swap(heap_pointer[parent_index], heap_pointer[index]);
  102.             return bubble_up(parent_index);
  103.         }
  104.     }
  105.     void printHeap() {
  106.         for (int i = 0; i < heap_size; ++i)
  107.         {
  108.             cout << heap_pointer[i] << " ";
  109.         }
  110.         cout << std::endl;
  111.     }
  112.     int getMaxElement()
  113.     {
  114.         return heap_pointer[0];
  115.     }
  116.     void Make_3_Heap(int64_t* arr, int n, int i)
  117.     {
  118.         int root = i;
  119.  
  120.         int l = 2 * i + 1;
  121.         int m = 2 * i + 2;
  122.         int r = 2 * i + 3;
  123.  
  124.         if (l < n && arr[l] > arr[root])
  125.             root = l;
  126.         if (m < n && arr[m] > arr[root])
  127.             root = m;
  128.         if (r < n && arr[r] > arr[root])
  129.             root = r;
  130.  
  131.         if (root != i)
  132.         {
  133.             swap(arr[i], arr[root]);
  134.             Make_3_Heap(arr, n, root);
  135.         }
  136.     }
  137.     void sort()
  138.     {
  139.         for (int i = heap_size / 2 - 1; i >= 0; i--)
  140.             Make_3_Heap(heap_pointer, heap_size, i);
  141.  
  142.         for (int i = heap_size - 1; i >= 0; i--)
  143.         {
  144.             swap(heap_pointer[0], heap_pointer[i]);
  145.  
  146.             Make_3_Heap(heap_pointer, i, 0);
  147.         }
  148.     }
  149. };
  150.  
  151. void MakePseudoRandomSorted_3_Heap_Array(int size, ofstream& _file)
  152. {
  153.     srand(time(NULL));
  154.  
  155.     DHeap heap_rand_sort;
  156.  
  157.     int64_t* rand_arr;
  158.     rand_arr = new int64_t[size];
  159.  
  160.     cout << "\n";
  161.  
  162.     cout << "Making unsorted random array of " << size << " elements" << "\n";
  163.  
  164.     for (int i = 0; i < size; i++)
  165.     {
  166.         rand_arr[i] = rand() % 1'000'000'000;
  167.    }
  168.  
  169.    heap_rand_sort.heapify(rand_arr, size);
  170.    cout << "Made an array" << "\n";
  171.  
  172.    auto start = high_resolution_clock::now();
  173.  
  174.    heap_rand_sort.sort();
  175.    cout << "Array sorted";
  176.  
  177.    auto stop = high_resolution_clock::now();
  178.  
  179.    auto duration = duration_cast<milliseconds>(stop - start);
  180.    _file << "Time taken to sort an array of " << size << " elements: " << duration.count() << " milliseconds" << "\n";
  181.  
  182.    cout << "\n";
  183. }
  184.  
  185. void MakeSortedIncreasingArray_3_Heap(int size, ofstream& _file)
  186. {
  187.    DHeap heap_inc_sort;
  188.  
  189.    int64_t* arr;
  190.    arr = new int64_t[size];
  191.  
  192.    cout << "\n";
  193.    cout << "Making increasing array of " << size << " elements" << "\n";
  194.  
  195.    for (int i = 0; i < size; i++)
  196.    {
  197.        arr[i] = i;
  198.    }
  199.  
  200.    heap_inc_sort.heapify(arr, size);
  201.    cout << "Made an array" << "\n";
  202.  
  203.    auto start = high_resolution_clock::now();
  204.  
  205.    heap_inc_sort.sort();
  206.    cout << "Array sorted";
  207.  
  208.    auto stop = high_resolution_clock::now();
  209.  
  210.    auto duration = duration_cast<milliseconds>(stop - start);
  211.    _file << "Time taken to sort an array of " << size << " elements: " << duration.count() << " milliseconds" << "\n";
  212.  
  213.    cout << "\n";
  214. }
  215.  
  216. void MakeSortedDecreasingArray_3_Heap(int size, ofstream& _file)
  217. {
  218.    DHeap heap_inc_sort;
  219.  
  220.    int64_t* arr;
  221.    arr = new int64_t[size];
  222.  
  223.    cout << "\n";
  224.    cout << "Making increasing array of " << size << " elements" << "\n";
  225.  
  226.    for (int i = 0; i < size; i++)
  227.    {
  228.        arr[i] = size - i;
  229.    }
  230.  
  231.    heap_inc_sort.heapify(arr, size);
  232.    cout << "Made an array" << "\n";
  233.  
  234.    auto start = high_resolution_clock::now();
  235.  
  236.    heap_inc_sort.sort();
  237.    cout << "Array sorted";
  238.  
  239.    auto stop = high_resolution_clock::now();
  240.  
  241.    auto duration = duration_cast<milliseconds>(stop - start);
  242.    _file << "Time taken to sort an array of " << size << " elements: " << duration.count() << " milliseconds" << "\n";
  243.  
  244.    cout << "\n";
  245. }
  246.  
  247. void MakePseudoRandomSortedQuickArray(int size, ofstream& _file)
  248. {
  249.    srand(time(NULL));
  250.  
  251.    int64_t* arr;
  252.    arr = new int64_t[size];
  253.  
  254.    cout << "\n";
  255.    cout << "Making unsorted random array of " << size << " elements" << "\n";
  256.  
  257.    for (int i = 0; i < size; i++)
  258.    {
  259.        arr[i] = rand() % 1'000'000'000;
  260.     }
  261.     cout << "Made an array" << "\n";
  262.  
  263.     auto start = high_resolution_clock::now();
  264.  
  265.     QuickSort(arr, 0, size - 1);
  266.     cout << "Array sorted";
  267.  
  268.     auto stop = high_resolution_clock::now();
  269.  
  270.     auto duration = duration_cast<milliseconds>(stop - start);
  271.     _file << "Time taken to sort an array of " << size << " elements: " << duration.count() << " milliseconds" << "\n";
  272.  
  273.     cout << "\n";
  274. }
  275.  
  276. void MakeSortedIncreasingQuickArray(int size, ofstream& _file)
  277. {
  278.     int64_t* arr;
  279.     arr = new int64_t[size];
  280.  
  281.     cout << "\n";
  282.     cout << "Making increasing array of " << size << " elements" << "\n";
  283.  
  284.     for (int i = 0; i < size; i++)
  285.     {
  286.         arr[i] = i;
  287.     }
  288.  
  289.     cout << "Made an array" << "\n";
  290.  
  291.     auto start = high_resolution_clock::now();
  292.  
  293.     QuickSort(arr, 0, size - 1);
  294.     cout << "Array sorted";
  295.  
  296.     auto stop = high_resolution_clock::now();
  297.  
  298.     auto duration = duration_cast<milliseconds>(stop - start);
  299.     _file << "Time taken to sort an array of " << size << " elements: " << duration.count() << " milliseconds" << "\n";
  300.  
  301.     cout << "\n";
  302. }
  303.  
  304. void MakeSortedDecreasingQuickArray(int size, ofstream& _file)
  305. {
  306.     int64_t* arr;
  307.     arr = new int64_t[size];
  308.  
  309.     cout << "\n";
  310.     cout << "Making decreasing array of " << size << " elements" << "\n";
  311.  
  312.     for (int i = 0; i < size; i++)
  313.     {
  314.         arr[i] = size - i;
  315.     }
  316.  
  317.     cout << "Made an array" << "\n";
  318.  
  319.     auto start = high_resolution_clock::now();
  320.  
  321.     QuickSort(arr, 0, size - 1);
  322.     cout << "Array sorted";
  323.  
  324.     auto stop = high_resolution_clock::now();
  325.  
  326.     auto duration = duration_cast<milliseconds>(stop - start);
  327.     _file << "Time taken to sort an array of " << size << " elements: " << duration.count() << " milliseconds" << "\n";
  328.  
  329.     cout << "\n";
  330. }
  331.  
  332. void SortTestFirstCase(ofstream& file)
  333. {
  334.     cout << "\n";
  335.  
  336.     //-----------------------------3-Heap Sorts-----------------------------//
  337.     file << "First Case: " << "\n\n\n";
  338.     file << "Time measurments for 3-heap sorting a pseudo random array with numbers which have a maximum value of 10^9" << "\n";
  339.     for (int64_t i = 1; i <= 1'000'001; i += 10'000)
  340.    {
  341.        MakePseudoRandomSorted_3_Heap_Array(i, file);
  342.    }
  343.    file << "\n\n";
  344.    file << "Time measurments for 3-heap sorting an increasing array with numbers which have a maximum value of 10^9" << "\n";
  345.    for (int64_t i = 1; i <= 1'000'001; i += 10'000)
  346.     {
  347.         MakeSortedIncreasingArray_3_Heap(i, file);
  348.     }
  349.     file << "\n\n";
  350.     file << "Time measurments for 3-heap sorting a decreasing array with numbers which have a maximum value of 10^9" << "\n";
  351.     for (int64_t i = 1; i <= 1'000'001; i += 10'000)
  352.    {
  353.        MakeSortedDecreasingArray_3_Heap(i, file);
  354.    }
  355.  
  356.    //-------------------------Quick Sorts-------------------------//
  357.  
  358.    file << "\n\n";
  359.    file << "Time measurments for quick sorting a pseudo random array with numbers which have a maximum value of 10^9" << "\n";
  360.    for (int64_t i = 1; i <= 1'000'001; i += 10'000)
  361.     {
  362.         MakePseudoRandomSortedQuickArray(i, file);
  363.     }
  364.     file << "\n\n";
  365.     file << "Time measurments for quick sorting an increasing array with numbers which have a maximum value of 10^9" << "\n";
  366.     for (int64_t i = 1; i <= 1'000'001; i += 10'000)
  367.    {
  368.        MakeSortedIncreasingQuickArray(i, file);
  369.    }
  370.    file << "\n\n";
  371.    file << "Time measurments for quick sorting a decreasing array with numbers which have a maximum value of 10^9" << "\n";
  372.    for (int64_t i = 1; i <= 1'000'001; i += 10'000)
  373.     {
  374.         MakeSortedDecreasingQuickArray(i, file);
  375.     }
  376. }
  377.  
  378. class DHeap
  379. {
  380. private:
  381.     int d = 3;
  382.     int64_t* heap_pointer;
  383.     int64_t heap_size;
  384. public:
  385.     void heapify(int64_t* arr_point, int64_t arr_size)
  386.     {
  387.         heap_size = arr_size;
  388.         heap_pointer = new int64_t[heap_size];
  389.         for (int i = 0; i < heap_size; i++)
  390.         {
  391.             heap_pointer[i] = 0;
  392.         }
  393.         for (int i = 0; i < heap_size; i++)
  394.         {
  395.             insert(arr_point[i], i);
  396.         }
  397.     }
  398.     void insert(int64_t value, int i)
  399.     {
  400.         heap_pointer[i] = value;
  401.         bubble_up(i);
  402.     }
  403.     int bubble_up(int index)
  404.     {
  405.         int parent_index = (index - 1) / d;
  406.         if (index == 0 || heap_pointer[parent_index] >= heap_pointer[index])
  407.         {
  408.             return index;
  409.         }
  410.         else
  411.         {
  412.             swap(heap_pointer[parent_index], heap_pointer[index]);
  413.             return bubble_up(parent_index);
  414.         }
  415.     }
  416.     void printHeap() {
  417.         for (int i = 0; i < heap_size; ++i)
  418.         {
  419.             cout << heap_pointer[i] << " ";
  420.         }
  421.         cout << std::endl;
  422.     }
  423.     int getMaxElement()
  424.     {
  425.         return heap_pointer[0];
  426.     }
  427.     void Make_3_Heap(int64_t* arr, int n, int i)
  428.     {
  429.         int root = i;
  430.  
  431.         int l = 2 * i + 1;
  432.         int m = 2 * i + 2;
  433.         int r = 2 * i + 3;
  434.  
  435.         if (l < n && arr[l] > arr[root])
  436.             root = l;
  437.         if (m < n && arr[m] > arr[root])
  438.             root = m;
  439.         if (r < n && arr[r] > arr[root])
  440.             root = r;
  441.  
  442.         if (root != i)
  443.         {
  444.             swap(arr[i], arr[root]);
  445.             Make_3_Heap(arr, n, root);
  446.         }
  447.     }
  448.     void sort()
  449.     {
  450.         for (int i = heap_size / 2 - 1; i >= 0; i--)
  451.             Make_3_Heap(heap_pointer, heap_size, i);
  452.  
  453.         for (int i = heap_size - 1; i >= 0; i--)
  454.         {
  455.             swap(heap_pointer[0], heap_pointer[i]);
  456.  
  457.             Make_3_Heap(heap_pointer, i, 0);
  458.         }
  459.     }
  460. };
Advertisement
Add Comment
Please, Sign In to add comment