常用排序算法—Merge Sort(归并排序)
2013-03-21 22:41
399 查看
Merge sort is based on Divide and conquer method. It takes the list to be sorted and divide it in half to create two unsorted lists. The two unsorted lists are then sorted and merged to get a sorted list. The two unsorted lists are sorted by continually calling the merge-sort algorithm; we eventually get a list of size 1 which is already sorted. The two lists of size 1 are then merged.
![](http://images.cnitblog.com/blog/445835/201303/21224202-b66a7c4e7f8e4bd2a0a829260b376900.png)
Worst case – When the array is sorted in reverse order O(nlogn).
Average case – O(nlogn).
Extra space is required, so space complexity is O(n) for arrays and O(logn) for linked lists.
![](http://images.cnitblog.com/blog/445835/201303/21224202-b66a7c4e7f8e4bd2a0a829260b376900.png)
/* Logic: This is divide and conquer algorithm. This works as follows. (1) Divide the input which we have to sort into two parts in the middle. Call it the left part and right part. Example: Say the input is -10 32 45 -78 91 1 0 -16 then the left part will be -10 32 45 -78 and the right part will be 91 1 0 6. (2) Sort Each of them seperately. Note that here sort does not mean to sort it using some other method. We already wrote fucntion to sort it. Use the same. (3) Then merge the two sorted parts.*/
/*This function Sorts the array in the range [left,right]. That is from index left to index right inclusive */ void MergeSort(int *array, int left, int right){ int mid = (left+right)/2; /* We have to sort only when left<right because when left=right it is anyhow sorted*/ if(left<right) { /* Sort the left part */ MergeSort(array,left,mid); /* Sort the right part */ MergeSort(array,mid+1,right); /* Merge the two sorted parts */ Merge(array,left,mid,right); } } /* Merge functions merges the two sorted parts. Sorted parts will be from [left, mid] and [mid+1, right]. */ void Merge(int *array, int left, int mid, int right) { /*We need a Temporary array to store the new sorted part*/ int tempArray[right-left+1]; int pos=0,lpos = left,rpos = mid + 1; while(lpos <= mid && rpos <= right) { if(array[lpos] < array[rpos]) { tempArray[pos++] = array[lpos++]; } else { tempArray[pos++] = array[rpos++]; } } while(lpos <= mid) tempArray[pos++] = array[lpos++]; while(rpos <= right) tempArray[pos++] = array[rpos++]; int iter; /* Copy back the sorted array to the original array */ for(iter = 0;iter < pos; iter++) { array[iter+left] = tempArray[iter]; } return; }
Properties:
Best case – When the array is already sorted O(nlogn).
Worst case – When the array is sorted in reverse order O(nlogn).
Average case – O(nlogn).
Extra space is required, so space complexity is O(n) for arrays and O(logn) for linked lists.
相关文章推荐
- 归并排序(Merge sort)
- 归并排序(MergeSort)
- Natural Merge Sort(自然归并排序)
- 单链表的排序 快速排序 归并排序 quicksort mergesort
- C:C的排序算法:归并排序(MergeSort)
- 归并排序 (Merge Sort)
- 归并排序(Merge Sort)
- 归并排序(Merge Sort)
- 归并排序(MergeSort)
- Java排序算法(三)--归并排序(MergeSort)递归与非递归的实现
- 归并排序(Merge Sort)
- (2011.12.03) 07_归并排序(mergesort).cpp
- 自顶向下归并排序(Merge Sort)
- 排序算法之归并排序(Mergesort)解析
- 归并排序(Merge Sort)递归、非递归 Java实现
- 归并排序(2)MergeSort顺序实现之非预分配内存
- C++ Merge sort(归并排序)
- 【算法导论学习-002】归并排序(MergeSort)
- 排序算法——归并排序(Merge Sort)
- 归并排序(merge sort)