\
RandomizedQuickSort(A[],beg,end)
1. if (beg < end)
2. pivotIndex = partition(arr,beg, end)
3. RandomizedQuickSort(arr, beg, pivotIndex)
4. RandomizedQuickSort(arr, pivotIndex + 1, end)
Randomizedpartition(arr, beg, end)
1. set random element from the range beg-end
2. exchange it with beg element
3. set beg as pivot
4. small_no_count = 0
5.for i = beg to end
6. if arr[i] < pivot
7. small_no_count++
8.swap(beg,beg+small_no_count)
9.set i = beg & j = end
10.while i< j
11. if arr[i]<=pivot then i++
12. else if arr[j]>pivot then j--;
13. else swap(i,j)
14. return beg+small_no_count
|
|
Space | ||
| Best Case | Average Case | Worst Case |
|
|
|
|
|
|
What is the working mechanism of Randomized Quick
Sort
Randomized quick sort is designed to decrease the chances of
the algorithm being executed in the worst case time complexity of
O(n2). The worst case time complexity of quick sort arises when
the input given is an already sorted list, leading to n(n - 1)
comparisons.
Making the pivot element a random variable is commonly used method
in the randomized quick sort. Here, even if the input is sorted,
the pivot is chosen randomly so the worst case time complexity is avoided.
Is randomized Quick sort a stable,
in-place sorting algorithm or an
out-of-place sorting algorithm?
Randomized Quick sort is not a stable algorithm ,meaning that it won't be guaranteed
to preserve the order of elements as it reorders; two elements that are
exactly the same in the unsorted array could show up in a reversed order
in the sorted one! Stability ends up being what causes people to choose
merge sort over quicksort, and it's the one area where merge sort appears
as the obvious winner.
It needs memory in order of O(log n) this completely disqualifies quicksort as
an in-place algorithm. However, quicksort is always considered to be in-place,
it sorts the elements within the array with at most a constant amount of them
outside the array at any given time.
How does the number of elements impact the efficiency of Randomized Quick sort?
As the number of elements being sorted increases, the probability of selecting a good pivot element by chance also increases,
which improves the performance of randomized quicksort. However, if the input list is already sorted or nearly sorted,
its performance can degrade significantly, and other sorting algorithms like merge sort may be more suitable.
What are advantages and disadvantages of Randomized Quick Sort?
Advantages: