Contoh Program Bubble Short

#include <iostream.h>
#include <conio.h>

int data[10],data2[10];
int n;
void tukar(int a,int b)
{
int t;
t = data[b];
data[b] = data[a];
data[a] = t;
}

void bubble_sort(){
for(int i=1;i<n;i++){
for(int j=n-1;j>=i;j–){
if(data[j]<data[j-1]) tukar(j,j-1);
}
}
cout<<“bubble sort selesai!\n”;
}
void Input(){
cout<<“Masukkan jumlah data = “;cin>>n;
for(int i=0;i<n;i++){
cout<<“Masukkan data ke- “<<(i+1)<<“=”;cin>>data[i];
data2[i] = data[i];
}
}
void Tampil(){
cout<<“Data : “;
for(int i=0;i<n;i++){
cout<<data[i]<<” “;
}
cout<<“\n”;
}

void main(){
clrscr();
int pil;
do{
clrscr();
cout<<“1. Input Data\n”;
cout<<(“2. Bubble Sort\n”);
cout<<(“3. Tampilkan Data\n”);
cout<<(“4. Exit\n”);
cout<<(“Pilihan = “);cin>>pil;
switch(pil){
case 1:Input();break;
case 2:bubble_sort();break;
case 3:Tampil();break;

}
getch();
}while(pil!=4);
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s