0% found this document useful (0 votes)
38 views11 pages

Metode Pengurutan Data Buble Sort

The document discusses various sorting algorithms including bubble sort, quicksort, selection sort, insertion sort, heap sort, binary search, shell sort, and more. For each algorithm, it provides code implementations in C programming language to read in an array, print the array, perform the sorting algorithm on the array, and print the sorted array. It contains main functions to test each algorithm and accept user input for the unsorted array and values to search for.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views11 pages

Metode Pengurutan Data Buble Sort

The document discusses various sorting algorithms including bubble sort, quicksort, selection sort, insertion sort, heap sort, binary search, shell sort, and more. For each algorithm, it provides code implementations in C programming language to read in an array, print the array, perform the sorting algorithm on the array, and print the sorted array. It contains main functions to test each algorithm and accept user input for the unsorted array and values to search for.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

METODE PENGURUTAN DATA

BUBLE SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//-------------------------------------------------------------------------void read_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n\n\t INPUT ISI ARRAY [%d] : ",i);
scanf("%d",&a[i]);
}
}
//-------------------------------------------------------------------------void print_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
//-------------------------------------------------------------------------void bubble_sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\n\n\t TAHAP %d : ",i);
print_list(a,n);
}
}
//-------------------------------------------------------------------------int main()
{
int a[20],n;
//textcolor(10);
//clrscr();
printf("\n\n\t INPUTKAN JUMLAH ARRAY : ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t BERIKUT INI ADALAH ISI ARRAY : ");
print_list(a,n);

bubble_sort(a,n);
printf("\n\n\t SETELAH DIURUTKAN : ");
print_list(a,n);
getch();
}

//-------------------------------------------------------------------------QUICK SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//-------------------------------------------------------------------------void read_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n\n\t INPUT ISI ARRAY [%d] :: ",i);
scanf("%d",&a[i]);
}
}
//-------------------------------------------------------------------------void print_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\n\n\t %d",a[i]);
}
//-------------------------------------------------------------------------void quick_sort(int a[],int first,int last)
{
int low,high,temp,pivot;
low=first;
high=last;
pivot=a[(first+last)/2];
do
{
while(a[low]<pivot)
low++;
while(a[high]>pivot)
high--;
if(low<=high)
{
temp=a[low];
a[low]=a[high];
a[high]=temp;
low=low+1;
high=high-1;

}
}while(low<=high);
if(first<high)
quick_sort(a,first,high);
if(low<last)
quick_sort(a,low,last);
}
//-------------------------------------------------------------------------int main()
{
int a[20],n;
//textcolor(10);
//clrscr();
printf("\n\n\t MASUKKAN PANJANG ARRAY : ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t BERIKUT INI ISI ARRAY : ");
print_list(a,n);
quick_sort(a,0,n-1);
printf("\n\n\t SETELAH DIURUTKAN : ");
print_list(a,n);
getch();
}
//--------------------------------------------------------------------------

SELECTION SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//-------------------------------------------------------------------------void read_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n\n\t INPUT ISI ARRAY [%d] :: ",i);
scanf("%d",&a[i]);
}
}
//-------------------------------------------------------------------------void print_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
//-------------------------------------------------------------------------void select_sort(int a[],int n)

{
int i,j,temp,min;
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[min]>a[j])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
printf("\n\n\t TAHAP %d :: ",i);
print_list(a,n);
}
}
//-------------------------------------------------------------------------int main()
{
int a[20],n;
//textcolor(10);
//clrscr();
printf("\n\n\t MASUKKAN PANJANG ARRAY : ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t BERIKUT INI ISI ARRAY : ");
print_list(a,n);
select_sort(a,n);
printf("\n\n\t SETELAH DIURUTKAN : ");
print_list(a,n);
getch();
}
//--------------------------------------------------------------------------

INSERT SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//-------------------------------------------------------------------------void read_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n\n\t INPUT ISI ARRAY [%d] :: ",i);
scanf("%d",&a[i]);
}
}
//-------------------------------------------------------------------------void print_list(int a[],int n)

{
int i;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
//-------------------------------------------------------------------------void insert_sort(int a[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0 && a[j]>temp;j--)
a[j+1]=a[j];
a[j+1]=temp;
printf("\n\n\t TAHAP %d :: ",i);
print_list(a,n);
}
}
//-------------------------------------------------------------------------int main()
{
int a[20],n;
//textcolor(10);
//clrscr();
printf("\n\n\t MASUKKAN PANJANG ARRAY: ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t BERIKUT ISI ARRAY : ");
print_list(a,n);
insert_sort(a,n);
printf("\n\n\t SETELAH DIURUTKAN : ");
print_list(a,n);
getch();
}
//--------------------------------------------------------------------------

