Jumat, 04 April 2014

PROGRAM SORTING C++ SEDERHANA

Contoh ASCENDING





=======================================================
Contoh DESCENDING



======================================================
#include <cstdlib>
#include <iostream>

using namespace std;

main()
{ int a[5],i,j,n,tampung,terkecil,pil;
cout<<"Input Banyak Data =";cin>>n;
for(i=0;i<n;i++){
cout<<"Input data ke "<<i<<" = ";cin>>a[i];
}
cout<<"Sebelum sorting: "<<endl;
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"1.ASC"<<endl;
cout<<"2.DSC"<<endl;
cout<<"Input Pilihan =";cin>>pil;
if (pil ==1)
{
for(i=0;i<n-1;i++){
for (j=i;j<n-1;j++){
if(a[i]>a[j+1]){
tampung=a[j+1];
a[j+1]=a[i];
a[i]=tampung;

}
}
}
for (i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
if (pil ==2)
{ for(i=0;i>n-1;i++){
for (j=i;j>n-1;j++){
if(a[i]<a[j+1]){
tampung=a[j+1];
a[j+1]=a[i];
a[i]=tampung;

}
}
}
for (i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
system("pause");
return 0;
}
====================================================
Semoga membantu

Tidak ada komentar:

Posting Komentar