Algoritma :
Deklarasi
i,j : integer
pos : integer
data[ ] : 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 :
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 x, int y){
int z;
z=data[y];
data[y]=data[x];
data[x]=z;}
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);
if(pos != i) tukar(pos,i);
}
}
int main(){
cout<<"Selection Sort Program";
cout<<"\nInputkan Banyak data : ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<"Data ke-"<<i<<" : ";
cin>>data[i];
data2[i]=data[i];
int main(){
cout<<"Selection Sort Program";
cout<<"\nInputkan Banyak 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 Dilakukan Penyeleksian : ";
for(int i=1; i<=n; i++){
cout<<" "<<data[i];
selection();
cout<<"Data setelah Dilakukan Penyeleksian : ";
for(int i=1; i<=n; i++){
cout<<" "<<data[i];
}
getch();
return 0;
getch();
return 0;
}
Tidak ada komentar:
Posting Komentar