Quicksort Pivot

前端 未结 5 1733
鱼传尺愫
鱼传尺愫 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 07:09

    For the given array [6, 11, 4, 9, 8, 2, 5, 8, 13, 7]

    Calculate something like this:

    • Select first element which is 6
    • Select last element of list which is 7

    • Select mid element which is mid = (length%2==0) ? (length/2)-1 : (length-1)/2, which is 8

    • This makes an array and sort this it will be [6,7,8], now mid element in your array is mid.

提交回复
热议问题