void MSort(int unsorted[],int TmpArray[],int left,int right){ int Center; if(left < right){ Center =( left + right ) / 2; MSort(unsorted,TmpArray,left,Center); MSort(unsorted,TmpArray,Center + 1,right); Merge(unsorted,TmpArray,left,Center + 1,right); } }
/* 借助brr数组对arr[start...end]内的元素进行归并排序 归并排序后的顺序为从小到大 */ void MSort(int *arr,int *brr,int start,int end) { if(start < end) { int mid = (start+end)/2; MSort(arr,brr,start,mid); //左边递归排序 MSort(arr,brr,mid+1,end); //右边递归排序 Merge(arr,brr,start,mid,end); //左右序列归并 } }
void MSort( int arr[ ], int tmpArray[ ], int left, int right ) { int mid; if( left < right ) { mid = ( left + right ) / 2; MSort( arr, tmpArray, left, mid ); MSort( arr, tmpArray, mid + 1, right ); Merge( arr, tmpArray, left, mid + 1, right ); } }
void MSort(ElementType S[], ElementType TmpS[], long L, long RightEnd) { long Center; if (L < RightEnd) { Center = (L + RightEnd) / 2; MSort(S, TmpS, L, Center); MSort(S, TmpS, Center + 1, RightEnd); Merge1(S, TmpS, L, Center + 1, RightEnd); } }
void MSort(ElemType A[], ElemType TmpArray[], int Left, int Right) { int Center; if(Left < Right){ Center = (Left + Right)/2; MSort(A, TmpArray, Left, Center); MSort(A, TmpArray, Center + 1, Right); Merge(A, TmpArray, Left, Center + 1, Right); } }
//length = 10 //0 5 1 9 3 7 4 8 6 2 void MSort(int SR[], int TR1[], int s, int len) { int m; int TR2[MAX + 1]; if (s == len) TR1[s] = SR[s]; else { m = (s + len) / 2; MSort(SR, TR2, s, m); MSort(SR, TR2, m + 1, len); Merge(TR2, TR1, s, m, len); } }
void MSort(RedType SR[], RedType TR1[], int s, int t) { // 算法10.13 // 将SR[s..t]归并排序为TR1[s..t]。 int m; RedType TR2[20]; if (s==t) TR1[t] = SR[s]; else { m=(s+t)/2; // 将SR[s..t]平分为SR[s..m]和SR[m+1..t] MSort(SR,TR2,s,m); // 递归地将SR[s..m]归并为有序的TR2[s..m] MSort(SR,TR2,m+1,t); // 将SR[m+1..t]归并为有序的TR2[m+1..t] Merge(TR2,TR1,s,m,t); // 将TR2[s..m]和TR2[m+1..t]归并到TR1[s..t] } } // MSort
void MSort(RcdType SR[], RcdType TR1[], int s, int t) { int m; RcdType TR2[t - s]; if (s == t) { TR1[s] = SR[s]; } else { m = (s+t)/2; MSort(SR, TR2, s, m); MSort(SR, TR2, m+1, t); Merge(TR2, TR1, s, m, t); } }
void MSort(int SR[], int TR1[], int s, int t) { // 将SR[s..t]归并为TR1[s..t] int m; int TR2[MAXSIZE + 1]; if (s == t) TR1[s] = SR[s]; else { m = (s + t) / 2; MSort(SR, TR2, s, m); MSort(SR, TR2, m + 1, t); Merge(TR2, TR1, s, m, t); } }
void MSort(int SR[], int TR1[], int s, int t) { // 将SR[s..t]归并排序为TR1[s..t] int m; int TR2[100+10]; if (s == t) { TR1[s] = SR[s]; } else { m = (s + t) / 2; MSort(SR, TR2, s, m); MSort(SR, TR2, m + 1, t); Merge(TR2, TR1, s, m, t); } }// MSort
void MSort(int SR[], int TR1[], int s, int t) { int m; int TR2[t]; memset(TR2, 0, sizeof(int) * t); if (s == t) { TR1[s] = SR[s]; } else { m = (s + t) / 2; MSort(SR, TR2, s, m); MSort(SR, TR2, m + 1, t); Merge(TR2, TR1, s, m, t); } }
/* 将该排序算法封装起来 */ void Merge_Sort(int *arr,int len) { int *brr = (int *)malloc(len*sizeof(int)); MSort(arr,brr,0,len-1); free(brr); brr = 0; }
/* START: fig7_9.txt */ void MSort(ElementType A[], ElementType TmpArray[], int Left, int Right) { const int SPACE = 2 * sizeof(ElementType) + 2 * sizeof(int); Resource_logSpace(SPACE); int Center; //1 if (Left < Right) { //1 Center = (Left + Right) / 2; //4 Resource_logTime(5); MSort(A, TmpArray, Left, Center); MSort(A, TmpArray, Center + 1, Right); Merge(A, TmpArray, Left, Center + 1, Right); } Resource_logTime(4); Resource_logSpace(-SPACE); }
void MSort(int* A, int p, int r) { if (p >= r) { return; } int q = (p + r) / 2; MSort(A, p, q); MSort(A, q + 1, r); Merge(A, p, q, r); }
void MSort(int *arr,int start,int end,int *arr1) { int buffer[MAX]; int m; if(end>start){ m=(end+start)/2; MSort(arr,start,m,buffer); MSort(arr,m+1,end,buffer); Merge(buffer,start,m,end,arr1); }else if(end==start){ arr1[start]=arr[start]; }else{ perror("can't get here"); } }
void MSort(int src[], int des[], int low, int high, int max) { if( low == high ) { des[low] = src[low]; } else { int mid = (low + high) / 2; int* space = (int*)malloc(sizeof(int) * max); /* 辅助空间 */ if( space != NULL ) { MSort(src, space, low, mid, max); /* 左递归 */ MSort(src, space, mid+1, high, max); /* 右递归 */ Merge(space, des, low, mid, high); /* 归并拷贝 */ } free(space); } }
void MergeSort(int unsorted[],int len){ if(unsorted == NULL || len <= 0) return; int *TmpArray = (int*)malloc(sizeof(int) * len); if(TmpArray != NULL){ MSort(unsorted,TmpArray,0,len - 1); free(TmpArray); } else{ printf("No Space for tmp array!\n"); exit(1); } }
void MergeSort( int arr[ ], int len ) { int *tmpArray; tmpArray = malloc( len * sizeof( int ) ); if( tmpArray != NULL ) { MSort( arr, tmpArray, 0, len - 1 ); free( tmpArray ); } else printf( "No space for tmp array!!!" ); }
void Mergesort(ElementType A[], int N) { ElementType *TmpArray; TmpArray = malloc(N * sizeof(ElementType)); if (TmpArray != NULL) { MSort(A, TmpArray, 0, N - 1); } else FatalError("No space for tmp array!!!"); }
void MergeSort(ElementType A[], int N) { ElementType *TmpArray; TmpArray = malloc(N * sizeof(ElementType)); if (TmpArray != NULL) { MSort(A, TmpArray, 0, N-1); free(TmpArray); } else { exit(1); } }
void MergeSort(ElemType A[], int N) { ElemType *TmpArray; TmpArray = malloc(N * sizeof(ElemType)); if(TmpArray == NULL){ FatalError("Out of space!"); }else{ MSort(A, TmpArray, 0, N - 1); free(TmpArray); } }
//每次分为两路 当只剩下一个元素时,就不需要在划分 void MSort(int src[], int des[], int low, int high, int max) { if (low == high) //只有一个元素,不需要归并,结果赋给des[low] { des[low] = src[low]; } else //如果多个元素,进行两路划分 { int mid = (low + high) / 2; int* space = (int*)malloc(sizeof(int)* max); //递归进行两路,两路的划分 //当剩下一个元素的时,递归划分结束,然后开始merge归并操作 if (space != NULL) { MSort(src, space, low, mid, max); MSort(src, space, mid + 1, high, max); Merge(space, des, low, mid, high); //调用归并函数进行归并 } free(space); } }
void mergesort(ElementType A[], int N) { Resource_logSpace(SPACE_ELEMENT_INT); ElementType *TmpArray; //1 TmpArray = malloc(N * sizeof(ElementType)); Resource_logSpace(N * sizeof(ElementType)); if (TmpArray != NULL) { //1 MSort(A, TmpArray, 0, N - 1); free(TmpArray); //1 Resource_logTime(1); Resource_logSpace(-N * sizeof(ElementType)); } else fatalError( "No space for tmp array!!!" ); Resource_logTime(4); Resource_logSpace(-SPACE_ELEMENT_INT); }
int main() { srand((unsigned)time(NULL)); int A[COUNT]; int i; for (i = 0; i < COUNT; i++) A[i] = rand() % 100; printf("before sort.....\n"); print(A); MSort(A, COUNT); printf("\nafter sort......\n"); print(A); return 0; }
void UpdateMedian(char *state, double ts, double value) { struct median_state *s = (struct median_state *)state; int curr_size; curr_size = F_COUNT(s->series); if(curr_size > s->M_size) { s->M_count = curr_size = s->M_size; } else { s->M_count = curr_size; } /* * update the sorted list */ /* * increment the artificial time stamp */ s->artificial_time = s->artificial_time + 1; /* * use artificial time stamp instead of real one to * keep things in terms of entries instead of seconds */ MSort(s->M_array,s->M_ts,value,s->artificial_time,curr_size); return; }
void MergeSort(int *arr,int n) { MSort(arr,0,n-1,arr); }
void MergeSort(SqList *L) { if (L == NULL) return; MSort(L->r, L->r, 1, L->length); }
void MergeSort(SqList *L) { MSort(L->r, L->r, 1, L->length); }
void MergeSort(int array[], int len) // O(n*logn) { MSort(array, array, 0, len - 1, len); }
void _main(void) { char Line[255] ; char Chose ; do { //2012: lock the interrupt sys_disable_interrupt(); cprintf("\n"); cprintf("!!!!!!!!!!!!!!!!!!!!\n"); cprintf("!!!! MERGE SORT !!!!\n"); cprintf("!!!!!!!!!!!!!!!!!!!!\n"); cprintf("\n"); readline("Enter the number of elements: ", Line); int NumOfElements = strtol(Line, NULL, 10) ; int *Elements = malloc(sizeof(int) * NumOfElements) ; cprintf("Chose the initialization method:\n") ; cprintf("a) Ascending\n") ; cprintf("b) Descending\n") ; cprintf("c) Semi random\n"); cprintf("Select: ") ; Chose = getchar() ; cputchar(Chose); cputchar('\n'); //2012: lock the interrupt sys_enable_interrupt(); int i ; switch (Chose) { case 'a': InitializeAscending(Elements, NumOfElements); break ; case 'b': InitializeDescending(Elements, NumOfElements); break ; case 'c': InitializeSemiRandom(Elements, NumOfElements); break ; default: InitializeSemiRandom(Elements, NumOfElements); } MSort(Elements, 1, NumOfElements); cprintf("Sorting is Finished!!!!it'll be checked now....\n") ; //PrintElements(Elements, NumOfElements); uint32 Sorted = CheckSorted(Elements, NumOfElements); if(Sorted == 0) panic("The array is NOT sorted correctly") ; else { cprintf("===============================================\n") ; cprintf("Congratulations!! The array is sorted correctly\n") ; cprintf("===============================================\n\n") ; } free(Elements) ; cprintf("Do you want to repeat (y/n): ") ; Chose = getchar() ; cputchar(Chose); cputchar('\n'); } while (Chose == 'y'); }