Algorithm 长度为2的集合上的快速排序

Algorithm 长度为2的集合上的快速排序,algorithm,sorting,quicksort,Algorithm,Sorting,Quicksort,当对数据集进行快速排序时,列表会被拆分,并且是递归的,因为解决方案会在较小的列表上调用自己。 我在一个算法上练习快速排序,但是一个长度为2的子列表是我眼中的一块石头,我无法解决它。原来的名单是: 2 0 1 7 4 3 5 6 轴在2,左在0,右在6,我开始。向左移动到7,7>=2。右移到1,1您案例中的问题来自于我没有在其排序位置移动枢轴这一事实。使用pivot2进行分区后,您的数组应该如下所示: 0 1 2 7 4 3 5 6 ^ A is arr

当对数据集进行快速排序时,列表会被拆分,并且是递归的,因为解决方案会在较小的列表上调用自己。 我在一个算法上练习快速排序,但是一个长度为2的子列表是我眼中的一块石头,我无法解决它。原来的名单是:

2  0  1  7  4  3  5  6

轴在2,左在0,右在6,我开始。向左移动到7,7>=2。右移到1,1您案例中的问题来自于我没有在其排序位置移动枢轴这一事实。使用pivot
2
进行分区后,您的数组应该如下所示:

0  1  2  7  4  3  5  6
      ^
A is array left..right
pivot = A[right]
i = left - 1                 // the one before the first bigger than the pivot
for j = left to right - 1
    if A[j] <= pivot
        i = i + 1
        swap A[i] with A[j]
swap A[i+1] with A[right]    // put pivot at its place, i + 1 - is the index to split on
让我们用输入数组
13 19 9 5 12 8 7 4 21 2 6 11
完成
partition
过程。让我们选择
11
作为支点

在此过程中,您需要维护两个指针,一个指针用于第一个大于轴的元素前面的元素,另一个指针用于当前的元素

代码如下所示:

0  1  2  7  4  3  5  6
      ^
A is array left..right
pivot = A[right]
i = left - 1                 // the one before the first bigger than the pivot
for j = left to right - 1
    if A[j] <= pivot
        i = i + 1
        swap A[i] with A[j]
swap A[i+1] with A[right]    // put pivot at its place, i + 1 - is the index to split on
数组是左..右
枢轴=A[右]
i=左-1//第一个轴之前的轴比枢轴大
对于j=从左到右-1
如果A[j]11,则跳过
^^  ||
13 19 9 5 12 8 7 4 21 2 6 11 19>11,跳过
^^      ||
9 19 13 5 12 8 7 4 21 2 6 11 9<11,互换
^^      ||
9511391287421261115<11,互换
^^      ||
9 5 13 19 12 8 7 4 21 2 6 11 12>11,跳过
^^          ||
958191313742162118<11,互换
^^          ||
9587112319421262117<11,互换
^^          ||
958744113121626114<11,互换
^^          ||
95874191221226121>11,跳过
^^              ||

你能自己继续吗?

你的问题来自于我没有将枢轴移动到其排序的位置。使用pivot
2
进行分区后,您的数组应该如下所示:

0  1  2  7  4  3  5  6
      ^
A is array left..right
pivot = A[right]
i = left - 1                 // the one before the first bigger than the pivot
for j = left to right - 1
    if A[j] <= pivot
        i = i + 1
        swap A[i] with A[j]
swap A[i+1] with A[right]    // put pivot at its place, i + 1 - is the index to split on
让我们用输入数组
13 19 9 5 12 8 7 4 21 2 6 11
完成
partition
过程。让我们选择
11
作为支点

在此过程中,您需要维护两个指针,一个指针用于第一个大于轴的元素前面的元素,另一个指针用于当前的元素

代码如下所示:

0  1  2  7  4  3  5  6
      ^
A is array left..right
pivot = A[right]
i = left - 1                 // the one before the first bigger than the pivot
for j = left to right - 1
    if A[j] <= pivot
        i = i + 1
        swap A[i] with A[j]
swap A[i+1] with A[right]    // put pivot at its place, i + 1 - is the index to split on
数组是左..右
枢轴=A[右]
i=左-1//第一个轴之前的轴比枢轴大
对于j=从左到右-1
如果A[j]11,则跳过
^^  ||
13 19 9 5 12 8 7 4 21 2 6 11 19>11,跳过
^^      ||
9 19 13 5 12 8 7 4 21 2 6 11 9<11,互换
^^      ||
9511391287421261115<11,互换
^^      ||
9 5 13 19 12 8 7 4 21 2 6 11 12>11,跳过
^^          ||
958191313742162118<11,互换
^^          ||
9587112319421262117<11,互换
^^          ||
958744113121626114<11,互换
^^          ||
95874191221226121>11,跳过
^^              ||

你能自己继续吗?

快速排序算法只有空数组或大小为1的数组的基本情况。在[2 0]的情况下,算法选择2作为轴心,将[2 0]划分为空数组和数组[0],并将其与轴心[2]合并,得到排序数组[0 2]。

快速排序算法只有空数组或大小为1的数组的基本情况。在[2 0]的例子中,算法选择2作为轴,将[2 0]划分为空数组和数组[0],并将其与轴[2]合并,得到排序数组[0 2]。

对于小数组,使用前面提到的冒泡排序或插入排序,快速排序会创建更小的列表,当数组变得足够小时,您可以对其调用
insertionSort
。在长度为2的列表上,快速排序的分区部分将对列表进行排序。长度小于等于1的列表是您的基础case@Yola值得指出的是,对小数组使用冒泡排序或插入排序是一种优化—一个可工作的快速排序实现应该可以一直工作到1或0个元素,即使这可能比混合方法慢一点。对于小数组,使用前面提到的冒泡排序或插入排序,快速排序会创建更小的列表,当数组变得足够小时,可以在其上调用
insertionSort
。在长度为2的列表上,快速排序的分区部分会对列表进行排序。长度小于等于1的列表是您的基础case@Yola值得指出的是,对小数组使用冒泡排序或插入排序是一种优化——一个可工作的快速排序实现应该可以一直工作到1或0个元素,即使这可能比混合方法慢一点。