Skip to content

Bubble Sort

November 26, 2008

# 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<<i;

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<<i;

gotoxy(24,y);

cout<<"|";

gotoxy(28,y);

cout<<L[i];

gotoxy(32,y);

cout<<"|";

y++;

}

gotoxy(20,(7+n));

cout<<"=============";

getch();

}

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: