-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.py
More file actions
43 lines (29 loc) · 765 Bytes
/
insertion_sort.py
File metadata and controls
43 lines (29 loc) · 765 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
36
37
38
39
40
41
42
import time
start = time.time()
# Python program for implementation of Insertion Sort
# Function to do insertion sort
def insertionSort(arr):
count = 0
for i in range(1, len(arr)):
count = count + 1
key = arr[i]
j = i-1
while j >= 0 and key < arr[j]:
count = count + 1
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
print(count)
# Driver code to test above
fp = open("case") # Open file on read mode
arr = fp.read().split(" ") # Create a list containing all lines
fp.close()
list2 = []
for i in range(len(arr)):
t = int(arr[i])
list2.append(t)
insertionSort(list2)
for i in range(len(list2)):
print ("% d" % list2[i])
end = time.time()
print(end - start)