0% found this document useful (0 votes)
24 views8 pages

Program Implementasi Quick Sort: Nama: Aditya Rizki Pratama Kelas: A Prodi: Manajemen Informatika

The document describes three algorithms: quicksort, mergesort, and heapsort. It provides code implementations and examples of each algorithm. For quicksort, it shows C code to implement quicksort on an array and provides a step-by-step example. For mergesort, it similarly shows C code and an example. For heapsort, it shows C code to implement heapsort using a max heap and provides explanations of the algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Program Implementasi Quick Sort: Nama: Aditya Rizki Pratama Kelas: A Prodi: Manajemen Informatika

The document describes three algorithms: quicksort, mergesort, and heapsort. It provides code implementations and examples of each algorithm. For quicksort, it shows C code to implement quicksort on an array and provides a step-by-step example. For mergesort, it similarly shows C code and an example. For heapsort, it shows C code to implement heapsort using a max heap and provides explanations of the algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAM IMPLEMENTASI QUICK SORT

Nama

Aditya Rizki Pratama

Kelas

Prodi

Manajemen Informatika

#include < stdio.h >


void quicksort ( int [ 10 ] , int , int ) ;
int main () {
int x [ 20 ] , ukuran, i ;
printf ( " Masukkan ukuran array : " ) ;
scanf ( " % d " , & ukuran ) ;
printf ( " Masukkan elemen d% : " , ukuran ) ;
for ( i = 0 ; i < ukuran ; i ++ )
scanf ( " % d " , & x [ i ] ) ;
quicksort ( x , 0 , ukuran 1 ) ;
printf ( " elemen Diurutkan : " ) ;
for ( i = 0 ; i < ukuran ; i ++ )
printf ( " % d " , x [ i ] ) ;
}

return 0 ;

void quicksort ( int x [ 10 ] , int pertama , int terakhir) {


int pivot , j , temp, i ;
if ( pertama < terakhir) {
pivot = pertama ;
i = pertama ;
j = terakhir;
wihile ( i < j ) {
while ( x [ i ] < = x [ pivot ] && i < terakhir)
i ++ ;
while ( x [ j ] > x [ pivot ] )
j-- ;
if ( i < j ) {
temp = x [ i ] ;
x [ i ] = x [ j ] ;
x [ j ] = suhu ;
}
}

temp = x [ pivot ] ;
x [ pivot ] = x [ j ] ;
x [ j ] = temp ;
quicksort ( x , pertama , j - 1 ) ;
quicksort ( x , j + 1 , terakhir) ;
}

Ilustrasi quick sort


Contoh :

L.1
L.2
L.3
L.4
L.5

3
3
3
3
3

7
7
4
4
4

8
8
2
2
2

5
4
7
1
1

2
2
8
5
5

1
1
1
7
5

9
9
9
9
9

5
5
5
8
8

4
5
5
5
7

L.6
L.7

2
1

1
2

3
3

4
4

5
5

5
5

7
7

8
8

9
9

PROGRAM IMPLEMENTASI MERGE SORT


Nama

Aditya Rizki Pratama

Kelas

Prodi

Manajemen Informatika

#include<stdio.h>
void merge(int kiri,int tengah,
int kanan)
{
int h,i,j,b[50],k;
h=kiri;
i=kiri;
j=tengah+1;
while((h<=tengah)&&(j<=kanan))
{
if(bilangan[h]<=bilangan[j])
{
b[i]=bilangan[h];
h++;
}
else
{
b[i]=bilangan[j];
j++;
}
i++;
}
if(h>tengah)
{
for(k=j;k<=kanan;k++)
{
b[i]=bilangan[k];
i++;
}
}
else
{
for(k=h;k<=tengah;k++)
{
b[i]=bilangan[k];
i++;
}
}

for(k=kiri;k<=kanan;k++)
bilangan[k]=b[k];
}
void merge_sort(int kiri,
int kanan)
{
int tengah;
if(kiri<kanan)
{
tengah=(kiri+kanan)/2;
merge_sort(kiri,tengah);
merge_sort(tengah+1,
kanan);
merge(kiri,tengah,
kanan);
}

Ilustrasi merge sort


Contoh :

22

10

15

Proses 1
1

L.1
L.2

10
22

15
3

3
15

8
2

2
8

Proses 2
L.1 10
L.2 3

22
10

3
15

15
22

2
2

8
8

Proses 3
L.1 3
L.2 3

10
2

15
8

22
10

2
15

8
22

22
10

PROGRAM IMPLEMENTASI HEAP SORT


Nama

Aditya Rizki Pratama

Kelas

Prodi

Manajemen Informatika

#include<stdio.h>
void heapsort(int[],int);
main()
{
int n,i,a[50];
system("clear");
printf("\nMasukkan batas:");
scanf("%d",&n);
printf("\nMasukkan elemen:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\nelemen diurutkan:\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n");

void heapsort(int a[],int n)


{

int i,t;
heapsort(a,n);
for(i=n-1;i>0;i--)

t = a[0];
a[0] = a[i];
a[i] = t;
menyesuaikan(a,i);

}
{

void heapsort(int a[],int n)


int k,i,j,item;

for(k=1;k<n;k++)

item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j]))

a[i] = a[j];
i = j;
j = (i-1)/2;

a[i] = item;

}
}

void heapsort(int a[],int n)

int i,j,item;
j = 0;
item = a[j];
i = 2*j+1;
while(i<=n-1)

if(i+1 <= n-1)


if(a[i] <a[i+1])
i++;
if(item<a[i])
{

a[j] = a[i];
j = i;
i = 2*j+1;

}
else

}
a[j] = item;

break;

Ilustrasi heap sort

You might also like