Algoritma dan Program Selection Sort

Posted in
Algoritma :
Deklarasi
    i,j           : integer
    pos       : integer
    data[]    : integer
Deskripsi
    for i <- 1 to n-1 do
        pos <- i
        for j <- i+1 to n do
            if (data[j] < data[pos]) then
            pos <- i
            end if
        end for
        if (pos != i) then
        tukar (pos,i)
        end if
    end for
    end
           
Program :
#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(){
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);}}
int main(){
cout<<"Selection Sort";
cout<<"\nBanyak data : ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<"Data ke-"<<i<<" : ";
cin>>data[i];
data2[i]=data[i];}
selection();
cout<<"Data setelah di sort : ";
for(int i=1; i<=n; i++){
cout<<" "<<data[i];}
getch();
return 0;}