Esempio n. 1
0
kocok(k)
def u_bub(arr):
    n = len (arr)
    for i in range (n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1] :
                arr[j], arr[j+1] = arr[j+1], arr[j]
def u_sel(A):
    for i in range(len(A)):
        min_in = i
        for j in range(i+1, len (A)):
            if A[min_in] > A[j]:
                    min_in = j
        A[i], A[min_in] = A[min_in], A[i]
def u_ins(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i-1
        while j >= 0 and key < arr[j]:
                arr[j+1] = arr[j]
                j -= 1
        arr[j+1] = key

bub = k[:]
sel = k[:]
ins = k[:]

aw = detak(); u_bub(bub);ak = detak();print('bubble : %g detik' %(ak-aw));
aw1 = detak(); u_sel(sel);ak1 = detak();print('selection : %g detik' %(ak1-aw1));
aw2 = detak(); u_ins(ins);ak2 = detak();print('insertion : %g detik' %(ak1-aw1));
Esempio n. 2
0
    return ( i+1 ) 
  
def quickSort(arr,low,high): 
    if low < high: 
        pi = partition(arr,low,high) 
        quickSort(arr, low, pi-1) 
        quickSort(arr, pi+1, high)


bub = k[:]
sel = k[:]
ins = k[:]
mer = k[:]
qui = k[:]

aw=detak();bubb(bub);ak=detak();print('bubble : %g detik' %(ak-aw));
aw=detak();sele(sel);ak=detak();print('selection : %g detik' %(ak-aw));
aw=detak();inse(ins);ak=detak();print('insertion : %g detik' %(ak-aw));
aw=detak();mergeSort(mer);ak=detak();print('merge : %g detik' %(ak-aw));
aw=detak();quickSort(qui,0,len(qui)-1);ak=detak();print('quick : %g detik' %(ak-aw));

#Nomer 5
import random
def _merge_sort(indices, the_list):
    start = indices[0]
    end = indices[1]
    half_way = (end - start)//2 + start
    if start < half_way:
        _merge_sort((start, half_way), the_list)
    if half_way + 1 <= end and end - start != 1:
       _merge_sort((half_way + 1, end), the_list)
Esempio n. 3
0
            koleksi[j] = koleksi[j-1]
            koleksi[j-1] = temp
            j -=1
        i += 1
        
from time import time as detak
from random import shuffle as kocok
k = list(range(6000))
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]
u_mrg = k[:]
u_qck = k[:]

aw = detak();bubbleSort(u_bub);ak=detak();print('bubble: %g detik' %(ak-aw));
aw = detak();selectionSort(u_sel);ak=detak();print('selection: %g detik' %(ak-aw));
aw = detak();insertionSort(u_ins);ak=detak();print('insertion: %g detik' %(ak-aw));
aw = detak();mergeSort(u_mrg);ak=detak();print('merge: %g detik' %(ak-aw));
awal = 0
akhir = len(u_qck)-1
aw = detak();quickSort(u_qck,awal,akhir);ak=detak();print('quick: %g detik' %(ak-aw));

#No 5
import random
def _merge_sort(indices, the_list):
    start = indices[0]
    end = indices[1]
    half_way = (end - start)//2 + start
    if start < half_way:
        _merge_sort((start, half_way), the_list)
Esempio n. 4
0
def inse(arr):
    for i in range(1, len(arr)):

        key = arr[i]
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key


bub = k[:]
sel = k[:]
ins = k[:]

aw = detak();
bubb(bub);
ak = detak();
print('bubble : %g detik' % (ak - aw));
aw = detak();
sele(sel);
ak = detak();
print('selection : %g detik' % (ak - aw));
aw = detak();
inse(ins);
ak = detak();
print('insertion : %g detik' % (ak - aw));


Esempio n. 5
0
    L[low], L[i - 1] = L[i - 1], L[low] 
    return i - 1, result


