-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort.py
More file actions
98 lines (75 loc) · 2.76 KB
/
Copy pathselectionsort.py
File metadata and controls
98 lines (75 loc) · 2.76 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
"""
Bri Miskovitz
CS I
Homework 5
SOLUTIONS TO QUESTIONS:
1. A list that's in order, i.e. {5, 10, 15, 20, 25, 30, 35, 40, 50}
2. Solving for time complexity of selectionSort:
As stated in the lecture, the function insertion_sort's best scenario has a time
complexity of O(N) and its worst scenario is O(N^2). The selectionSort function's
iterates N-1 x T_findMinFrom(N) times, so the time complexity can be found by
analyzing findMinFrom. The function findMinFrom uses a for loop to iterate from
the index in the iteration within the selectionSort function to the length of the
list. So the total number of iterations for findMinFrom function is
N + N - 1 + N - 2 +...+ 2 + 1 regardless of the list. Therefore, the time complexity
for selectionSort is always O(N^2), and it's worst than insertion_sort's function's
best scenario.
Short version of solution:
While the time complexity of insertion_sort for the list is O(N), the
time complexity of selectionSort, for the same list, is O(N^2). Therefore,
insertion_sort performs this test better than selectionSort.
"""
def selectionSort(list):
"""
selectionSort sorts a list of integers by
moving them from the sequence they originate in
into a new sequence, where the smallest numbers
are placed first, then the largest
:param list: a list of numbers
:return: the sorted list
"""
for mark in range(len(list) - 1):
swap(list, mark, findMinFrom(list, mark))
return list
def findMinFrom(lst, mark):
"""
findMinFrom finds the minimum value in list from an index (mark)
:param list: a list of numbers
:param mark: index in the list
:return: index of the smallest number
"""
index = mark
min = lst[mark]
for num in range(mark, len(lst)):
if min > lst[num]:
min = lst[num]
index = num
return index
def swap(list, i, j):
"""
swap swaps elements in a list
:param i: an index
:param j: another index
:param list: a list of numbers
:return: list with swapped elements
"""
temp = list[i]
list[i] = list[j]
list[j] = temp
return list
def main():
"""
main prompts a text file from user,
opens, reads, and converts file into list of numbers,
and outputs original list and sorted list,
where the function PerkSort sorts it
:return: original and sorted list
"""
textFile = input("Enter an text file: ")
list = []
for line in open(textFile/textFile):
n = int(line.strip())
list += [n]
print("initial list =", list)
print("sorted list by selection sort =", selectionSort(list))
main()