Data Structure (2130702)

BE | Semester-3   Winter-2016 | 01/02/2017

Q4) (b)

Write an algorithm for Quick sort.

Procedure pivot (T [i… j]; var l)
{Permutes the elements in array T [i… j] and returns a value l such that, at the end, i<=l<=j,
T[k] <=P for all i = k < l, T[l] =P, and T[k] > P for all l < k = j, where P is the initial value T[i]}
P ? T[i]
K ? i; l ? j+1
Repeat k ? k+1 until T[k] > P
Repeat l ? l-1 until T[l] = P
 While k < l do
  Swap T[k] and T[l]
Repeat k ? k+1 until T[k] > P
Repeat l ? l-1 until T[l] = P
 Swap T[i] and T[l]

Procedure quicksort (T [i… j]) {Sorts sub array T [i… j] into non decreasing order}
if j – i is sufficiently small then insert (T[i,…,j])
else
 pivot (T[i,…,j],l)
 quicksort (T[i,…, l - 1])
 quicksort (T[l+1,…,j]