show the array after each swap when stoogesortA 0 3 is calle
show the array after each swap when stoogesort(A, 0, 3) is called where A is{6, 3, 2, 5}
L(A,i,j)
Solution
At i = 0, j = 3.
L = {6, 3, 2, 5}.
function stoogesort(L,i,j)
if L[j] < L[i] then L[0] is 6, and L[3] is 5. L[3] < L[0] is true. So, exchange which leads to L = {5, 3, 2, 6}.
L[i] L[j]
if (i + 1) >= j then i+1 is 1, and 1 >= 3 is false.
return
t = (j - i + 1) / 3 t = (3 - 0 + 1) / 3 = 4 / 3 = 1.
stoogesort(L, i , j-t) stoogesort(L, 0, 2)
stoogesort(L, i+t, j ) stoogesort(L, 1, 3)
stoogesort(L, i , j-t) stoogesort(L, 0, 2)
return L
And the sequence for various values of i, and j are:
Initally: 6 3 2 5
0 3: 6 3 2 5
0 2: 5 3 2 6
0 1: 2 3 5 6
1 2: 2 3 5 6
0 1: 2 3 5 6
1 3: 2 3 5 6
1 2: 2 3 5 6
2 3: 2 3 5 6
1 2: 2 3 5 6
0 2: 2 3 5 6
0 1: 2 3 5 6
1 2: 2 3 5 6
0 1: 2 3 5 6
Finally: 2 3 5 6
