十几年前注册的账户,突然有一天收到邮件,居然还能用🎉🎉
不过在发布时,居然提示质量不高,可能涉及广告,缺少代码
下面增加一段代码已表示敬意
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void merge_sort_recursive(int in[], int sorted[], int start, int end)
{
int start1,end1;
int start2,end2;
int mid;
int k;
int len;
/* single value is naturally ordered */
if (start >= end) {
return;
}
/* divide into 2 part */
len = end - start;
mid = (len>>1) + start;
start1 = start;
end1 = mid;
start2 = mid+1;
end2 = end;
/* recursive function call */
merge_sort_recursive(in, sorted, start1, end1);
merge_sort_recursive(in, sorted, start2, end2);
/* combination */
k = start;
while (start1 <= end1 && start2 <= end2){
sorted[k++] = in[start1] < in[start2] ? in[start1++] : in[start2++];
}
while (start1 <= end1) {
sorted[k++] = in[start1++];
}
while (start2 <= end2) {
sorted[k++] = in[start2++];
}
for(k=start;k<=end;k++) {
in[k] = sorted[k];
}
}
int main(void)
{
int arry[] = {-1,14,25,10,9,9,3,3,3,3,-7,-7,2,23,2,14,10,9,123,-10,0,1,9};
int nums = sizeof(arry)/sizeof(arry[0]);
int i;
printf("-------------------<Test merge sort algrithm>------------------------\n");
printf("test arry origin: num=%d\n", nums);
for (i=0; i<nums; i++) {
printf("%d ", arry[i]);
}
printf("\n");
int *tmparry = (int *)malloc(sizeof(int) * nums);
memset(tmparry, 0, sizeof(int) * nums);
merge_sort_recursive(arry,tmparry, 0, nums-1);
printf("test arry sorted: num=%d\n", nums);
for (i=0; i<nums; i++) {
printf("%d ", arry[i]);
}
printf("\n");
printf("-------------------<Test bubble sort algrithm>------------------------\n");
free(tmparry);
return 0;
}