Selasa, 25 November 2008

Bubble Sort

# include iostream.h>

# include conio.h>



void main()

{

float L[50];

int n,i,j;

float temp;

//Input data

cout<<"Masukkan banyak data : ";
cin>>n;

cout<<"\nMasukkan Nilai";
//Membuat tabel

cout<<"\n=============";
cout<<"\n| i | Nilai |";
cout<<"\n|-----------|";
int y = 7;

gotoxy(1,(y+n));

cout<<"=============";
for (i=1;i<=n;i++)
{

gotoxy(3,y);

cout< gotoxy(1,y);

cout<<("|");
gotoxy(5,y);

cout<<"|";
gotoxy(13,y);

cout<<"|";
y++;

}

y=7;

//Input nilai data

for (i=1;i<=n;i++)
{

gotoxy(9,y);

cin>>L[i];

y++;

}

cout<<"\nUrutkan secara : ";
cout<<"\n1. Ascending";
cout<<"\n2. Descending";
int pilih;

do //Validasi pilihan

{

cout<<"\nMasukkan nomor nya saja : ";
cin>>pilih;

if ((pilih!=1)&&(pilih!=2))

cout<<"\nPilihan tidak valid, ulangi!";}
while((pilih != 1) && (pilih!= 2));

//Cek kondisi

if (pilih==1)

{

//Mengurutkan ascending

for (i=1;i<=n;i++)
{

for (j=n;j>=2;j--)

{

if (L[j] < L[j-1])
{

//Menukar nilai

temp=L[j];

L[j]=L[j-1];

L[j-1]=temp;

}

}

}

}

else

{

//Mengurutkan descending

for (i=1;i<=n;i++)
{

for (j=n;j>=2;j--)

{

if (L[j] > L[j-1])

{

//Menukar nilai

temp=L[j];

L[j]=L[j-1];

L[j-1]=temp;

}

}

}

}

//Membuat tabel data yang terurut

y=7;

gotoxy(17,3);

cout<<"Data Setelah di Urut";
gotoxy(20,4);

cout<<"=============";
gotoxy(20,5);

cout<<"| i | Nilai |";
gotoxy(20,6);

cout<<"-------------";
for (i=1;i<=n;i++)
{

gotoxy(20,y);

cout<<"|";
gotoxy(22,y);

cout< gotoxy(24,y);

cout<<"|";
gotoxy(28,y);

cout< gotoxy(32,y);

cout<<"|";
y++;

}

gotoxy(20,(7+n));

cout<<"=============";
getch();

}

0 komentar:

Template by : kendhin x-template.blogspot.com