PHP Code:
#include <iostream>
using namespace std;
void x_merge (int * first_array, int size1, int * second_array, int size2){
int i,j,k;
int * third_array = new int [size1 + size2];
for(i=0, j=0, k=-1 ; (i<size1) && (j<size2) ;){
if ( *(first_array + i) <= *(second_array + j) ){
++k;
*(third_array + k) = *(first_array + i);
++i;
} else {
++k;
*(third_array + k) = *(second_array + j);
++j;
}
}
if( i>= size1 ){
for(;j<size2;){
++k;
*(third_array + k) = *(second_array + j);
++j;
}
} else {
for(;i<size1;){
++k;
*(third_array + k) = *(first_array + i);
++i;
}
}
for(i=0; i<size1 ;++i){
*(first_array + i) = *(third_array + i);
}
j=i;
for(i=0; i<size2 ;++i){
*(second_array + i) = *(third_array + i + j);
}
delete [] third_array ;
}
void x_mergesort(int * array, int size){
if (size == 2){
int a,b,c;
a=*array;
b=*(array + 1 );
if (b < a ){
c= *array;
*array = b;
*(array +1) = c;
}
}else{
if (size > 2){
x_mergesort( array, (size/2));
x_mergesort(array + (size/2),size - (size/2));
x_merge(array, (size/2), array + (size/2), size - (size/2));
}
}
}
void x_print (int * array, int size){
int i;
for(i=0;i<size;++i){
cout << * (array + i) << endl;
}
}
int main () {
int numeri [] = {1,10,5,3,345,2,33,22,44,33,1,2,4,2,5,8,3,9,4,445,22,334,54};
x_mergesort(numeri,23);
x_print(numeri,23);
cin.get();
return 0;
}