algorithm - Cormen quick sort modify partition function -
i learning quick sort introduction algorithms.i got stuck on question 7.1-2. of chapter 7 quicksort-
"what value of q partition return when elements in array a[p…r] have same value? modify partition q=⌊(p+r)/2⌋ when > elements in array a[p…r] have same value."
the first part easy , answer r.but can't figure out second part asking.i mean reason setting pivot (p+r)/2.further can't understand solutions found on searching on google.
please me in understanding advantage of modification in case elements equal , if possible please provide algorithm so.
by setting pivot middle of p , r, divide array of size n 2 sub-problems of equal size n/2. if draw recursion tree following recurrence, see height o(lgn)
t(n) = 2t(n/2)+o(n)
imagine if position of pivot returned partition last element in array. recurrence run time
t(n) = t(n-1)+o(n)
do see why inefficient if recursion tree linked list? try drawing tree , adding costs @ each node in both cases.
also modifying partition method return (p+r)/2 easy.
hint: 1 easy way split <= in if condition of partition.
Comments
Post a Comment