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
- 3-way Merge Sort
- Merge Sort
- Iterative Merge Sort
- C Program for Merge Sort
- In-Place Merge Sort
- C Program for Iterative Merge Sort
- Python Program for Merge Sort
- Java Program for Merge Sort
- Merge Sort for Linked Lists
- Visualization of Merge sort using Matplotlib
- Python Program for Iterative Merge Sort
- Java Program for Iterative Merge Sort
- Merge Sort for Doubly Linked List
- Count Inversions in an array | Set 1 (Using 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.