Sunday 18 December 2011

Insertion sort-ascending


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int p;
int x,y, L[60], n;
cout<<"Masukan Banyaknya Data: "; cin>>n;
cout<<"\n";
for (p=1; p<=n; p++)
{
cout<<"Data ke-"<<p<<" : "; cin>>L[p];
}

for (p=1; p<n; p++)
{
x=p+1;
for (int c=1; c<x; c++)
{
if (L[x]<L[c])
{
y=L[c];
L[c]=L[x];
L[x]=y;
}
}
}
cout<<"\n";
cout<<"\n Secara Ascending\n";
for (p=1; p<=n; p++)
{
cout<<L[p]<<" ";
}
for (p=1; p<n; p++)
{
x=p+1;
for (int c=1; c<x; c++)
{
if (L[x]>L[c])
{
y=L[c];
L[c]=L[x];
L[x]=y;
}
}
}
getch();}

No comments:

Post a Comment