Quicksort Pivot

前端 未结 5 1746
鱼传尺愫
鱼传尺愫 2021-01-12 06:36

Sort the following array a using quicksort,

[6, 11, 4, 9, 8, 2, 5, 8, 13, 7]

The pivot should be chosen as the arithmetic mean of the first

5条回答
  •  醉话见心
    2021-01-12 06:51

    Your pivot is 6. Your pivot is NOT the 6th element Now you can apply the following algorith.

    function quicksort(array)
     var list less, greater
     if length(array) ≤ 1
         return array  // an array of zero or one elements is already sorted
     select and remove a pivot value pivot from array
     for each x in array
         if x ≤ pivot then append x to less
         else append x to greater
     return concatenate(quicksort(less), pivot, quicksort(greater))
    

提交回复
热议问题