Assignment 2
Assignment 2
Steven Motes
Steven Motes
Contents
Problem 1 3
Page 2 of 6
Steven Motes
Problem 1
This assignment involved two les, sorts.py and test.py. The sorts were dened within sorts.py and are shown in the gure below. Within the test script, test.py on page 4, the python datetime library was used to measure the runtime execution of the two sorts. Several random arrays of various sizes of N were generated and then sent into the sorting algorithms. The output of the script is shown on page 6. Notice that quicksort only beats out counting sort for low values of N (N=10, N=100, N=1000), but counting sort is faster for large values of N (N = 100000, N=10000000). sorts.py
#!/usr/bin/env python from math import ceil
5
def quicksort(array): # Base case: array of size 0 or 1 is already sorted i f len(array) <= 1: return array else: # Subarrays to be sorted for elements less than or greater than pivot left = [] right = []
10
15
# Calculate pivot index = int(ceil(len(array)/2.0)) pivot = array[index] # Remove pivot from array array = array[:index-1] + array[index:] # Put elements in left (less than or equal to pivot) or right (greater than pivot) for element in array: i f element <= pivot: left.append(element) else: right.append(element) return quicksort(left) + [pivot] + quicksort(right)
20
25
30
# generate counting array for element in array: count[element] = count[element] + 1 # Sorted array to be returned b = [] # Iterate through counting array and put elements in correct spot in sorted array for i in range(0, len(count)):
40
Page 3 of 6
Steven Motes
Problem 1 (continued)
45
while count[i] > 0: count[i] = count[i] - 1 b.append(i) # Finally, return the sorted array return b
test.py
#!/usr/bin/env python from random import randint from datetime import datetime from sorts import quicksort, counting_sort i f __name__ == __main__: a = [] max_val = 2**15
10
print N: %s % len(a) before = datetime.now() quicksort(a) runtime1 = datetime.now() - before before = datetime.now() counting_sort(a) runtime2 = datetime.now() - before print Quicksort: %s % runtime1 print Counting sort: %s\n % runtime2 a = [] # n = 100 for i in range(0, 100): a.append(randint(0, max_val)) print N: %s % len(a) before = datetime.now() quicksort(a) runtime1 = datetime.now() - before before = datetime.now() counting_sort(a) runtime2 = datetime.now() - before print Quicksort: %s % runtime1 print Counting sort: %s\n % runtime2
20
25
30
35
40
45
a = []
Page 4 of 6
Steven Motes
Problem 1 (continued)
65
# n = 100000 for i in range(0, 100000): a.append(randint(0, max_val)) print N: %s % len(a) before = datetime.now() quicksort(a) runtime1 = datetime.now() - before before = datetime.now() counting_sort(a) runtime2 = datetime.now() - before print Quicksort: %s % runtime1 print Counting sort: %s\n % runtime2
70
75
80
a = [] # n = 1000000 for i in range(0, 1000000): a.append(randint(0, max_val)) print N: %s % len(a) before = datetime.now() quicksort(a) runtime1 = datetime.now() - before before = datetime.now() counting_sort(a) runtime2 = datetime.now() - before
95
85
90
Page 5 of 6
Steven Motes
Problem 1 (continued)
N: 100 Quicksort: 0:00:00.002000 Counting sort: 0:00:00.048000 N: 1000 Quicksort: 0:00:00.012000 Counting sort: 0:00:00.013000 N: 100000 Quicksort: 0:00:02.356000 Counting sort: 0:00:00.135000 N: 1000000 Quicksort: 0:00:37.089000 Counting sort: 0:00:01.187000
10
15
Page 6 of 6