-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInsertionSort.py
More file actions
64 lines (45 loc) · 1.16 KB
/
InsertionSort.py
File metadata and controls
64 lines (45 loc) · 1.16 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
"""
File: SelectionSort.py
Original Author: Br. Burton, designed to be completed by others.
Sorts a list of numbers.
"""
def sort(numbers):
"""
Fill in this method to sort the list of numbers
"""
for index in range(1, len(numbers)):
currentValue = numbers[index]
position = index
while position > 0 and numbers[position - 1] > currentValue:
numbers[position] = numbers[position - 1]
position = position - 1
numbers[position] = currentValue
def prompt_for_numbers():
"""
Prompts the user for a list of numbers and returns it.
:return: The list of numbers.
"""
numbers = []
print("Enter a series of numbers, with -1 to quit")
num = 0
while num != -1:
num = int(input())
if num != -1:
numbers.append(num)
return numbers
def display(numbers):
"""
Displays the numbers in the list
"""
print("The list is:")
for num in numbers:
print(num)
def main():
"""
Tests the sorting process
"""
numbers = prompt_for_numbers()
sort(numbers)
display(numbers)
if __name__ == "__main__":
main()