From the course: Complete Guide to Parallel and Concurrent Programming with C++

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Solution: Merge sort

Solution: Merge sort

(upbeat music plays) - [Instructor] For our solution to the merge_sort challenge, we used a recursive divide and conquer approach with multiple threads sorting subsections of the overall array. For the divide phase, rather than recursively subdividing the array until it reaches single elements, we've first configured our base case to subdivide the array based on the number of processors on the computer. For example, if the computer only had four processors, then it would only go through two layers of subdivisions to produce four sub arrays in need of sorting. We then give each of the processors a separate thread executing the sequential merge_sort algorithm to sort each sub array in place. And then the main processor merges the results back together as the recursion unwinds, and each of the other threads finishes sorting their sub arrays. By limiting the depth of recursion in our base case, we're able to use a few threads to sort large sections of the array, rather than spawning a…

Contents