Selasa, 25 November 2008

Insertion Sort

# include iostream.h>

# include conio.h>



void main()

{

float L[50];

int n,i,j;

float p;

int pilih;

do

{

clrscr();

cout<<"==================";
cout<<"\n| Insertion Sort |";
cout<<"\n==================";
cout<<"\n";
//Input Data

cout<<"\nMasukkan banyak data : ";
cin>>n;

//Tampilan berupa tabel

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

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

}

//Input nilai data

y=11;

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

gotoxy(9,y);

cin>>L[i];

y++;

}

cout<<"\nUrutkan secara : ";
cout<<"\n1. Ascending";
cout<<"\n2. Descending";
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 pilihan

if (pilih==1)

{

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

p = L[i];

j = i-1;

while ((j>0)&&(L[j] > p))

{

L[j + 1] = L[j];

j = j-1;

}

L[j+1] = p;

}

}

else

{

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

p = L[i];

j = i-1;

while ((j>0)&&(L[j] < p))
{

L[j + 1] = L[j];

j = j-1;

}

L[j+1] = p;

}

}

//Tampilan data terurut dalam tabel

y=11;

gotoxy(17,7);

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

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

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

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,(11+n));

cout<<"=============";
gotoxy(1,n+16);

do

{

cout<<"\n1. Ulang Program";
cout<<"\n2. Keluar";
cout<<"\nMasukkan pilihan : ";
cin>>pilih;

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

{

cout<<"Pilihan tidak valid! Ulangi!";
}

}

while(pilih!=1 && pilih!=2);

}

while(pilih!=2);

getch();

}

2 komentar:

meidhy andaresta mengatakan...

saya sangat terbantu dengan tulisan mas,,
saya mau jadi penggemar setianya deh..
tapi saya butuh bantuan,saya dapat tugas membuat algoritma insertion Sort Descending dalam paskal,,apakah bisa dibantu dalam 2 hari ini?..



http://Meidhyandarestablogme.wordpress.com

Dedik PS mengatakan...

kan uda saya posting untuk semua teknik sorting mas.. dalam bahasa pascal, dan dalam bentuk unit.

http://adekdik.blogspot.com/2008/10/unit-sorting.html

Template by : kendhin x-template.blogspot.com