예제 #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
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"
예제 #3
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"
예제 #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
예제 #5
0
# LIS (Longest Increasing Subsequence) Problem:

# Find the length of the longest subsequence of a given sequence:
# such that all elements of the subsequence are sorted in increasing order. 
# For example, length of LIS for { 10, 22, 9, 33, 21, 50, 41, 60, 80 } is 6 and the LIS is {10, 22, 33, 50, 60, 80}.

# This is a Dynamic Programming Problem

import random
from misc_functions import Stopwatch
s=Stopwatch()

def LIS(arr): # O(n^2) algorithm...an O(n.log(n)) solution also exists, using binary search.
	lis=[]
	maximum=0
	for i in range(0,len(arr)):  # for (i=0; i<len(arr); i++){
		lis.append(1)
	# print lis

	for i in range(0,len(arr)):
		for j in range(0,i):
			if arr[i]>arr[j] and lis[i]<lis[j]+1:
				lis[i]=lis[j]+1
	# print lis

	maximum=max(lis)
	return maximum
num=1000
num_range=1000
arr=[(i%num_range) for i in range(0,num)]
random.shuffle(arr)