C语言 归并排序

来源:互联网 发布:天命神童 知乎 编辑:程序博客网 时间:2024/05/22 12:44

《数据结构与算法分析 C语言版》


上 归并排序的实现,注释 有空加- -

#include<stdio.h>void MSort(ElementType A[],ElementType TempArray[],int Left,int Right){int Center;if(Left<Right){Center=(Left+Right)/2;MSort(A[],TempArray[],0,Center);MSort(A[],TempArray[],Center+1,Right);Merge(A,TempArray[],Left,Center+1,Right);}}void Merge(ElementType A[],Element TempArray[],int LeftStart,int RightStart,int RightEnd){int pos_l=LeftStart;int pos_r=RightStart;int temp=LeftStart;while(pos_1<RightStart&&pos_r<RightEnd+1){if(A[point_l]<A[poing_r]){TempArray[temp++]=A[pos_l++];}else{TempArray[temp++]=A[pos_r++];}}while(pos_1<RightStart)TempArray[temp++]=A[pos_l++];while(pos_r<RightEnd+1)TempArray[temp++]=A[pos_r++];for(int i=0;i<temp;i++)A[i]=TempArray[i];}void Mergesort(ElementType A[],int N){ElementType *TempArray;TempArray=(ElementType*)malloc(N*sizeof(ElementType));MSort(A,TempArray,0,N-1);free(TempArray);}


原创粉丝点击