Non-recursive version of merge sort, omitting the middle of the stack space, direct application period O (n) to the address space, so the space complexity is O (n), the time complexity is O (nlogn);
Algorithm idea:
For a start at intervals of merging, that is, the first element merge with the second one. The third and fourth for merging;
Then, at intervals of 2 to perform merging, merging 1-4, 5-8 merge;
And then at intervals of 2 * 2. Similarly, up to know more than the array length 2 * k.
while(i<length){
Merge(arr,temp,i,length);
i *= 2;
}
When the two groups merge is not enough, if more than k elements, still be merged; if the remaining elements of no more than k elements, then copied directly to the middle of the array.
while(i <= length-2*k){
sort(arr1,temp,i,i+k-1,i+2*k-1);
i += 2*k;
}
if(i < length-k+1)//如过剩余个数比一个k长度还多...那么就在进行一次合并
sort(arr1,temp,i,i+k-1,length-1);
else
for(j=i;j<length;j++)
temp[j] = arr1[j];
The main code:
void MergeSort(int *arr,int length){
int *temp = (int *)malloc(sizeof(int)*length);
int i = 1;
while(i<length){
Merge(arr,temp,i,length);
i *= 2;
}
}
void Merge(int *arr1,int *temp,int k,int length){
int i = 0,j;
while(i <= length-2*k){
sort(arr1,temp,i,i+k-1,i+2*k-1);
i += 2*k;
}
if(i < length-k+1)//如过剩余个数比一个k长度还多...那么就在进行一次合并
sort(arr1,temp,i,i+k-1,length-1);
else
for(j=i;j<length;j++)
temp[j] = arr1[j];
for(i=0;i<length;i++){
arr1[i] = temp[i];
}
}
void sort(int *arr3,int *arr1,int begin,int m,int end){
int i=begin,j=m+1,k,h;
for(k=i; i<=m && j<=end;k++){
if(arr3[i] < arr3[j])
arr1[k] = arr3[i++];
else
arr1[k] = arr3[j++];
}
if(i <= m){
for(h=0; h<=m-i;h++)
arr1[k+h] = arr3[i+h];
}else{
for(h=0; h<=end-j;h++)
arr1[k+h] = arr3[j+h];
}
}
All Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int arrtest1[10] = {4,3,7,8,0,9,1,2,6,5};
int arrtest2[10] = {0,1,2,3,4,5,6,7,8,9};
int arrtest3[10] = {9,8,7,6,5,4,3,2,1,0};
void copy(int *from,int *arr,int length);
void print(int *arr,int length);
void MergeSort(int *arr,int length);
void Merge(int *arr1,int *temp,int k,int length);
void sort(int *arr3,int *arr1,int begin,int m,int end);
int main(){
int Arr[10],i;
copy(arrtest1,Arr,10);
print(Arr,10);
MergeSort(Arr,10);
print(Arr,10);
getchar();
return 0;
}
void MergeSort(int *arr,int length){
int *temp = (int *)malloc(sizeof(int)*length);
int i = 1;
while(i<length){
Merge(arr,temp,i,length);
i *= 2;
}
}
void Merge(int *arr1,int *temp,int k,int length){
int i = 0,j;
while(i <= length-2*k){
sort(arr1,temp,i,i+k-1,i+2*k-1);
i += 2*k;
}
if(i < length-k+1)//如过剩余个数比一个k长度还多...那么就在进行一次合并
sort(arr1,temp,i,i+k-1,length-1);
else
for(j=i;j<length;j++)
temp[j] = arr1[j];
for(i=0;i<length;i++){
arr1[i] = temp[i];
}
}
void sort(int *arr3,int *arr1,int begin,int m,int end){
int i=begin,j=m+1,k,h;
for(k=i; i<=m && j<=end;k++){
if(arr3[i] < arr3[j])
arr1[k] = arr3[i++];
else
arr1[k] = arr3[j++];
}
if(i <= m){
for(h=0; h<=m-i;h++)
arr1[k+h] = arr3[i+h];
}else{
for(h=0; h<=end-j;h++)
arr1[k+h] = arr3[j+h];
}
}
void copy(int *from,int *arr,int length){
int i;
for(i=0;i<length;i++){
arr[i] = from[i];
}
}
void print(int *arr,int length){
int i;
for(i=0;i<length;i++){
printf("%d ",arr[i]);
}
printf("\n");
}
Running instance:
Reproduced in: https: //my.oschina.net/u/204616/blog/545448