The document discusses the worst case running time of quicksort on a list of 10 items when using the first item as the pivot. It notes that the worst case occurs when the split is unbalanced, such as when all items are the same value. To avoid this, it recommends choosing the median value as the pivot instead of the first item, which can improve the running time to O(n log n).
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Give an example of a list that contains 10 characters and generates wo.docx
1. Give an example of a list that contains 10 characters and generates worst case running time when
applying the quick sort algorithm (Assume that you are using the first item in a list as Pivot).
Also, write one approach to avoid such worst case running time for the given list.
Solution
Lets assume that there are 'n' elements in an array.
The Quick sort gives worst time complexity of O(n 2
).
The following are the cases which give worst case:
1. When the split of the list is unbalanced i.e ratio as 1: (n-1)
2. When all the elements are same in the array i.e 2,2,2,2,2,2.
The running time can be reduced to O(nlog n) if the median is selected as the pivot.