int partition(int arr[], int start, int end){
        int pivot = arr[start];
        int count = 0;
        for (int i = start + 1; i <= end; i++) {
          if (arr[i] <= pivot)
            count++;
        }
        int pivotIndex = start + count;
        swap(arr[pivotIndex], arr[start]);
        int i = start, j = end;
        while (i < pivotIndex && j > pivotIndex) {
          while (arr[i] <= pivot) {
            i++;
          }
          while (arr[j] > pivot) {
            j--;
          }
          if (i < pivotIndex && j > pivotIndex) {
            swap(arr[i++], arr[j--]);
          }
        }
        return pivotIndex;
      }
      
      void quickSort(int arr[], int start, int end){
        if (start >= end)
          return;
        int p = partition(arr, start, end);
        quickSort(arr, start, p - 1);
        quickSort(arr, p + 1, end);
      }

      Cpp language logo
      Quick Sort

      Cpp recipes

      Sorts array using quick sort algorithm

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.