标签:归并排序
void merge_array(int list1,int list1_size,int list2,int list2_size) { int i,j,k; i=j=k=0; //声明临时数组用暂存归并结果 int list[list1_size + list2_size]; while(i < list1_size && j < list2_size) list[k++] = list1[i] <= list2[j] ? list1[i++] : list2[j++]; while(i < list1_size) list[k++] = list1[i++]; while(j < list2_size) list[k++] = list2[j++]; for(int ii = 0;ii < list1_size + list2_size;++ii) list1[ii] = list[ii]; } void merge_sort(int *list,int list_size) { if(list_size > 1) { //把数组平均分成两份 int *list1 = list; int list1_size = list_size / 2; int *list2 = list + list_size / 2; int list2_size = list_size - list1_size; <pre name="code" class="html"> <span style="white-space:pre"> </span>merge_sort(list1, list1_size); merge_sort(list2,list2_size);
<span style="white-space:pre"> </span> merge_array(list1,list1_size,list2,list2_size); } }
标签:归并排序
原文地址:http://blog.csdn.net/u012118523/article/details/36218247