1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include<iostream>
using namespace std;
class OddEvenMergeSort{
public:
int size;
int a[];
public:
void print(){
for(int i=0;i<size;i++)
cin>>a[i];
}
OddEvenMergeSort(){
cout<<"shemoitanet masivis ganzomileba"<<endl;
cin>>size;
cout<<"shemoitanet masivis elementebi"<<endl;
for(int i(0);i<size;i++)
cin>>a[i];
}
~OddEvenMergeSort(){};
void Sort(){
oddEvenMergeSorti(0,size);
}
void oddEvenMergeSorti(int l,int n){
if (n>1)
{
int m=n/2;
oddEvenMergeSorti(l, m);
oddEvenMergeSorti(l+m, m);
oddEvenMerge(l, n, 1);
}
}
void oddEvenMerge(int l, int n, int r){
int m=r*2;
if (m<n)
{
oddEvenMerge(l, n, m); // even subsequence
oddEvenMerge(l+r, n, m); // odd subsequence
for (int i=l+r; i+r<l+n; i+=m)
compare(i, i+r);
}
else
compare(l, l+r);
}
void compare(int i, int j)
{
if (a[i]>a[j])
swap(i, j);
}
};
int main() {
OddEvenMergeSort A;
A.Sort();
A.print();
return 0;
}
|