Selection Sort

Selection sort adalah mengurutkan data array 1 dimensi dengan menggunakan pembanding terkecil dan terbesar yaitu variabel min dan max, kemudian di urutkan dari terkecil sampai terbesar. Berikut code selengkapnya :

 

#include <cstdlib>
#include <iostream>

using namespace std;
      class Sorting {//class sorting
            public ://deklrasi fungsi
                   Sorting (){
                    cout<<"Program sorting"<<endl<<endl;
                    }
                   void input();
                   void selection_sort();
                   void cetak();
                   void tukar(int&a, int&b);
                   void maksmin(int, int, int&, int&);
                   
            private ://deklrasi variabel
                    int i, j, n;
                    int x[100];
            };//akhir class
            
      void Sorting::input(){//deskripsi fungsi input
           cout<<"Banyak data : ";
           cin>>n;
           for(i = 0; i < n; i++){ //perulangan for
                 cout<<"Data ke-"<<(i + 1)<<" : ";
                 cin>>x[i]; //input array 1 dimensi
                 }
           
           }
      void Sorting::cetak(){ //deskripsi fungsi cetak
           for(i = 0; i < n; i++){ //perulangan for
                 cout<<x[i]<<" ";
                 }
           cout<<endl;
           }
      void Sorting::tukar(int&a, int&b){ //deskripsi fungsi tukar
           int temp;
           temp=a;
           a=b;
           b=temp;
           }
           
       void Sorting::selection_sort(){ //deskripsi fungsi sort
           int posmin, posmaks;
           int i = 0;
           int j = n-1;
           while(i <= j){ //perulangan while
                   maksmin(i,j, posmin,posmaks);
                   tukar(x[i], x[posmin]);
                   maksmin(i,j, posmin,posmaks);
                   tukar(x[j], x[posmaks]);
                   i++;
                   j--;
                   }         
           } 
           //deskripsi fungsi maxmin
      void Sorting :: maksmin(int awal, int akhir, int & posmin, int & posmaks){
           int maks=x[akhir];
           int min=x[awal];
           posmin = awal;
           posmaks = akhir;
           for(i = awal; i<= akhir; i++){
                    if(x[i] > maks){
                            maks=x[i];
                            posmaks = i;
                            }
                    if(x[i]< min){
                             min = x[i];
                             posmin = i;
                             }
                    }
           }
int main()
{
    Sorting a;
    a.input();
    cout<<"\nData sebelum disorting :"<<endl;
    a.cetak();
    a.selection_sort();
    cout<<"\nSorting menggunakan selection sort : "<<endl;
    a.cetak();
    cout<<"\n\n";
    
    system("PAUSE"); system("cls");
    return main ();
}

 

Hasil running :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: