Algoritma Analizi'nde Python ile Bubble Sort ve İnsertion Sort'un sıralama yaparken diziyi kaç kere taramasının karşılaştırması
In [1]:
import random
def createRandom(n):
myarray=[]
for i in range(n):
myarray.append(random.randint(0,100))
return myarray
In [2]:
def myBubble_Sort(my_array):
number_of_comparision=0
for passnum in range(len(my_array)-1,0,-1):
for i in range(passnum):
number_of_comparision=number_of_comparision+1
if my_array[i]>my_array[i+1]:
temp = my_array[i]
my_array[i] = my_array[i+1]
my_array[i+1] = temp
return(number_of_comparision)
In [3]:
n=10
for i in range(3):
print(myBubble_Sort(createRandom(n)))
In [4]:
n=20
for i in range(3):
print(myBubble_Sort(createRandom(n)))
In [5]:
n=30
for i in range(3):
print(myBubble_Sort(createRandom(n)))
In [6]:
n=50
for i in range(3):
print(myBubble_Sort(createRandom(n)))
In [7]:
def insertionsort( aList ):
comparison=0
for i in range( 1, len( aList ) ):
tmp = aList[i]
k = i
comparison=comparison+1
while k > 0 and tmp < aList[k - 1]:
aList[k] = aList[k - 1]
k -= 1
comparison=comparison+1
aList[k] = tmp
return(comparison)
In [8]:
n=10
for i in range(3):
print(insertionsort(createRandom(n)))
In [9]:
n=20
for i in range(3):
print(insertionsort(createRandom(n)))
In [10]:
n=30
for i in range(3):
print(insertionsort(createRandom(n)))
In [11]:
n=50
for i in range(3):
print(insertionsort(createRandom(n)))
Yorumlar
Yorum Gönder