Merge Sort is a popular sorting technique which divides an array or list into two halves and then start merging them when sufficient depth is reached. Time complexity of merge sort is O(nlogn).
Threads are lightweight processes and threads shares with other threads their code section, data section and OS resources like open files and signals. But, like process, a thread has its own program counter (PC), a register set, and a stack space.
Multi-threading is way to improve parallelism by running the threads simultaneously in different cores of your processor. In this program, we’ll use 4 threads but you may change it according to the number of cores your processor has.
Input : 83, 86, 77, 15, 93, 35, 86, 92, 49, 21, 62, 27, 90, 59, 63, 26, 40, 26, 72, 36 Output : 15, 21, 26, 26, 27, 35, 36, 40, 49, 59, 62, 63, 72, 77, 83, 86, 86, 90, 92, 93 Input : 6, 5, 4, 3, 2, 1 Output : 1, 2, 3, 4, 5, 6
Note* It is better to execute the program in linux based system.
To compile in linux System :
g++ -pthread program_name.cpp
Sorted array: 15 21 26 26 27 35 36 40 49 59 62 63 72 77 83 86 86 90 92 93 Time taken: 0.001023
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Merge Sort with O(1) extra space merge and O(n lg n) time
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Quick Sort vs Merge Sort
- Merge Sort vs. Insertion Sort
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- Count of smaller elements on right side of each element in an Array using Merge sort
- 3D Visualisation of Merge Sort using Matplotlib
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Visualization of Merge sort using Matplotlib
- Multithreading in C
- Multithreading in C++
- Merge Sort for Linked Lists
- Iterative Merge Sort
- Merge Sort for Doubly Linked List
- Find a permutation that causes worst case of Merge Sort
- Concurrent Merge Sort in Shared Memory
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.