You are on page 1of 6

COMP 3270: Assignment #2

Due on Friday, February 22, 2013

Dr. Carlisle 3:00pm

Steven Motes

Steven Motes

COMP 3270 (Dr. Carlisle 3:00pm): Assignment #2

Contents
Problem 1 3

Page 2 of 6

Steven Motes

COMP 3270 (Dr. Carlisle 3:00pm): Assignment #2

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

def counting_sort(array): # initialize counting array elements to 0 count = [0] * (max(array) + 1)


35

# 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

Problem 1 continued on next page. . .

Page 3 of 6

Steven Motes

COMP 3270 (Dr. Carlisle 3:00pm): Assignment #2

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

# n = 10 for i in range(0, 10): a.append(randint(0, max_val))


15

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 = []

Problem 1 continued on next page. . .

Page 4 of 6

Steven Motes

COMP 3270 (Dr. Carlisle 3:00pm): Assignment #2

Problem 1 (continued)

# n = 1000 for i in range(0, 1000): a.append(randint(0, max_val))


50

print N: %s % len(a) before = datetime.now() quicksort(a) runtime1 = datetime.now() - before


55

before = datetime.now() counting_sort(a) runtime2 = datetime.now() - before


60

print Quicksort: %s % runtime1 print Counting sort: %s\n % runtime2 a = []

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

print Quicksort: %s % runtime1 print Counting sort: %s\n % runtime2

Problem 1 continued on next page. . .

Page 5 of 6

Steven Motes

COMP 3270 (Dr. Carlisle 3:00pm): Assignment #2 Output

Problem 1 (continued)

N: 10 Quicksort: 0:00:00 Counting sort: 0:00:00.013000


5

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

You might also like