def mot(L, low, high):
    mid = (low + high - 1) // 2
    a = L[low]
    b = L[mid]
    c = L[high - 1]
    if a <= b <= c:
        return b, mid
    if c <= b <= a:
        return b, mid
    if a <= c <= b:
        return c, high - 1
    if b <= c <= a:
        return c, high - 1
    return a, low

k = list(range(6000))
kocok(k)
u_mrg = k[:]
u_qck = k[:]
u_mrg_new = k[:]
u_qck_new = k[:]

aw = detak();mergeSort(u_mrg);ak = detak();print('Merge : %g detik' %(ak-aw));
aw = detak();quickSort(u_qck);ak = detak();print('Quick : %g detik' %(ak-aw));
aw = detak();mergeSort_new(u_mrg_new);ak = detak();print('Merge New : %g detik' %(ak-aw));
aw = detak();quickSort_new(u_qck_new);ak = detak();print('Quick New : %g detik' %(ak-aw));
Esempio n. 6
0

def cariPosisiYangTerkecil(A, darisini, sampaisini):
    posisiYangTerkecil = darisini
    for i in range(darisini + 1, sampaisini):
        if A[i] < A[posisiYangTerkecil]:
            posisiYangTerkecil = i
    return posisiYangTerkecil


k = []
for i in range(1, 6001):
    k.append(i)
kocok(k)
u_buble = k[:]
u_selection = k[:]
u_insertion = k[:]

awal = detak()
bubbleSort(u_buble)
akhir = detak()
print('bubble: %g detik' % (akhir - awal))
awal = detak()
selectionSort(u_selection)
akhir = detak()
print('selection: %g detik' % (akhir - awal))
awal = detak()
insertionSort(u_insertion)
akhir = detak()
print('insertion: %g detik' % (akhir - awal))
Esempio n. 7
0
    i = (low - 1)
    pivot = A[high]
    for j in range(low, high):
        if A[j] <= pivot:
            i = i + 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[high] = A[high], A[i + 1]
    return i + 1
def quickSortBantu(A, low, high):
    if low < high:
        pi = partition(A, low, high)
        quickSortBantu(A, low, pi - 1)
        quickSortBantu(A, pi + 1, high)
def quickSort(A):
    quickSortBantu(A, 0, len(A)-1)

k = [i for i in range(1, 6000)]
kocok(k)
bub = k[:]
sel = k[:]
ins = k[:]
mer = k[:]
qui = k[:]

aw = detak(); bubbleSort(bub); ak = detak(); print('bubble : %g detik' % (ak-aw))
aw = detak(); selectionSort(sel); ak = detak(); print('selection : %g detik' % (ak-aw))
aw = detak(); insertionSort(ins); ak = detak(); print('insertion : %g detik' % (ak-aw))
aw = detak(); mergeSort(mer); ak = detak(); print('merge : %g detik' % (ak-aw))
aw = detak(); quickSort(qui); ak = detak(); print('quick : %g detik' % (ak-aw))

Esempio n. 8
0
        if (ascending and L[j] < pivot) or (not ascending and L[j] > pivot):
            L[i], L[j] = L[j], L[i]  
            i += 1
    L[low], L[i-1] = L[i-1], L[low] 
    return i - 1, result

def median_of_three(L, low, high):
    mid = (low+high-1)//2
    a = L[low]
    b = L[mid]
    c = L[high-1]
    if a <= b <= c:
        return b, mid
    if c <= b <= a:
        return b, mid
    if a <= c <= b:
        return c, high-1
    if b <= c <= a:
        return c, high-1
    return a, low
mer = k[:]
qui = k[:]
mer2 = k[:]
qui2 = k[:]


