【十二题解】 | #图片整理#(归并排序)
图片整理
http://www.nowcoder.com/practice/2de4127fda5e46858aa85d254af43941
归并真的是又简单又快
#include<stdio.h>
void merge(chara, chartmp, int l, int r, int rightend){
int lenth = rightend-l+1;
int leftend = r-1;
int loc = l;
while(l<=leftend && r<=rightend){
if(a[l]>a[r]){
tmp[loc++]=a[r++];
}
else{
tmp[loc++]=a[l++];
}
}
while(l<=leftend)tmp[loc++]=a[l++];
while(r<=rightend)tmp[loc++]=a[r++];
while(lenth!=0){
a[rightend--]=tmp[rightend];
lenth--;
}
}
void mort(chara, chartmp, int l , int rightend){
int center;
if(l<rightend){
center = (rightend+l)/2;
mort(a, tmp, l, center);
mort(a, tmp, center+1, rightend);
merge(a, tmp, l, center+1, rightend);
}
}
void mort_sort(char*a, int number){
char*tmp = (char*)malloc(sizeof(char)*number);
mort(a, tmp, 0, number-1);
}
int main(){
char data[1000]={0};
while(scanf("%s", data) != -1){
int lenth;
for(lenth = 0; data[lenth]!='\0'; lenth++);
mort_sort(data, lenth);
for (int i=0; i<lenth; i++){
printf("%c", data[i]);
}
printf("\n");
}
}