Exemplo n.º 1
0
def sort(num, num_range=None, sort=None, print_arrays=False, timed=False, print_time=False):
	if num_range==None: 
		num_range=num

	if timed:
		s=Stopwatch()
		s.start()

	a=[(i%num_range) for i in range(0,num)]
	if timed:
		build_array_time=s.lap()
		if print_time:
			print " Time to build array of %s elements, range 0-%s: %s"%(num_to_words(num), num_to_words(num_range), float(build_array_time))

	random.shuffle(a)
	if timed:
		shuffle_time=s.lap()
		if print_time:
			print " Time to shuffle array: %s"%float(shuffle_time)

	if print_arrays:
		print "\n Unsorted:\n %s"%a

	if sort is not None:
		if sort.lower()=="mergesort":
			mergesort(a)
		if sort.lower()=="quicksort":
			quicksort(a)
		if sort.lower()=="mergesort_rec":
			mergesort_rec(a)
		if sort.lower()=="heapsort":
			heapsort(a)
	else:
		# something I made from test data...
		if float(num)/float(num_range) > 6 and num<=50000 :
			mergesort(a)
			sort="mergesort"
		elif float(num)/float(num_range) < 100 and num>=50000 and num<500000:
			quicksort(a)
			sort="quicksort"
		else: 
			mergesort(a)
			sort="mergesort"

	if timed:
		sort_time=s.lap()
		if print_time:
			print " Time to sort array: %s"%float(sort_time)
		s.stop()


	if print_arrays:
		print "\n Sorted:\n %s"%a

	# print "\n Sorted: %s"%is_sorted(a)
	# print "Used: %s"%sort
	if timed: 
		return sort_time
Exemplo n.º 2
0
def use_case(n):
	print "N=%s"%n
	s=Stopwatch()
	s.start()
	print Binom_coeff_tab(n, int(n/2))
	print str(s.lap()) +" seconds"

	print Binom_coeff_mem_driver(n, int(n/2))
	print str(s.lap()) +" seconds"

	print Binom_coeff_rec(n, int(n/2))
	print str(s.lap()) +" seconds"
	s.stop()
	print "\n\n"
def use_case(n):
    print "N=%s" % n
    s = Stopwatch()
    s.start()
    print Binom_coeff_tab(n, int(n / 2))
    print str(s.lap()) + " seconds"

    print Binom_coeff_mem_driver(n, int(n / 2))
    print str(s.lap()) + " seconds"

    print Binom_coeff_rec(n, int(n / 2))
    print str(s.lap()) + " seconds"
    s.stop()
    print "\n\n"
Exemplo n.º 4
0
def sort(num,
         num_range=None,
         sort=None,
         print_arrays=False,
         timed=False,
         print_time=False):
    if num_range == None:
        num_range = num

    if timed:
        s = Stopwatch()
        s.start()

    a = [(i % num_range) for i in range(0, num)]
    if timed:
        build_array_time = s.lap()
        if print_time:
            print " Time to build array of %s elements, range 0-%s: %s" % (
                num_to_words(num), num_to_words(num_range),
                float(build_array_time))

    random.shuffle(a)
    if timed:
        shuffle_time = s.lap()
        if print_time:
            print " Time to shuffle array: %s" % float(shuffle_time)

    if print_arrays:
        print "\n Unsorted:\n %s" % a

    if sort is not None:
        if sort.lower() == "mergesort":
            mergesort(a)
        if sort.lower() == "quicksort":
            quicksort(a)
        if sort.lower() == "mergesort_rec":
            mergesort_rec(a)
        if sort.lower() == "heapsort":
            heapsort(a)
    else:
        # something I made from test data...
        if float(num) / float(num_range) > 6 and num <= 50000:
            mergesort(a)
            sort = "mergesort"
        elif float(num) / float(
                num_range) < 100 and num >= 50000 and num < 500000:
            quicksort(a)
            sort = "quicksort"
        else:
            mergesort(a)
            sort = "mergesort"

    if timed:
        sort_time = s.lap()
        if print_time:
            print " Time to sort array: %s" % float(sort_time)
        s.stop()

    if print_arrays:
        print "\n Sorted:\n %s" % a

    # print "\n Sorted: %s"%is_sorted(a)
    # print "Used: %s"%sort
    if timed:
        return sort_time