Advertisement
KingAesthetic

C++ Exmaple 3

Aug 12th, 2024 (edited)
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <chrono>
  5.  
  6. // Kaizer here. I implemented and compared different sorting algorithms using 3 main ones. quicksort, mergesort, and heapsort, in terms of performance.
  7. // function to print an array
  8. void printArray(const std::vector<int>& arr) {
  9.     for (int num : arr) {
  10.         std::cout << num << " ";
  11.     }
  12.     std::cout << std::endl;
  13. }
  14.  
  15. // quicksort implementation
  16. int partition(std::vector<int>& arr, int low, int high) {
  17.     int pivot = arr[high];
  18.     int i = low - 1;
  19.     for (int j = low; j < high; j++) {
  20.         if (arr[j] < pivot) {
  21.             i++;
  22.             std::swap(arr[i], arr[j]);
  23.         }
  24.     }
  25.     std::swap(arr[i + 1], arr[high]);
  26.     return i + 1;
  27. }
  28.  
  29. void quicksort(std::vector<int>& arr, int low, int high) {
  30.     if (low < high) {
  31.         int pi = partition(arr, low, high);
  32.         quicksort(arr, low, pi - 1);
  33.         quicksort(arr, pi + 1, high);
  34.     }
  35. }
  36.  
  37. // mergesort implementation
  38. void merge(std::vector<int>& arr, int left, int mid, int right) {
  39.     int n1 = mid - left + 1;
  40.     int n2 = right - mid;
  41.     std::vector<int> L(n1), R(n2);
  42.  
  43.     for (int i = 0; i < n1; i++)
  44.         L[i] = arr[left + i];
  45.     for (int j = 0; j < n2; j++)
  46.         R[j] = arr[mid + 1 + j];
  47.  
  48.     int i = 0, j = 0, k = left;
  49.     while (i < n1 && j < n2) {
  50.         if (L[i] <= R[j]) {
  51.             arr[k] = L[i];
  52.             i++;
  53.         } else {
  54.             arr[k] = R[j];
  55.             j++;
  56.         }
  57.         k++;
  58.     }
  59.  
  60.     while (i < n1) {
  61.         arr[k] = L[i];
  62.         i++;
  63.         k++;
  64.     }
  65.  
  66.     while (j < n2) {
  67.         arr[k] = R[j];
  68.         j++;
  69.         k++;
  70.     }
  71. }
  72.  
  73. void mergesort(std::vector<int>& arr, int left, int right) {
  74.     if (left < right) {
  75.         int mid = left + (right - left) / 2;
  76.         mergesort(arr, left, mid);
  77.         mergesort(arr, mid + 1, right);
  78.         merge(arr, left, mid, right);
  79.     }
  80. }
  81.  
  82. // heapsort implementation
  83. void heapify(std::vector<int>& arr, int n, int i) {
  84.     int largest = i;
  85.     int left = 2 * i + 1;
  86.     int right = 2 * i + 2;
  87.  
  88.     if (left < n && arr[left] > arr[largest])
  89.         largest = left;
  90.  
  91.     if (right < n && arr[right] > arr[largest])
  92.         largest = right;
  93.  
  94.     if (largest != i) {
  95.         std::swap(arr[i], arr[largest]);
  96.         heapify(arr, n, largest);
  97.     }
  98. }
  99.  
  100. void heapsort(std::vector<int>& arr) {
  101.     int n = arr.size();
  102.     for (int i = n / 2 - 1; i >= 0; i--)
  103.         heapify(arr, n, i);
  104.  
  105.     for (int i = n - 1; i > 0; i--) {
  106.         std::swap(arr[0], arr[i]);
  107.         heapify(arr, i, 0);
  108.     }
  109. }
  110.  
  111. // function to measure and compare sorting times
  112. void compareSorts(std::vector<int> arr) {
  113.     std::vector<int> arr1 = arr;
  114.     std::vector<int> arr2 = arr;
  115.     std::vector<int> arr3 = arr;
  116.  
  117.     auto start = std::chrono::high_resolution_clock::now();
  118.     quicksort(arr1, 0, arr1.size() - 1);
  119.     auto end = std::chrono::high_resolution_clock::now();
  120.     std::chrono::duration<double> quicksortTime = end - start;
  121.     std::cout << "Quicksort time: " << quicksortTime.count() << " seconds" << std::endl;
  122.  
  123.     start = std::chrono::high_resolution_clock::now();
  124.     mergesort(arr2, 0, arr2.size() - 1);
  125.     end = std::chrono::high_resolution_clock::now();
  126.     std::chrono::duration<double> mergesortTime = end - start;
  127.     std::cout << "Mergesort time: " << mergesortTime.count() << " seconds" << std::endl;
  128.  
  129.     start = std::chrono::high_resolution_clock::now();
  130.     heapsort(arr3);
  131.     end = std::chrono::high_resolution_clock::now();
  132.     std::chrono::duration<double> heapsortTime = end - start;
  133.     std::cout << "Heapsort time: " << heapsortTime.count() << " seconds" << std::endl;
  134. }
  135.  
  136. int main() {
  137.     std::vector<int> arr = {12, 11, 13, 5, 6, 7};
  138.     std::cout << "Original array: ";
  139.     printArray(arr);
  140.  
  141.     compareSorts(arr);
  142.  
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement