Stooge sort是一种排序算法,如下所述:
Stooge-Sort(A,i,j)
if(A[i]>A[j]])
then exchange(A[i],A[j])
if i+1>=j
then return
k = [(j-i+1)/3]
Stooge-Sort(A,i,j-k)
Stooge-Sort(A,i+k,j)
Stooge-Sort(A,i,j-k)
算法的运行时间非常糟糕,我知道 问题:我想知道这个算法是如何工作的?
答案 0 :(得分:4)
您对子数组的第一个和最后一个元素进行排序。 (如果需要,交换)
然后你递归地排序子阵列的第一个2/3 rd ,最后2/3 rd 的子阵列再次首先2/3 rd 子阵列。
为证明正确性,您可以使用归纳法。
1. Clearly this algo works for 0, 1 and 2 element array.
2. Assuming it works for all arrays shorter than subarray
[i, j] let's prove it works for array[i,j] also. Let's
divide range[i,j] in 3 parts x, y and z;
a. After Stooge-Sort(A,i,j-k), or [xy], every element in range
y are larger than that of x. (Because range [xy] has
lesser elements than range[i,j])
b. After Stooge-Sort(A,i+k,j), or [yz], all largest elements
have moved to z and are sorted. So z is sorted and contains
largest elements of the array.
c. After Stooge-Sort(A,i,j-k), or [xy], first (2/3)rd part of
array is also sorted making the complete array sorted.
从步骤a,b和c,我们可以得出结论x,y和z的部分被排序,x的所有元素都比y大(或等于),并且z的所有元素都是(大于或等于)z和完整的数组已经排序。
答案 1 :(得分:2)
它使用分而治之的策略。
Set i = list first element index , j = list last element index
Set list item at position i = min(list value at position i, list value at position j)
Set list item at position j = max(list value at position i, list value at position j)
If (j - i) <= 1 return sorted list
Else Set t = (j - i + 1) / 3
Call sort with {i = i, j = j - t} then {i = i + t, j = j } then {i = i, j = j - t}
让我们说我要排序数组[1,4,5,3,-6,3,7,10,-2,-5]
这种方式会开始:
Before: [1,4,5,3,-6,3,7,10,-2,-5] (i=0 ;j=9)
Before: [-5,4,5,3,-6,3,7,10,-2,1] (i=0 ;j=6)
Before: [-5,4,5,3,-6,3,7,10,-2,1] (i=0 ;j=4)
Before: [-6,4,5,3,-5,3,7,10,-2,1] (i=0 ;j=3)
Before: [-6,4,5,3,-5,3,7,10,-2,1] (i=0 ;j=2)
Before: [-6,4,5,3,-5,3,7,10,-2,1] (i=0 ;j=1)
After: [-6,4,5,3,-5,3,7,10,-2,1] (i=0 ;j=1)
Before: [-6,4,5,3,-5,3,7,10,-2,1] (i=1 ;j=2)
After: [-6,4,5,3,-5,3,7,10,-2,1] (i=1 ;j=2)
Before: [-6,4,5,3,-5,3,7,10,-2,1] (i=0 ;j=1)
After: [-6,4,5,3,-5,3,7,10,-2,1] (i=0 ;j=1)
After: [-6,4,5,3,-5,3,7,10,-2,1] (i=0 ;j=2)
Before: [-6,4,5,3,-5,3,7,10,-2,1] (i=1 ;j=3)
Before: [-6,3,5,4,-5,3,7,10,-2,1] (i=1 ;j=2)
After: [-6,3,5,4,-5,3,7,10,-2,1] (i=1 ;j=2)
Before: [-6,3,5,4,-5,3,7,10,-2,1] (i=2 ;j=3)
After: [-6,3,4,5,-5,3,7,10,-2,1] (i=2 ;j=3)
Before: [-6,3,4,5,-5,3,7,10,-2,1] (i=1 ;j=2)
After: [-6,3,4,5,-5,3,7,10,-2,1] (i=1 ;j=2)
After: [-6,3,4,5,-5,3,7,10,-2,1] (i=1 ;j=3)
Before: [-6,3,4,5,-5,3,7,10,-2,1] (i=0 ;j=2)
Before: [-6,3,4,5,-5,3,7,10,-2,1] (i=0 ;j=1)
After: [-6,3,4,5,-5,3,7,10,-2,1] (i=0 ;j=1)
Before: [-6,3,4,5,-5,3,7,10,-2,1] (i=1 ;j=2)
After: [-6,3,4,5,-5,3,7,10,-2,1] (i=1 ;j=2)
Before: [-6,3,4,5,-5,3,7,10,-2,1] (i=0 ;j=1)
After: [-6,3,4,5,-5,3,7,10,-2,1] (i=0 ;j=1)
After: [-6,3,4,5,-5,3,7,10,-2,1] (i=0 ;j=2)
After: [-6,3,4,5,-5,3,7,10,-2,1] (i=0 ;j=3)
Before: [-6,3,4,5,-5,3,7,10,-2,1] (i=1 ;j=4)
Before: [-6,-5,4,5,3,3,7,10,-2,1] (i=1 ;j=3)
Before: [-6,-5,4,5,3,3,7,10,-2,1] (i=1 ;j=2)
After: [-6,-5,4,5,3,3,7,10,-2,1] (i=1 ;j=2)
Before: [-6,-5,4,5,3,3,7,10,-2,1] (i=2 ;j=3)
//etc