NAMA : DANDY ALFIANTO
NIM : 41119189
UNIVERSITAS DIAN NUSANTARA
NIM : 41119189
UNIVERSITAS DIAN NUSANTARA
Manfaat
:
- Untuk
mengatur sekelompok bilangan dengan urutan dari kecil ke besar.
- Untuk
proses, akan dicari elemen-elemen yang belum diurutkan yang memiliki nilai
terkecil akan dipertukarkan ke posisi yang tepat di dalam array.
Program :
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
int Nilai[20];
int i, j, N, l;
int temp, U, Imaks;
cout<<"PROGRAM
SELECTION SORT"<<endl;
cout<<"-----------------------"<<endl;
cout<<"DANDY
ALFIANTO-41119189"<<endl;
cout<<endl;
cout<<"Masukkan Banyak Bilangan : ";
cin>>N;
for(i=0; i<N; i++)
{
cout<<"Elemen
ke-"<<i<<" : ";
cin>>Nilai[i];
}
//Proses Cetak Sebelum diurutkan
cout<<"\nData sebelum diurut : "<<endl;
for(i=0; i<N; i++)
cout<<setw(3)<<Nilai[i];
//Proses Pengurutan
U=N-1;
for(i=0; i<=N-2; i++)
{
Imaks = 0;
for(j=1; j<=U; j++)
{
if(Nilai[j] > Nilai[Imaks])
Imaks = j;
}
temp = Nilai[U];
Nilai[U] = Nilai[Imaks];
Nilai[Imaks] = temp;
U--;
cout<<endl;
for(l=0; l<N; l++)
cout<<setw(3)<<Nilai[l];
}
cout<<endl;
cout<<"\nData Setelah di urut : "<<endl;
for(i=0; i<N; i++)
cout<<setw(3)<<Nilai[i];
getch();
}
REFERENSI :



0 komentar:
Posting Komentar