Siapakah yang paling cepat antara Open Address dengan Close Address :
Source Code :
from __future__ import print_function
import time
import random
waktu = time.time()
print ("Waktu sekarang :", waktu)
print("=".center(80,"="))
print("Tugas Praktikum 4".center(80))
print("=".center(80,"="))
print()
print("=".center(80,"="))
print("No.1 Linear Probbing".center(80))
print("=".center(80,"="))
table = [None]*110
def Linear(x):
return x % 11
def insert(table,keys,value):
index = Linear(keys)
if table[index]==None:
table[index]=value
else:
tumbukan=index
found = False
ind = tumbukan + 1
if ind >= len(table)-1:
ind = 0
while (ind<=len(table)-1) and not(found):
if table[ind]== None:
found = True
table[ind]=value
ind +=1
return table
keys = []
for i in range (0,100):
keys.append(random.randint(11,111))
value = list(keys)
for j in range(0,100):
linear = insert(table,keys[j],value[j])
print (linear)
waktu1 = time.time()
print ("Waktu sekarang :", waktu1)
print("=".center(80,"="))
print("Quadratic Probbing".center(80))
print("=".center(80,"="))
table1 = [None]*100
def Quadrat(x):
return x% 11
def insert1(table1,keys1,value1):
index = Quadrat(keys1)
if table1[index]==None:
table1[index]=value1
else:
tumbukan=index
found = False
ind = tumbukan + 1
if ind >= len(table1)-1:
ind = 0
i = 1
while (ind<=len(table1)-1) and not(found):
i+=1
if table1[ind]== None:
found = True
table1[ind]=value1
ind = (ind+(i**2))
return table1
keys1 = list(keys)
value1 = list(keys)
for j in range(0,100):
quadratic=insert1(table1,keys1[j],value1[j])
print (quadratic)
waktu2 = time.time()
print ("Waktu sekarang :", waktu2)
print("=".center(80,"="))
print("Chaining(Close Address)".center(80))
print("=".center(80,"="))
table2 = [[]]*20
def Chaining(x):
return x % 11
def insert2(table2,keys2,value2):
index = Chaining(keys2)
if table2[index]==[]:
table2[index]=[value2]
else:
table2[index].append(value2)
return table2
keys2 = list(keys)
value2 = list(keys)
for j in range(0,100):
chaining = insert2(table2,keys2[j],value2[j])
print (chaining)
waktu3 = time.time()
print ("Waktu sekarang :", waktu3)
print("=".center(80,"="))
print("No.2 Methode yang Paling Cepat".center(80))
print("=".center(80,"="))
if waktu2-waktu1 > waktu3-waktu2 < waktu1-waktu :
print ("Metode Chaining Yang Paling Cepat".upper().center(80))
elif waktu2-waktu1 == waktu3-waktu2:
print ("Methode Chaining dengan Quadratic sama-sama cepat".upper().center(80))
else:
print ("Methode Quadratic Paling Cepat".upper().center(80))
Silahkan Mencoba dan tentukan mana yang paling cepat...
Tidak ada komentar:
Posting Komentar