aw=detak();mergeSort(mer);ak=detak();print('merge : %g detik' %(ak-aw));
aw=detak();quickSort(qui,0,len(qui)-1);ak=detak();print('quick : %g detik' %(ak-aw));
aw=detak();merge_sort(mer2);print('merge mod : %g detik' %(ak-aw));
aw=detak();quickSortMOD(qui2, False);print('quick mod : %g detik' %(ak-aw));
Esempio n. 9
0
            j+=1
            k+=1

def partition(arr,low,high): 
    i = ( low-1 )        
    pivot = arr[high]    
    for j in range(low , high): 
        if   arr[j] <= pivot: 
            i = i+1 
            arr[i],arr[j] = arr[j],arr[i] 
    arr[i+1],arr[high] = arr[high],arr[i+1] 
    return ( i+1 ) 

def quickSort(arr,low,high): 
    if low < high: 
        pi = partition(arr,low,high) 
        quickSort(arr, low, pi-1) 
        quickSort(arr, pi+1, high)

bub = k[:]
sel = k[:]
ins = k[:]
mer = k[:]
qui = k[:]

aw=detak();bubb(bub);ak=detak();print('Bubble : %g detik' %(ak-aw));
aw=detak();sele(sel);ak=detak();print('Selection : %g detik' %(ak-aw));
aw=detak();inse(ins);ak=detak();print('Insertion : %g detik' %(ak-aw));
aw=detak();mergeSort(mer);ak=detak();print('Merge : %g detik' %(ak-aw));
aw=detak();quickSort(qui,0,len(qui)-1);ak=detak();print('Quick : %g detik' %(ak-aw));
Esempio n. 10
0
print (selectionSort(daftar))
print (insertionSort(daftar))
mergeSort(daftar)
print (daftar)
quickSort(daftar)
print (daftar)

k =  [[i] for i in range(1, 6001)]
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]
u_mrg = k[:]
u_qck = k[:]

aw=detak();bubbleSort(u_bub);ak=detak();print("bubble: %g detik" %(ak-aw));
aw=detak();selectionSort(u_sel);ak=detak();print("selection: %g detik" %(ak-aw));
aw=detak();insertionSort(u_ins);ak=detak();print("insertion: %g detik" %(ak-aw));
aw=detak();mergeSort(u_mrg);ak=detak();print("merge: %g detik" %(ak-aw));
aw=detak();quickSort(u_qck);ak=detak();print("quick: %g detik" %(ak-aw));

#4
class MhsTIF():
    def __init__(self, nama, nim, kota, us):
        self.nama = nama
        self.nim = nim
        self.kota = kota
        self.us = us

    def __str__(self):
        s = self.nama +', NIM '+str(self.nim)\
Esempio n. 11
0
    A[penandaKanan] = temp

    return penandaKanan

k=[]
for i in range(1, 6001):
    k.append(i)
kocok(k)

u_bub = k[:]
u_sel = k[:]
u_ins = k[:]
u_mrg = k[:]
u_qck = k[:]

aw = detak();bubbleSort(u_bub);ak=detak();print("Bubble Sort : %g detik" %(ak-aw));
aw = detak();selectionSort(u_sel);ak=detak();print("Selection Sort : %g detik" %(ak-aw));
aw = detak();insertionSort(u_ins);ak=detak();print("Insertion Sort : %g detik" %(ak-aw));
aw = detak();mergeSort(u_mrg);ak=detak();print("Merge Sort: %g detik" %(ak-aw));
aw = detak();quickSort(u_qck);ak=detak();print("Quick Sort : %g detik" %(ak-aw));


("No. 4")

("No. 5")
import random
def _merge_sort(indices, the_list):
    start = indices[0]
    end = indices[1]
    half_way = (end - start)//2 + start
    if start < half_way:
Esempio n. 12
0
    n = len(A)
    for i in range(1,n):
        nilai = A[i]
        pos = i
        while pos > 0 and nilai < A[pos - 1]:
            A[pos] = A[pos -1]
            pos = pos -1
        A[pos] = nilai
        
