Advertisement
YouKnowWho07

Recursion Quick Sort

Sep 7th, 2023 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int partition(int arr[], int low, int high)
  5. {
  6.     int pivot = arr[high];
  7.     int i = (low - 1);
  8.  
  9.     for(int j = low; j <= high - 1; j++)
  10.     {
  11.         if(arr[j] <= pivot)
  12.         {
  13.             i++;
  14.             int temp = arr[i];
  15.             arr[i] = arr[j];
  16.             arr[j] = temp;
  17.         }
  18.     }
  19.     int temp = arr[i + 1];
  20.     arr[i + 1] = arr[high];
  21.     arr[high] = temp;
  22.  
  23.     return (i + 1);
  24. }
  25.  
  26. void quickSort(int arr[], int low, int high)
  27. {
  28.     if(low < high)
  29.     {
  30.         int pivotIndex = partition(arr, low, high);
  31.         quickSort(arr, low, pivotIndex - 1);
  32.         quickSort(arr, pivotIndex + 1, high);
  33.     }
  34. }
  35.  
  36. int main()
  37. {
  38.     int arrSize;
  39.     cout << "Enter the size of the array: ";
  40.     cin >> arrSize;
  41.  
  42.     int arr[arrSize];
  43.     cout << "Enter elements of the array: ";
  44.     for (int i = 0; i < arrSize; ++i)
  45.     {
  46.         cin >> arr[i];
  47.     }
  48.  
  49.     quickSort(arr, 0, arrSize - 1);
  50.  
  51.     cout << "Sorted array: ";
  52.     for (int i = 0; i < arrSize; i++) {
  53.         std::cout << arr[i] << " ";
  54.     }
  55.     cout << endl;
  56.  
  57.     return 0;
  58. }
  59.  
  60.  
  61. /*
  62. #include <iostream>
  63.  
  64. // Function to partition the array into two segments and return the pivot index
  65. int partition(int arr[], int low, int high) {
  66.     int pivot = arr[high]; // Choose the last element as the pivot
  67.     int i = (low - 1); // Index of the smaller element
  68.  
  69.     for (int j = low; j <= high - 1; j++) {
  70.         // If the current element is smaller than or equal to the pivot
  71.         if (arr[j] <= pivot) {
  72.             i++;
  73.             // Swap arr[i] and arr[j]
  74.             int temp = arr[i];
  75.             arr[i] = arr[j];
  76.             arr[j] = temp;
  77.         }
  78.     }
  79.  
  80.     // Swap arr[i + 1] and arr[high] (pivot)
  81.     int temp = arr[i + 1];
  82.     arr[i + 1] = arr[high];
  83.     arr[high] = temp;
  84.  
  85.     return (i + 1);
  86. }
  87.  
  88. // Function to perform Quick Sort recursively
  89. void quickSort(int arr[], int low, int high) {
  90.     if (low < high) {
  91.         // Find pivot element such that
  92.         // element smaller than pivot are on the left and
  93.         // elements greater than pivot are on the right
  94.         int pivotIndex = partition(arr, low, high);
  95.  
  96.         // Recursively sort elements before and after pivot
  97.         quickSort(arr, low, pivotIndex - 1);
  98.         quickSort(arr, pivotIndex + 1, high);
  99.     }
  100. }
  101.  
  102. int main() {
  103.     int arrSize;
  104.     cout << "Enter the size of the array: ";
  105.     cin >> arrSize;
  106.  
  107.     int arr[arrSize];
  108.     cout << "Enter elements of the array: ";
  109.     for(int i = 0; i < arrSize; ++i) {
  110.         cin >> arr[i];
  111.     }
  112.  
  113.     quickSort(arr, 0, arrSize - 1);
  114.  
  115.     cout << "Sorted array: ";
  116.     for (int i = 0; i < arrSize; i++) {
  117.         cout << arr[i] << " ";
  118.     }
  119.     cout << std::endl;
  120.  
  121.     return 0;
  122. }
  123.  
  124. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement