selection with diminishing increment is one-dimensional array sorting by using the method of addition and subtraction array index. sorce code follows the program more :
#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;
}
sorce code running result :