-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertion_sort.py
More file actions
35 lines (29 loc) 路 970 Bytes
/
Copy pathinsertion_sort.py
File metadata and controls
35 lines (29 loc) 路 970 Bytes
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
# Insertion sort, created by github @adnanhf
def insertionSort(list):
for i in range(len(list)):
x, j = list[i], i - 1
while j >= 0 and list[j] < x:
list[j + 1] = list[j]
j = j - 1
list[j + 1] = x
def sequentialSearch(list, item):
pos = 0
found = False
while pos < len(list) and not found:
if list[pos] == item:
found = True
else:
pos += 1
if found:
print('the number you are looking for is at %s' % (pos + 1))
else:
print('the number you are looking for is not found!')
if __name__ == '__main__':
thelist, n = [], int(input('Input the amount of list elements : '))
for i in range(n):
element = int(input('Input the %s elements : ' % (i + 1)))
thelist.append(element)
insertionSort(thelist)
find = int(input('Input the number you want to search: '))
print(thelist)
sequentialSearch(thelist, find)