Quicksort Pivot

前端 未结 5 1728
鱼传尺愫
鱼传尺愫 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:53

    By the way the question is worded, the pivot should just be 6 and not necessarily the 6th item in the array.

    This is most definitely the case because if there were only 3 items in the array, for example, and the arithmetic mean came out to be greater than 3, you would have no pivot to choose because there is no item with that index.

    Note: Be careful with the way you index elements in your array. You said the 6th element is '2', when it may be '5' if your programming language starts indices at 0.

提交回复
热议问题