我目前正在学习快速排序,并想知道如何选择第一个(或最后一个)元素作为支点。以第一个元素为快速排序的快速排序
例如说我有以下阵列:
{15, 19, 34, 41, 27, 13, 9, 11, 44}
这就是我的想法发生了:
{15, 19, 34, 41, 27, 13, 9, 11, 44}
^
pivot
{15, 19, 34, 41, 27, 13, 9, 11, 44}
^ ^
compare these two, they are good
{15, 19, 34, 41, 27, 13, 9, 11, 44}
^ ^
compare these two and swap
{11, 19, 34, 41, 27, 13, 9, 15, 44}
^ ^
compare these two and swap
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
End of first partition
这是它是如何工作的?如果是这样,那么19是新的枢轴点,还是你将数组分成两半来找到它(这样它会是27/13),还是取决于quicksort的实现?谢谢你的时间!
之后,你会继续上递归地应用快速排序的{13,9,11}和{27,44,19,34,41}的阵列的一部分。尝试用例如。一副扑克牌怎么会起作用! – phadej
这帮了很多,谢谢你清理我的困惑! –