-
Notifications
You must be signed in to change notification settings - Fork 32
/
lec10prob6.py
68 lines (56 loc) · 2.05 KB
/
lec10prob6.py
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
# lec10prob6.py
# Lecture 10, problem 6
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
# Here is another version of a sorting function, mySort():
def mySort(L):
clear = False
while not clear:
clear = True
for j in range(1, len(L)):
if L[j-1] > L[j]:
clear = False
temp = L[j]
L[j] = L[j-1]
L[j-1] = temp
# Compare this to newSort():
def newSort(L):
for i in range(len(L) - 1):
j=i+1
while j < len(L):
if L[i] > L[j]:
temp = L[i]
L[i] = L[j]
L[j] = temp
j += 1
# Calling mySort with list
rawList = [1,9,2,8,3,7,4,6,5]
print("List before sorting: \n" + str(rawList))
mySort(rawList)
print("List after sorting: \n" + str(rawList))
# Calling newSort with list
# rawList = [1,9,2,8,3,7,4,6,5]
# print("List before sorting: \n" + str(rawList))
# newSort(rawList)
# print("List after sorting: \n" + str(rawList))
# Questions:
#
# 1. Do these functions result in the same sorted lists?
# Yes
# No
# 2. Do these two functions execute the same number of assignments of values
# into entries of the lists?
# Yes, they execute the same number of assignments.
# No. newSort may use more - but never fewer - inserts than mySort.
# No. mySort may use more - but never fewer - inserts than newSort.
# No. Either function may use more inserts than the other.
# 3. Is the worst-case order of growth of these functions the same?
# Yes. newSort and mySort have the same complexity.
# No. newSort has higher complexity than mySort.
# No. mySort has higher complexity than newSort.
# 4. Do these two functions examine the same number of entries in the list?
# Yes. newSort and mySort examine the same number of entries.
# No. newSort examines more entries than mySort.
# No. mySort examines more entries than newSort.
# No. mySort and newSort examine different numbers of entries, but one
# cannot always say which function will examine the most entries.