def cariPosisiYangTerkecil(A,darisini, sampaisini):
    posisiYangTerkecil = darisini
    for i in range (darisini+1, sampaisini):
        if A[i] < A[posisiYangTerkecil]:
            posisiYangTerkecil = i
    return posisiYangTerkecil

k = []
for i in range(1, 6001):
    k.append(i)
kocok(k)
u_buble = k[:]
u_selection = k[:]
u_insertion = k[:]

awal = detak() ; bubbleSort(u_buble) ;
akhir = detak() ; print('bubble: %g detik' %(akhir - awal)) ;
awal = detak() ; selectionSort(u_selection) ;
akhir =detak() ; print('selection: %g detik' %(akhir - awal)) ;
awal = detak() ; insertionSort(u_insertion) ;
akhir = detak() ; print('insertion: %g detik' %(akhir - awal)) ;
Esempio n. 13
0
    n = len(A)
    for i in range(n - 1):
        indexKecil = cariPosisiYangTerkecil(A, i, n)
        if indexKecil != i :
            swap(A, i, indexKecil)


k = [i for i in range(1, 6001)]
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]
u_mrg = k[:]
u_qck = k[:]

aw = detak();bubleSort(u_bub);ak=detak();print('Bubble Sort : %g detik' %(ak - aw));
aw = detak();insertionSort(u_ins);ak=detak();print('Insertion Sort : %g detik' %(ak - aw));
aw = detak();selectionSort(u_sel);ak=detak();print('Selection Sort : %g detik' %(ak - aw));
aw = detak();mergeSort(u_mrg);ak=detak();print('Merge Sort : %g detik' %(ak - aw));
aw = detak();quickSort(u_qck);ak=detak();print('Quick Sort : %g detik' %(ak - aw));

"""
Hasil dari semua algoritma yang dipelajari di jalankan :
Bubble Sort : 16.8512 detik
Insertion Sort : 6.90141 detik
Selection Sort : 5.46095 detik
Merge Sort : 0.117203 detik
Quick Sort : 0.074353 detik

algoritma QuickSort merupakan algoritma yang paling cepet dari semua algoritma yang dipelajari
"""
Esempio n. 14
0
def selectionSort(A):
    n = len(A)
    for i in range(n-1):
        indexKecil = cariPosisiYangTerkecil(A, i, n)
        if indexKecil != i:
            swap(A, i, indexKecil)

def insertionSort(A):
    n = len(A)
    for i in range(1,n):
        nilai = A[i]
        pos = i
        while pos > 0 and nilai < A[pos-1]:
            A[pos] = A[pos-1]
            pos = pos-1
        A[pos] = nilai
        
from time import time as detak
from random import shuffle as kocok

k = [i for i in range(1,6001)]
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]

aw = detak();bubbleSort(u_bub);ak=detak();print("Bubble    : %g detik"%(ak-aw));
aw = detak();selectionSort(u_sel);ak=detak();print("Selection : %g detik"%(ak-aw));
aw = detak();insertionSort(u_ins);ak=detak();print("Insertion : %g detik"%(ak-aw));
Esempio n. 15
0
    for i in range(1, n):
        nilai = A[i]
        pos = i
        while pos > 0 and nilai < A[pos - 1]:
            A[pos] = A[pos - 1]
            pos = pos - 1
        A[pos] = nilai


from time import time as detak
from random import shuffle as kocok

k = [i for i in range(1, 6001)]
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]

p = detak()
bubbleSort(u_bub)
t = detak()
print("Bubble    : %g detik" % (t - p))
p = detak()
selectionSort(u_sel)
t = detak()
print("Selection : %g detik" % (t - p))
p = detak()
insertionSort(u_ins)
t = detak()
print("Insertion : %g detik" % (t - p))
Esempio n. 16
0
                
def selectionsort(A):
    n = len(A)
    for i in range (n-1):
        indexkecil = cariposisiterkecil(A,i,n)
        if indexkecil != i:
            swap(A,i,indexkecil)

