Setelah kita tauu tentang bubble sort dan insertion sort kini saya akan membahas tentang selection sort.... Marii kita liat algoritma dan programnya,, okayy check it out...
Algoritma
Procedure Selection Sort ( input/output data : larik, input n : integer )
Deskripsi
for i ß 1 to n-1 do
pos = i
for j ß i+1 to n do
if ( data[j]<data[pos])
pos = j
if (pos != 0 i) then
tukar(pos,i)
endif
endif
endfor
endfor
Berikut programnya :
#include <iostream.h>
#include <conio.h>
int data[10],data2[10];
int n;
void tukar(int a, int b)
{
int t;
t = data[b];
data[b] = data[a];
data[a] = t;
}
void selection_sort()
{
int pos,i,j;
for(i=1;i<=n-1;i++)
{
pos = i;
for(j = i+1;j<=n;j++)
{
if(data[j] < data[pos]) pos = j;
}
if(pos != i) tukar(pos,i);
}
}
void main()
{
cout<<"===PROGRAM SELECTION SORT==="<<endl;
cout<<"Masukkan Jumlah Data : ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"Masukkan data ke "<<i<<" : ";
cin>>data[i];
data2[i]=data[i];
}
selection_sort();
cout<<"Data Setelah di Sort : ";
for(int i=1; i<=n; i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nSorting dengan selection sort Selesai";
getch();
}
Output :
Program Explanation:
In the program above the selection sort program in C + +. Each data (eg the first data) will be compared with existing data in the next (second data). Which is how the program is looking for the most smallest data then placed on the left and then find the second smallest and most left placed second and so on in a similar manner.
In the program above the selection sort program in C + +. Each data (eg the first data) will be compared with existing data in the next (second data). Which is how the program is looking for the most smallest data then placed on the left and then find the second smallest and most left placed second and so on in a similar manner.
Ilustrasi
Diberikan sebuah data : [8,4,7,3,1,2,6,5]
Mengurutkan bilangan atau data di atas dengan selection sort sebagaimana cara kerjanya seperti di bawah ini :
Sekiaan dan terima kasiiih... semoga bergunaa,,
amieeennn... hehhehehehe
tahankz,, :)