I am writing a simple sorting code by recursion, and I test it in Python 2.7.6
and Python 3.3.3
. But I obtained two different results. The code is following.
import math, copy, random, sys
call_count = 0; # Keep track of number of times of stooge_sort() get called
swap_count = 0; # Keep track of number of times swapping.
def stooge_sort(a, origin):
global call_count, swap_count;
call_count += 1;
n = len(a);
m = int(math.ceil(n*2/3));
if(n == 2 and a[0] > a[1]):
a[0], a[1] = a[1], a[0];
swap_count += 1;
elif (n > 2):
first = copy.deepcopy(a[0:m]);
a[0:m] = stooge_sort(first, origin);
second = copy.deepcopy(a[n-m:]);
a[n-m:] = stooge_sort(second, origin);
first = copy.deepcopy(a[0:m]);
a[0:m] = stooge_sort(first, origin);
return a;
a = [int(random.random() * 100) for i in range(10)];
stooge_sort(a, a);
print("sort function call count = " + str(call_count))
print("swap count = " + str(swap_count));
1) if I run in Python 2.7.6
, I would get incorrect sorting and
sort function call count = 40
swap count = 2
2) if I run in Python 3.3.3
, I get correct sorting and
sort function call count = 364
swap count = 18
So I wonder which part went wrong in Python 2.7.6
?