//nama: i wayan agus gerdian
//nim : 100010021
#include <iostream.h>
#include <conio.h>
int data[1000];
void tampilkan_larik(int n)
int i;
for(i=1;i<=n;i++)
cout<<data[i]<<" ";
cout<<"\n";
}
void tukar(int a, int b)
{
int t;
t = data[b];
data[b] = data[a];
data[a] = t;
}
void bubble(){
clrscr();
int n,i,j,tmp;
cout<<"bubble sort";
cout<<"\nmasukkan banyak data: ";
cin>>n;
for(i=1;i<=n;i++){
cout<<"data ke ["<<i<<"]= ";
cin>>data[i];
}
cout<<"\nsebelum data diurutkan: ";
for(i=1;i<=n;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
for(i=1;i<n; i++){
for(j=i+1;j<=n;j++){
if(data[i]>data[j]){
tmp = data[i];
data[i] = data [j];
data[j] = tmp;
}
cout<<"tahap "<<i<<" : ";
tampilkan_larik (n);
}
}
getch();
}
void insertion(){
clrscr();
int n,i,temp,j;
cout<<"insertion";
cout<<"\nmasukkan banyak data: ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<"data ke ["<<i<<"]= ";
cin>>data[i];
}
cout<<"\nsebelum data diurutkan: ";
for(i=1;i<=n;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
for(i=1;i<=n;i++){
temp = data[i];
j = i -1;
while(data[j]>temp && j>=0)
{
data[j+1] = data[j];
j--;
}
data[j+1] = temp;
cout<<"tahap "<<i<<" : ";
tampilkan_larik (n);
}
getch();
}
void selection(){
clrscr();
int pos,i,j,n;
cout<<"selection sort";
cout<<"\nmasukkan banyak data: ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<"data ke ["<<i<<"]= ";
cin>>data[i];
}
cout<<"\nsebelum data diurutkan: ";
for(i=1;i<=n;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
for(i=1;i<=n-1;i++){
pos = i;
for(j = i+1;j<=n;j++){
if(data[j] < data[pos]) pos = j;
}
if(pos != i) tukar(pos,i);
{cout<<"tahap "<<i<<" : ";
tampilkan_larik (n);}
}
getch();
}
void main(){
int pilihan;
cout<<"++ menu sorting ++";
cout<<"\n\n1.Bubble";
cout<<"\n2.Insertion";
cout<<"\n3.Selection";
cout<<"\nmasukkan pilihan anda(1/2/3): ";
cin>>pilihan;
if(pilihan==1)
bubble();
else if(pilihan==2)
insertion();
else if(pilihan==3)
selection();
else
cout<<" tekan enter untuk exit";
getch();
}
0 comments:
Post a Comment
Komentar sobat akan saya hapus jika:
- Mengandung unsur SPAM
- LINK hidup
- tidak sesuai dengan topik
NB:
jika ingin menyisipkan LINK ,silahkan pilih Name/URL pada option profil !