def insertionsort(A):
    n = len(A)
    for i in range (1,n):
        nilai = A[i]
        pos = i
        while pos > 0 and nilai < A[pos-1]:
            A[pos] = A[pos-1]
            pos = pos-1
        A[pos] = nilai
        
from time import time as detak
from random import shuffle as kocok

k = [i for i in range(1,6001)]
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]

aw = detak();bubblesort(u_bub);ak=detak();print('Buble      : %g detik' %(ak-aw));
aw = detak();selectionsort(u_sel);ak=detak();print('Selection  : %g detik' %(ak-aw));
aw = detak();insertionsort(u_ins);ak=detak();print('Insertion  : %g detik' %(ak-aw));
Esempio n. 17
0
print (selectionSort(daftar))
print (insertionSort(daftar))
mergeSort(daftar)
print (daftar)
quickSort(daftar)
print (daftar)

k =  [[i] for i in range(1, 6001)]
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]
u_mrg = k[:]
u_qck = k[:]

aw=detak();bubbleSort(u_bub);ak=detak();print("bubble: %g detik" %(ak-aw));
aw=detak();selectionSort(u_sel);ak=detak();print("selection: %g detik" %(ak-aw));
aw=detak();insertionSort(u_ins);ak=detak();print("insertion: %g detik" %(ak-aw));
aw=detak();mergeSort(u_mrg);ak=detak();print("merge: %g detik" %(ak-aw));
aw=detak();quickSort(u_qck);ak=detak();print("quick: %g detik" %(ak-aw));

##nomor5
daftar = [54,26,93,17,77,31,44,55,20]
def mergeSort2(A, awal, akhir):
    mid = (awal+akhir)//2
    if awal < akhir:
        mergeSort2(A, awal, mid)
        mergeSort2(A, mid+1, akhir)
    a, f, l = 0, awal, mid+1
    tmp = [None] * (akhir - awal + 1)
    while f <= mid and l <= akhir:
Esempio n. 18
0
from time import time as detak
from random import shuffle as kocok
import no05  # mergeSort baru
import no06  # quickSort baru
import no03  # mergeSort dan quickSort awal
k = [i for i in range(1, 6000)]
kocok(k)

merA = k[:]
merB = k[:]
quiA = k[:]
quiB = k[:]

# merge Sort baru
aw = detak(); no05.merge_sort(merB); ak = detak(); print('merge sort baru : %g detik' % (ak-aw))
# Quick Sort baru
aw = detak(); no06.quickSort(quiB); ak = detak(); print('quick sort baru : %g detik' % (ak-aw))

# Merge Sort dan Quick Sort awal
aw = detak(); no03.mergeSort(merA); ak = detak(); print('merge sort awal : %g detik' % (ak-aw))
aw = detak(); no03.quickSort(quiA); ak = detak(); print('quick sort awal : %g detik' % (ak-aw))
Esempio n. 19
0
        pos = i
        while pos > 0 and nilai < A[pos - 1]:
            A[pos] = A[pos -1]
            pos = pos -1
        A[pos] = nilai

def swap(A,p,q):
    tmp = A[p]
    A[p]= A[q]
    A[q]= tmp
    
def cariPosisiYangTerkecil(A,p,q):
    posisiYangTerkecil = p
    for i in range (p+1, q):
        if A[i] < A[posisiYangTerkecil]:
            posisiYangTerkecil = i
        return posisiYangTerkecil
    
k = []
for i in range(1,6001):
    k.append(i)
kocok(k)

u_bub = k[:]
u_sel = k[:]
u_ins = k[:]

