Merge Sort in Go (Golang)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Merge Sort was invented by John von Neumann in 1945, and is an algorithm belonging to the “divide and conquer” class of algorithms. Meaning, the algorithmsplits an input into various pieces, sorts them and then merges them back together.

Runtime of Merge Sort

If we rephrase this algorithm:

  1. Divide the array into various “chunk” to process
  2. Merge the array while sorting (conquer)

Below I’ll provide a very basic example:

Input:   {4, 1, 5, 8, 9, 3, 2, 6, 7}
Divide: {4, 1, 5, 8}, {9, 3, 2, 6, 7}
Divide: {4, 1}, {5, 8}, {9, 3}, {2, 6}, {7}
Divide: {4}, {1}, {5}, {8}, {9}, {3}, {2}, {6}, {7}

Merge: {1, 4}, {5, 8}, {3, 9}, {2, 6}, {7}
Merge: {1, 4, 5, 8}, {2, 3, 6, 9}, {7}
Merge: {1, 2, 3, 4, 5, 6, 8, 9}, {7}
Merge: {1, 2, 3, 4, 5, 6, 7 ,8, 9}

In the above, you can see that I bold when we alter the position of a variable. It is clear that by dividing first it is easier to merge in order because we have to change positions less often, than say brute forcing. Brute forcing would be much slower because we would in the worst case have to search though the whole array each time and copy/move each value into a new array/flip positions. Thereby giving a brute force sorting algorithm a runtime of O(n^2).

Merge sort on the other hand is significantly quicker at sorting and further, can actually be implemented (more easily) in parallel (across multiple CPUs). Merge Sort in big-o notation, where n is the size of the array, as the following:

  • Runtime: O(nlogn)
  • Memory: O(n)

Implementing Merge Sort in Go

Merge Sort is a fairly quick algorithm and can often be implemented in parallel, however I chose to not implement a parallelized version in Go[1]. That being said, implementing the algorithm is really straight forward.

First, the input array needs to be divided into chucks to be sorted. This can be achieved in Go by finding the middle of the array (or slice in Go), and taking a slice from the left and a slice from the right and recursively call MergeSort(array) until there is only one node in the array.

// Runs MergeSort algorithm on a slice singlefunc MergeSort(array []int) []int {if len(array) < 2 {return array}mid := (len(array)) / 2return Merge(MergeSort(array[:mid]), MergeSort(array[mid:]))}

As you can see, I recursively call Mergeort(slice[:mid]) and  mergeSort(slice[mid:]), which represent the left and right portions of the array, or slice in the case of Go. Tomerge, we must also sort, to do this we make a new slice (also avoiding destroying the original, unsorted slice).  We then keep track of the left and right slices to be merged and just pick the next smallest value from the left or right slice to add to our new slice.  Think of it as picking the next best value from two piles, all the way down, until you have only one pile.

// Merges left and right slice into newly created slicefunc Merge(left, right []int) []int {size, i, j := len(left)+len(right), 0, 0slice := make([]int, size, size)for k := 0; k < size; k++ {if i > len(left)-1 && j <= len(right)-1 {slice[k] = right[j]j++} else if j > len(right)-1 && i <= len(left)-1 {slice[k] = left[i]i++} else if left[i] < right[j] {slice[k] = left[i]i++} else {slice[k] = right[j]j++}}return slice}



The output of each sorted slice feeding into the next recurrence, with the final, sorted slice, being returned to the main loop.

An example run of the program:

— unsorted —

[56843 -76716 -42042 30846 50290 -27458 47259 91477 6775 59768 15476 -21788 -33386 42947 -27244 -45848 -7689 14298 -41118 -99082 -3641 -27944 -59233 25235 -44990 56287 -13288 8899 2579 -28954 27772 41360 34462 8174 53027 -22394 -91773 -42887 14202 30264 42811 -43774 -3273 11149 27266 -32695 31350 -5934 30010 -32840]

— sorted —

[-99082 -91773 -76716 -59233 -45848 -44990 -43774 -42887 -42042 -41118 -33386 -32840 -32695 -28954 -27944 -27458 -27244 -22394 -21788 -13288 -7689 -5934 -3641 -3273 2579 6775 8174 8899 11149 14202 14298 15476 25235 27266 27772 30010 30264 30846 31350 34462 41360 42811 42947 47259 50290 53027 56287 56843 59768 91477]

That’s it!

The full program is available on my Github.

[1] Originally, I attempted to use goroutines with channels to implement it and it was awful. My assumption being goroutines do not exactly work in the same way threads do in C and I do not have enough knowledge of Go to implement a parallized merge sort in Go. That being said, below I have a fairly simplistic single thread Go program.

Related Articles

  • Pancake Sort: Everyday Algorithms
  • Using SVD to Obtain Regression Lines
  • Introduction to Markov Processes (a.k.a. Markov Chains)
  • Counting Sort in C
  • Abstract Libraries in Go
轉自: http://austingwalters.com/merge-sort-in-go-golang/?utm_source=tuicool


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.