You are on page 1of 10

# //Bubble Sort

#include <stdio.h>
#define N 20
int bubble(int n);
int i,j,A[N];
main()
{
int jml;
printf("\t METODE BUBBLE SORT \n\n");
printf("Masukkan jumlah bilangan: ");
scanf("%d",&jml);
printf("\n");
// input data
for (i=0;i<jml;i++)
{
printf("Bilangan ke %d : ",i+1);
scanf("%d",&A[i]);
}
printf("\n");
// mengurutkan data
bubble(jml);
// menampilkan data
printf("Data yang sudah terurut : \n");
for (i=0;i<jml;i++)
{
printf("%d\n",A[i]);

i++) { for (j=i.i<=n-1. } } } } . for (i=1. A[j] = temp.} } // fungsi bubble int bubble(int n) { int temp.j<n.j++) { if (A[i-1]>A[j]) { temp = A[i-1]. A[i-1] = A[j].

a+1).scanf("%d".&total).a++){ printf("bilangan ke %d = ".&data[a]).scanf("%d". .a<total. void input(){ printf("\t METODE MERGE SORT \n\n"). printf("masukkan jumlah bilangan = "). for(int a=0.h> int total.salin[10].//marge sort #include <stdio.data[10].

} for(int a=0. int lowerbound=lowptr.int lowptr.int highptr.} } void merge(int salin[]. int mid=highptr-1. while(lowptr<=mid && highptr<=upperbound){ if(data[lowptr]<data[highptr]){ salin[x++]=data[lowptr++].int upperbound){ int x=0.a++){ data[lowerbound+a]=salin[a]. } while(lowptr<=mid){ salin[x++]=data[lowptr++].a<n. } } . } else{ salin[x++]=data[highptr++]. int n=upperbound-lowerbound+1. } while(highptr<=upperbound){ salin[x++]=data[highptr++].

sorting\n").} void devide(int salin[].int kanan){ if(kiri<kanan){ int mid=(kiri+kanan)/2.a++){ printf("%d ".data[a]).int kiri. } } void sort(){ devide(salin.kiri.mid+1.mid).total-1). view().a<total.kanan). . printf("sebelum di. merge(salin. } void view(){ for(int a=0. } printf("\n"). devide(salin. } int main(){ input(). devide(salin.kanan).0.kiri.mid+1.

sort(). } //Quick Sort #include <stdio. printf("sesudah di.h> void quickSort( int[]. .sorting\n"). int). int. view().

int). printf("\n\nsesudah Di.&a[i]).&total). total-1).scanf("%d". printf("masukkan jumlah bilangan = "). int main() { int total. for(i=0. for(i = 0. int total.i++){ printf("bilangan ke %d = ". quickSort( a. i < total. ++i){ printf(" %d ". printf("\t METODE QUICK SORT \n\n"). ++i) printf(" %d ".sorting: ").sorting: "). } . 0.} printf("\n").int partition( int[]. a[i]). i < total. a[i]).scanf("%d". } printf("\n\nsebelum Di. int. for(i = 0.i+1).i<total. int a[1000]. int i.

int r) { int j. quickSort( a. int l. while( a[i] <= pivot && i <= r ). j-1). a[i] = a[j]. int l. quickSort( a.void quickSort( int a[]. while( a[j] > pivot ). if( i >= j ) break. a[j] = t. j+1. j. pivot = a[l]. i. } } int partition( int a[]. j = r+1. t. i = l. r). l. while( 1) { do ++i. r). . l. int r) { int pivot. do --j. if( l < r ) { // divide and conquer j = partition( a. t = a[i].

} t = a[l]. } Algoritma quicksort: if (right > left) then . a[j] = t. a[l] = a[j]. return j.

pivIdx) quicksort(num.left. pivNewIdx+1.right) → num . left.pivIdx ← left {pengambilan pivot point bisa apa saja / random} pivNewIdx ← partition (num. pivNewIdx-1) quicksort(num.right.