-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.py
31 lines (28 loc) · 822 Bytes
/
4.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
'''
4.a
Write a python program to implement insertion sort and merge sort using lists
'''
import time
import random
comparison,swaps = 0,0
# Nearly Brute Force
def insertion_sort_old(unsorted_list):
global comparison
global swaps
for i in range(len(unsorted_list)):
j = i + 1
for j in range(len(unsorted_list)):
comparison+=1
if unsorted_list[i] < unsorted_list[j]:
swaps+= 1
temp = unsorted_list[i]
unsorted_list[i] = unsorted_list[j]
unsorted_list[j] = temp
print(comparison,"Comparisons and", swaps, "Swaps")
return
randomlist = random.sample(range(0, 50), 50)
data = randomlist.copy()
print("Unsorted list",data)
insertion_sort_old(data)
print('Sorted list in Ascending Order:')
print(data)