-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.py
More file actions
123 lines (97 loc) · 3.74 KB
/
sorting.py
File metadata and controls
123 lines (97 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Recitation 10 - Sorting
# Don't forget you can ask the course staff or your recitation partner if you have any questions regarding this activity
from timeit import timeit
from random import randrange
# All of the coding for this assignment will be in these three functions
def selection_sort(numList):
"""Selection Sort: choose first, second, etc."""
size = len(numList)
for pos in range(size):
minpos = pos
for seek in range(pos,size): # find smallest in range
# -- YOUR CODE STARTS HERE
if (numList[seek] <= numList[minpos]):
minpos = seek
# -- YOUR CODE ENDS HERE
numList[minpos],numList[pos] = numList[pos],numList[minpos]
def insertion_sort(numList):
"""Insertion Sort: insert each new element into sorted array"""
size = len(numList)
for pos in range(1,size):
value = numList[pos] # insert this value into sorted data
# -- YOUR CODE STARTS HERE
#When inserting the value, repeatedly swap the value from its current position
#to its correct position, thus performing an insertion
for x in range(pos-1, -1, -1):
if (numList[x] >= value):
numList[x], numList[x+1] = numList[x+1], numList[x]
pos -= 1
else:
break
# -- YOUR CODE ENDS HERE
numList[pos] = value
def bubble_sort(numList):
"""Bubble Sort: exchange any adjacent values that are out of order"""
size = len(numList)
sorted = False # expect to check at least once
while not sorted:
sorted = True # assume it is sorted this time
for pos in range(1,size):
if numList[pos] < numList[pos-1]:
# -- YOUR CODE STARTS HERE
numList[pos-1], numList[pos] = numList[pos], numList[pos-1]
sorted = False
# -- YOUR CODE ENDS HERE
# Functions to test the results quickly
def verify(numList):
"""Make sure the data is sorted"""
size = len(numList)
for pos in range(1,size):
if numList[pos] < numList[pos-1]:
print("Not sorted at positions", pos-1, "and", pos)
return False
return True
def test_sort():
"""Test sorts for ten element array"""
arr = [1,3,5,7,9,8,6,4,2,0]
selection_sort(arr)
print(f'arr after Selection sort: {arr}')
arr = [1,3,5,7,9,8,6,4,2,0]
insertion_sort(arr)
print(f'arr after Insertion sort: {arr}')
arr = [1,3,5,7,9,8,6,4,2,0]
bubble_sort(arr)
print(f'arr after Bubble sort: {arr}')
def time_test(sort,size=0):
"""Time these sorts for a given array size"""
arr = [0]*size
for i in range(size):
arr[i] = randrange(1000000000)
time1 = timeit('sort(arr)', globals = locals(), number=1)
time2 = timeit('sort(arr)', globals = locals(), number=1)
print("{:7.3f} s".format(time1),"{:7.3f} s".format(time2))
verify(arr)
test_sort()
print(f'Selection Sort')
time_test(selection_sort, 500)
time_test(selection_sort, 1000)
print(f'Insertion Sort')
time_test(insertion_sort, 500)
time_test(insertion_sort, 1000)
print(f'Bubble Sort')
time_test(bubble_sort, 500)
time_test(bubble_sort, 1000)
# This is a bad approximation of the bubble sort, for demonstration only
def bad_sort(numList):
size = len(numList)
i = 1
while i < size:
if numList[i] < numList[i-1]:
numList[i],numList[i-1] = numList[i-1],numList[i]
i = 1
else:
i = i+1
print(f'Bad Sort: ')
time_test(bad_sort,50)
time_test(bad_sort,100)
time_test(bad_sort,200)