HEAP SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//--------------------------------------------------------------------------typedef struct heap
{
int val;
struct heap *left,*right;
}*TR;
//--------------------------------------------------------------------------TR new_node()
{
TR new;

new=malloc(sizeof(struct heap));
new->left=new->right=NULL;
return(new);
}
//--------------------------------------------------------------------------TR get_node()
{
TR new;
new=new_node();
printf("\n\n\t INPUT SUATU ANGKA : ");
scanf("%d",&new->val);
return(new);
}
//--------------------------------------------------------------------------void insert_node(TR temp,TR new)
{
if(temp->val>new->val)
if(temp->left!=NULL)
insert_node(temp->left,new);
else
temp->left=new;
else
if(temp->right!=NULL)
insert_node(temp->right,new);
else
temp->right=new;
}
//--------------------------------------------------------------------------void heap_sort(TR temp)
{
if(temp!=NULL)
{
heap_sort(temp->left);
printf("%d\t",temp->val);
heap_sort(temp->right);
}
}
//--------------------------------------------------------------------------TR create()
{
TR head,new,temp;
char c;
head=new_node();
new=get_node();
head->left=new;
printf("\n\n\t ADA LAGI (Y/N) : ");
c=getche();
while(c=='y'||c=='Y')
{
new=get_node();
insert_node(head->left,new);
printf("\n\n\t ADA LAGI (Y/N) : ");
c=getche();
}

return(head);
}
//--------------------------------------------------------------------------int main()
{
TR head;
int choice;
//textcolor(10);
while(1)
{
//clrscr();
printf("\n\n\t ******* MENU *******");
printf("\n\n\t 1> BACA ISI");
printf("\n\n\t 2> HEAP SORT");
printf("\n\n\t 3> KELUAR");
printf("\n\n\t MASUKKAN PILIHAN ANDA : ");
scanf("%d",&choice);
switch(choice)
{
case 1:head=create();
break;
case 2:printf("\n\n\t SETELAH DIURUTKAN : ");
printf("\n\n\t ");
heap_sort(head->left);
break;
case 3:printf("\n\n\t TEKAN ESC UNUTK KELUAR");
if(getch()==27)
exit(0);
break;
}
getch();
}
}
//---------------------------------------------------------------------------

SEQUENCE BINARY
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//-------------------------------------------------------------------------void read_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n\n\t INPUT ISI ARRAY [%d] :: ",i);
scanf("%d",&a[i]);
}

}
//-------------------------------------------------------------------------void print_list(int a[],int n)
{
int i;
printf("\n\n\t ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
//-------------------------------------------------------------------------void insert_sort(int a[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0 && a[j]>temp;j--)
a[j+1]=a[j];
a[j+1]=temp;
}
}
//-------------------------------------------------------------------------int seq_search(int a[],int n,int val)
{
int i;
for(i=0;i<n;i++)
if(a[i]==val)
return(i+1);
return(0);
}
//-------------------------------------------------------------------------int bin_search(int a[],int left,int right,int val)
{
int i,mid;
mid=(left+right)/2;
if(left>right)
return(0);
if(a[mid]==val)
return(mid+1);
if(val<a[mid])
bin_search(a,left,mid-1,val);
else
bin_search(a,mid+1,right,val);
}
//-------------------------------------------------------------------------int main()
{
int a[20],n,val,choice;
char c;
//textcolor(10);
while(1)
{
//clrscr();
printf("\n\n\t ****** MENU *******");
printf("\n\n\t 1> BACA ISI");

printf("\n\n\t 2> TAMPILKAN ISI");


printf("\n\n\t 3> SEQUENTIAL SEARCH");
printf("\n\n\t 4> BINARY SEARCH");
printf("\n\n\t 5> KELUAR");
printf("\n\n\t MASUKKAN PILIHAN ANDA : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n\n\t MASUKKAN PANJANG ARRAY : ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t ISI ARRAY : ");
print_list(a,n);
break;
case 2:printf("\n\n\t BERIKUT ISI ARRAY : ");
print_list(a,n);
break;
case 3:do
{
printf("\n\n\t MASUKKAN ANGKA YANG DICARI : ");
scanf("%d",&val);
val=seq_search(a,n,val);
if(val)
{
printf("\n\n\t POSISI ANGKA SEKARANG : %d",val);
print_list(a,n);
}
else
{
printf("\n\n\t TIDAK ADA");
print_list(a,n);
}
printf("\n\n\t CARI ANGKA LAGI (Y/N) :: ");
c=getche();
}while(c=='y'||c=='Y');
break;
case 4:do
{
printf("\n\n\t MASUKKAN ANGKA DICARI : ");
scanf("%d",&val);
insert_sort(a,n);
val=bin_search(a,0,n-1,val);
if(val)
{
printf("\n\n\t POSISI ANGKA SEKARANG : %d",val);
print_list(a,n);
}
else
{
printf("\n\n\t TIDAK ADA");
print_list(a,n);
}
printf("\n\n\t CARI ANGKA LAGI (Y/N) :: ");
c=getche();

}while(c=='y'||c=='Y');
break;
case 5:printf("\n\n\t TEKAN ESC UNTUK KELUAR");
if(getch()==27)
exit(0);
break;
}
getch();
}
}
//--------------------------------------------------------------------------

SHELL SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//-------------------------------------------------------------------------void read_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n\n\t INPUT ISI ARRAY [%d] :: ",i);
scanf("%d",&a[i]);
}
}
//-------------------------------------------------------------------------void print_list(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
//-------------------------------------------------------------------------void shell_sort(int a[],int n)
{
int temp,flag,m,i;
m=n;
do
{
flag=0;
m=(m+1)/2;
for(i=0;(i+m)<n;i++)
if(a[i]>a[i+m])
{
temp=a[i];
a[i]=a[i+m];
a[i+m]=temp;
flag=1;
}

printf("\n\n\t TAHAP %d ::",i);


print_list(a,n);
}while(flag!=0 || m>1);
}
//-------------------------------------------------------------------------int main()
{
int a[20],n;
//textcolor(10);
//clrscr();
printf("\n\n\t MASUKKAN PANJANG ARRAY : ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t BERIKUT ISI ARRAY : ");
print_list(a,n);
shell_sort(a,n);
printf("\n\n\t SETELAH DIURUTKAN : ");
print_list(a,n);
getch();
}
//--------------------------------------------------------------------------

You might also like