Shorting dengan metode Selection C++

Posted by Didi Setyapramana On 6:16 PM 0 komentar


#include <iostream.h>
#include <conio.h>

class Sorting{
      friend istream& operator>>(istream& in, Sorting& a);
      friend ostream& operator<<(ostream& out, Sorting& a);
public:
       void selection_sort();
       int pencarianBiner(int low, int high);
       void minimum(int, int, int&);
        void tukar(int& , int&);
private:
        
        int data [10];
        int n,a,b;
};

istream& operator>>(istream& in, Sorting& masukan){
         cout<<"Banyak data : \n";
         in>>masukan.n;
         for(int i=0; i<masukan.n; i++){
              cout<<"Data ke - "<<i+1<<" = ";
              in>>masukan.data[i];
              }
              return in;
         }  

ostream& operator<<(ostream& out, Sorting& keluaran){
        for(int i=0; i<keluaran.n; i++){
              out<<keluaran.data[i]<<" ";
              out<<"\n";
              }
              return out;
         }  
                 

void Sorting::minimum(int dari, int n, int &tempat){
     int min=data[dari];
     //int max=data[dari];
     for(int i= dari+1; i<n; i++)
     if(data[i] < min){
                min=data[i];
                tempat=i;
                }
     
     /* if(data[i] > max){
                max=data[i];
                cout<<max;
                }*/
     } 

void Sorting::tukar(int &a, int &b){
     int temp=a;
     a=b;
     b=temp;
     }

void Sorting::selection_sort(){
     int t;
     for(int i=0; i<n; i++){
             minimum(i,n,t);
             tukar(data[i],data[t]);
     }
}


int main(){
    Sorting x;
    cin>>x;
    cout<<endl;
    cout<<x;
    x.selection_sort();
    cout<<endl;
    cout<<x;
    getch();
    return 0;
}
    

0 Response for the "Shorting dengan metode Selection C++"

Post a Comment