-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.py
More file actions
32 lines (29 loc) · 939 Bytes
/
insertion_sort.py
File metadata and controls
32 lines (29 loc) · 939 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
def insertionSort(alist):
karsilastirma=0
yerdegistirme=0
for index in range(0,len(alist)):
yerdegistirme=yerdegistirme+1
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
karsilastirma=karsilastirma+1
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
print("karsilastirma sayisi :",karsilastirma)
print("yerdegistirme sayisi :" ,yerdegistirme)
def createAnArray(size):
import random
array=[]
for i in range(0,size):
array.append(int(random.uniform(-1000,1000)))
return array
size=int(input("size ?"))
alist=createAnArray(size)
import time
t_start=time.time()
insertionSort(alist)
t_end=time.time()
for i in range(0,len(alist)):
print(i,".item",alist[i])
print("n kare",size*size,"time:",t_end-t_start)