import mergesort.merge_sort
import quicksort.quicksort
import sys
import time
from random import randint

def multi_size(max_len):

	for n in [2**(n+1) for n in range(max_len)]:
		print 'Array size: %d' % n
		arr = [randint(0, 2**max_len) for n in range(n)]
		
		current_time = time.time()
		quicksort.quicksort.check(mergesort.merge_sort.sort(arr), n+1)
		print 'Merge sort: %f' % (time.time() - current_time)

		current_time = time.time()
		quicksort.quicksort.check(quicksort.quicksort.sort(arr, 0, n+1), n+1)
		print 'Quicksort: %f' % (time.time() - current_time)
		print '-----------------'

def fixed_time(sec, length):

	count = 0
	start = time.time()
	end = start + sec

	while time.time() < end:
		arr = [randint(0, length) for n in range(length)]
		mergesort.merge_sort.sort(arr)
		count += 1

	print 'Merge sort: %d %d-element arrays in %d seconds' % (count, length, sec)


	count = 0
	start = time.time()
	end = start + sec

	while time.time() < end:
		arr = [randint(0, length) for n in range(length)]
		quicksort.quicksort.sort(arr, 0, length)
		count += 1

	print 'Quicksort: %d %d-element arrays in %d seconds' % (count, length, sec)


if __name__ == '__main__':

	if len(sys.argv) > 2:
		fixed_time(int(sys.argv[1]), int(sys.argv[2]))
	else:
		multi_size(int(sys.argv[1]))
import mergesort.merge_sort
import quicksort.quicksort
import sys
import time
from random import randint

def main(max_len):

	for n in [2**(n+1) for n in range(max_len)]:
		print 'Array size: %d' % n
		arr = [randint(0, 2**max_len) for n in range(n)]
		
		current_time = time.time()
		quicksort.quicksort.check(mergesort.merge_sort.sort(arr))
		print 'Merge sort: %f' % (time.time() - current_time)

		current_time = time.time()
		quicksort.quicksort.check(quicksort.quicksort.sort(arr, 0, len(arr)))
		print 'Quicksort: %f' % (time.time() - current_time)
		print '-----------------'

if __name__ == '__main__':

	try:
		max_len = int(sys.argv[1])
	except (IndexError, ValueError):
		print 'Format: python sorting_test.py <log(max input)>'

	main(max_len)
--- 
+++ 
@@ -4,26 +4,50 @@
 import time
 from random import randint
 
-def main(max_len):
+def multi_size(max_len):
 
 	for n in [2**(n+1) for n in range(max_len)]:
 		print 'Array size: %d' % n
 		arr = [randint(0, 2**max_len) for n in range(n)]
 		
 		current_time = time.time()
-		quicksort.quicksort.check(mergesort.merge_sort.sort(arr))
+		quicksort.quicksort.check(mergesort.merge_sort.sort(arr), n+1)
 		print 'Merge sort: %f' % (time.time() - current_time)
 
 		current_time = time.time()
-		quicksort.quicksort.check(quicksort.quicksort.sort(arr, 0, len(arr)))
+		quicksort.quicksort.check(quicksort.quicksort.sort(arr, 0, n+1), n+1)
 		print 'Quicksort: %f' % (time.time() - current_time)
 		print '-----------------'
 
+def fixed_time(sec, length):
+
+	count = 0
+	start = time.time()
+	end = start + sec
+
+	while time.time() < end:
+		arr = [randint(0, length) for n in range(length)]
+		mergesort.merge_sort.sort(arr)
+		count += 1
+
+	print 'Merge sort: %d %d-element arrays in %d seconds' % (count, length, sec)
+
+
+	count = 0
+	start = time.time()
+	end = start + sec
+
+	while time.time() < end:
+		arr = [randint(0, length) for n in range(length)]
+		quicksort.quicksort.sort(arr, 0, length)
+		count += 1
+
+	print 'Quicksort: %d %d-element arrays in %d seconds' % (count, length, sec)
+
+
 if __name__ == '__main__':
 
-	try:
-		max_len = int(sys.argv[1])
-	except (IndexError, ValueError):
-		print 'Format: python sorting_test.py <log(max input)>'
-
-	main(max_len)
+	if len(sys.argv) > 2:
+		fixed_time(int(sys.argv[1]), int(sys.argv[2]))
+	else:
+		multi_size(int(sys.argv[1]))
Allow comparison within a fixed time period

To get an idea of average run-time, I wanted to be able to test
mergesort and quicksort with the same inputs many times over;
now by specifying a time limit and array length, the script will
run each algorithm on as many times as possible on random arrays
and report how many arrays were sorted within the time period.
