2017-02-17 95 views
-2

我需要幫助這個代碼寫入僞編寫代碼爲僞

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; 

}}

回答

0
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)