Go language Implementation and optimization of merge sort

Source: Internet
Author: User

To view the full code, click here

Do not understand the merger sort can be viewed Baidu encyclopedia analysis

Implementation of merge sort

Basic implementation

Package Mainimport "FMT"//merge [L,r] two parts of data, mid left half end, Mid + 1 is the beginning of the right half of the func merge (arr []int, L int, mid int, r int) {// Because the ARR data needs to be modified directly, the data from [L,r] is copied to the new array first, which is used to assign the operation temp: = Make ([]int, r-l+1) for I: = l; I <= R; i++ {Temp[i-l] = Arr[i]}//point to two-part starting point left: = L: = mid + 1 for i: = l; I <= R;  i++ {///left point exceeds midpoint, indicating only the right data if left > mid {arr[i] = Temp[right-l] right++// The data on the right exceeds the end point, indicating that only the left data is left} else if right > r {arr[i] = Temp[left-l] left++//Data on the left is greater than        The data, select the small number} else if temp[left-l] > Temp[right-l] {arr[i] = Temp[right-l] right++     } else {Arr[i] = temp[left-l] left++}}}func mergesort (arr []int, L int, r int) { If L >= r {return}//Recursive down Mid: = (R + L)/2 mergesort (arr, L, mid) mergesort (arr, mid+1, R)//merge up merge (arr, L, Mid, R)}func main () {    Arr: = []int{3, 1, 2, 5, 6,, 4} mergesort (arr, 0, Len (arr)-1) fmt. Println (ARR)}

Optimization point

    • Merging is only required if the maximum value of the left data is greater than the minimum value of the right data

      For example, if the left and right parts of the merge are now [1,3] and [4,5], then no "and" operations are required. [1,4] and [3,5] need to "and" for [1,3,4,5]

    • When the binary data goes to a certain stage, you can use the Insert sort instead of continuing the downward two points

      Although Nlogn is always greater than n^2 in complexity, the constant entry is ignored, and the merged constant is greater than the insert sort, so when n is enough, the insertion sort is faster

      The following are program changes that combine the optimization points:

Package main import "FMT" func merge (arr []int, L Int., mid int, r int) {temp: = make ([]int, r-l+1) for I: = L ; I <= R; i++ {Temp[i-l] = Arr[i]} Left: = L: = mid + 1 for i: = l; I <= R;            i++ {if left > mid {arr[i] = temp[right-l] right++} else if right > R { Arr[i] = temp[left-l] left++} else if temp[left-l] > Temp[right-l] {arr[i] = t EMP[RIGHT-L] right++} else {arr[i] = Temp[left-l] left++}}} F         UNC MergeSort (arr []int, L int, r int) {///second step optimization, when the data size is small enough, you can use the Insert sort if r-l <= 15 {//To perform an insert sort on l,r data For I: = l + 1; I <= R;                i++ {temp: = Arr[i] J: = I for, j > 0 && Temp < arr[j-1]; j--{ ARR[J] = arr[j-1]} arr[j] = temp} return} Mid: = (R + L)/2   MergeSort (arr, L, mid) mergesort (arr, mid+1, R)//First step optimization, the left and right parts have been ordered, only if the left maximum value is greater than the minimum value on the right, the merge operation is required for the two parts if    Arr[mid] > Arr[mid + 1] {merge (arr, L, Mid, R)}} Func main () {arr: = []int{3, 1, 2, 5, 6, 43, 4} MergeSort (arr, 0, Len (arr)-1) fmt. Println (ARR)}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.