Sorting With Diminishing Increment

Sorting With Diminishing Increment adalah sorting atau pengurutan dengan penambahan atau pengurangan. Berikut code selengkapnya :

#include <cstdlib>
#include <iostream>

using namespace std;

class sorting{
      friend istream& operator>>(istream&, sorting&);
      friend ostream& operator<<(ostream&, sorting&);

      public:
             sorting();
             void diminishing_increment();

      private:
              int inc;
              int current;
              int previous;
              int data[100];
              int k,x,n;
              int inserted;
      };

istream& operator>>(istream& in, sorting& a){
         cout<<"Banyak data : ";
         in>>a.n;
         for(int i=1;i<=a.n;i++){
                 cout<<"Data ke-"<<(i+1)<<" : ";
                 in>>a.data[i];
                 }
         cout<<"\n\nData sebelum diurutkan :"<<endl;
         for(int i=1;i<=a.n;i++){
                 cout<<a.data[i]<<" ";
                 }
         cout<<"\n\n";
         return in;
         }

ostream& operator<<(ostream& out, sorting& b){
         out<<"\n\nData setelah diurutkan :"<<endl;
         for(int i=1;i<=b.n;i++){
                 out<<b.data[i]<<" ";
                 }
         out<<"\n\n";
         return out;
         }

sorting::sorting(){
                   cout<<"Sorting by Diminishing Increment"<<"\n\n";
                   }

void sorting::diminishing_increment(){
     inc = n;
     while(inc>1){
     inc = inc /2;
      for(int i=1;i<=inc;i++){
       k = i + inc;
     while(k<=n){
      inserted = 0;
  x = data[k];
  current = k;
   previous = current - inc;
  while((previous>=i)&&(inserted == 0)){
  if(x<data[previous]){
   data[current]=data[previous];
 current=previous;
 previous=previous-inc;
     }
 else inserted = 1;
   }
   data[current]=x;
   k = k + inc;
   }
   }
  }
     }

int main(int argc, char *argv[])
{
    sorting dyas;
    cin>>dyas;
    dyas.diminishing_increment();
    cout<<dyas;

    system("PAUSE");
    return EXIT_SUCCESS;
}

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: