In: Computer Science
What type of algorithm is the Quicksort algorithm if it has random pivots?
Lomuto's partition scheme or Hoare's partition scheme algorithm is use for the Quicksort algorithm if it has random pivots. In general, quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays.
Lomuto's Partition Scheme Algorithm using Random Pivots:
partition(arr[], lo, hi)
pivot = arr[hi]
i = lo
for j := lo to hi – 1 do
if arr[j] <= pivot then
swap arr[i] with arr[j]
i = i + 1
swap arr[i] with arr[hi]
return i
partition_r(arr[], lo, hi)
r = Random Number from lo to hi
Swap arr[r] and arr[hi]
return partition(arr, lo, hi)
quicksort(arr[], lo, hi)
if lo < hi
p = partition_r(arr, lo, hi)
quicksort(arr, lo , p-1)
quicksort(arr, p+1, hi)
Lomuto's partition scheme is easy to implement as compared to Hoare scheme.