aw = detak();bubbleSort(u_bub);ak = detak();print('bubble: %g detik' %(ak-aw));
aw = detak();selectionSort(u_sel);ak = detak();print('selection: %g detik' %(ak-aw));
aw = detak();insertionSort(u_ins);ak = detak();print('insertion: %g detik' %(ak-aw));
Esempio n. 20
0
            if A[min_idx] > A[j]: 
                min_idx = j 
              
    # Swap the found minimum element with  
    # the first element         
        A[i], A[min_idx] = A[min_idx], A[i]

def inse(arr): 
  
    # Traverse through 1 to len(arr) 
    for i in range(1, len(arr)): 
  
        key = arr[i] 
  
        # Move elements of arr[0..i-1], that are 
        # greater than key, to one position ahead 
        # of their current position 
        j = i-1
        while j >=0 and key < arr[j] : 
                arr[j+1] = arr[j] 
                j -= 1
        arr[j+1] = key

bub = k[:]
sel = k[:]
ins = k[:]

aw=detak();bubb(bub);ak=detak();print('bubble : %g detik' %(ak-aw));
aw=detak();sele(sel);ak=detak();print('selection : %g detik' %(ak-aw));
aw=detak();inse(ins);ak=detak();print('insertion : %g detik' %(ak-aw));
Esempio n. 21
0
            A[penandaKanan] = temp

    temp = A[awal]
    A[awal] = A[penandaKanan]
    A[penandaKanan] = temp

    return penandaKanan
    
def quickSortBantu(A, awal, akhir):
    if awal < akhir:
        titikBelah = partisi(A, awal, akhir)
        quickSortBantu(A, awal, titikBelah - 1)
        quickSortBantu(A, titikBelah + 1, akhir)

def quickSort(A):
    quickSortBantu(A, 0, len(A) - 1)

k = list(range(6000)) 
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]
u_mrg = k[:]
u_qck = k[:]

aw = detak();bubbleSort(u_bub);ak = detak();print('bubble : %g detik' %(ak-aw));
aw = detak();selectionSort(u_sel);ak = detak();print('selection : %g detik' %(ak-aw));
aw = detak();insertionSort(u_ins);ak = detak();print('insertion : %g detik' %(ak-aw));
aw = detak();mergeSort(u_mrg);ak = detak();print('merge : %g detik' %(ak-aw));
aw = detak();quickSort(u_qck);ak = detak();print('quick : %g detik' %(ak-aw));
Esempio n. 22
0

def quickSort(arr, low, high):
    if low < high:
        pi = partition(arr, low, high)
        quickSort(arr, low, pi - 1)
        quickSort(arr, pi + 1, high)


bub = k[:]
sel = k[:]
ins = k[:]
mer = k[:]
qui = k[:]

aw = detak()
bubb(bub)
ak = detak()
print('bubble : %g detik' % (ak - aw))
aw = detak()
sele(sel)
ak = detak()
print('selection : %g detik' % (ak - aw))
aw = detak()
inse(ins)
ak = detak()
print('insertion : %g detik' % (ak - aw))
aw = detak()
mergeSort(mer)
ak = detak()
print('merge : %g detik' % (ak - aw))
Esempio n. 23
0
def test2(A):
    n = len(A)
    for i in range(n-1):
        indexKecil = cari(A, i, n)
        if indexKecil != i:
            temp(A, i, indexKecil)

def test3(A):
    n = len(A)
    for i in range(1,n):
        nilai = A[i]
        pos = i
        while pos > 0 and nilai < A[pos-1]:
            A[pos] = A[pos-1]
            pos = pos-1
        A[pos] = nilai
        
from time import time as detak
from random import shuffle as kocok

k = [i for i in range(1,6001)]
kocok(k)
u_bub = k[:]
u_sel = k[:]
u_ins = k[:]

p = detak();test1(u_bub);t=detak();print("Bubble    : %g detik"%(t-p));
p = detak();test2(u_sel);t=detak();print("Selection : %g detik"%(t-p));
p = detak();test3(u_ins);t=detak();print("Insertion : %g detik"%(t-p));