Selasa, 05 Juli 2011

Selection Sort

Program selection sort is the sort of data input is not initially be a sequential or random order according to which the largest numbers in a way menggesar large numbers left and a smaller number will be shifted to the right by comparing one by one from the initial input sequence, to obtain a sequential and a the smallest numbers on the right and followed a more Basar and continue that way until the largest number corresponding input previously entered, then the sort will stop by itself when it is sequential.

#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;
 //Input Data
 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<<"\n\n";
 //tampilkan data      
 cout<<"Data Setelah di Sort : ";
 for(int i=1; i<=n; i++){
  cout<<" "<<data[i];}
 cout<<"\n\n Selection Sort Selesai";
 getch();}
 

Tidak ada komentar:

Posting Komentar