Search code examples
arrayssortingquicksortswappseudocode

Write code into pseudocode


I need help to write this code into pseudocode

public void quickSort(int[] A, int p, int r) {
    if (p < r) {
        int q = partition(A, p, r);
        quickSort(A, p, q);
        quickSort(A, q + 1, r);
    }
}

private int partition(int[] A, int p, int r) {
    int x = A[p]; // pivot element x
    int i = p - 1;
    int j = r + 1;

    // partition
    while (true) {
        do {
            j--;
        } while (A[j] > x);
        do {
            i++;
        } while (A[i] < x);

        if (i < j)
            swap(A, i, j);
        else
            return j;
    }
}

    private void swap(int[] A, int i, int j) {
    int tmp = A[i];
    A[i] = A[j];
    A[j] = tmp;

} }


Solution

  • for each (unsorted) partition
      set first element as pivot
      storeIndex = pivotIndex + 1
      for i = pivotIndex + 1 to rightmostIndex
        if element[i] < element[pivot]
          swap(i, storeIndex); storeIndex++
      swap(pivot, storeIndex - 1)