Merge Sort using Multithreading in C++


We are given an unsorted integer array. The task is to sort the array using merge sort technique implemented via Multi-threading

Merge Sort

Merge sort is a sorting technique which is based on divide and conquer technique where we divide the array into equal halves and then combine them in a sorted manner.

Algorithm to implement merge sort is

  • check if there is one element in the list then return the element.

  • Else, Divide the data recursively into two halves until it can’t be divided further.

  • Finally, merge the smaller lists into new lists in sorted order.

Multi-Threading

In the operating system, Threads are the lightweight process which is responsible for executing the part of a task. Threads share common resources to execute the task concurrently.

Multi-threading is an implementation of multitasking where we can run multiple threads on a single processor to execute the tasks concurrently. It subdivides specific operations within a single application into individual threads. Each of the threads can run in parallel.

For Example-:

In −int arr[] = {3, 2, 1, 10, 8, 5, 7, 9, 4}

Out −Sorted array is: 1, 2, 3, 4, 5, 7, 8, 9, 10

Explanation −we are given an unsorted array with integer values. Now we will sort the array using merge sort with multithreading.

In −int arr[] = {5, 3, 1, 45, 32, 21, 50}

Out −Sorted array is: 1, 3, 5, 21, 32, 45, 50

Explanation −we are given an unsorted array with integer values. Now we will sort the array using merge sort with multithreading.

Approach used in the below program is as follows −

  • We will start by generating the random numbers using the rand() method in C++ STL.

  • Create an array of pthread_t type i.e. P_TH[thread_size].

  • Start loop FOR from i to 0 till i is less than the size of a thread. Inside the loop, call pthread_create(&P_TH[i], NULL, Sorting_Threading, (void*)NULL) method to create the thread with given array values.

  • Call function as combine_array(0, (size / 2 - 1) / 2, size / 2 - 1), combine_array(size / 2, size/2 + (size-1-size/2)/2, size - 1) and combine_array(0, (size - 1)/2, size - 1)

  • Print the sorted array stored in a arr[] of integer type.

  • Inside the function void* Sorting_Threading(void* arg)

    • Declare a variable as set_val to temp_val++, first to set_val * (size / 4), end to (set_val + 1) * (size / 4) - 1 and mid_val to first + (end - first) / 2

    • Check IF first less than end then call Sorting_Threading(first, mid_val), Sorting_Threading(mid_val + 1, end) and combine_array(first, mid_val, end);

  • Inside the function void Sorting_Threading(int first, int end)

    • Declare variable as mid_val to first + (end - first) / 2

    • Check IF first less than end then call Sorting_Threading(first, mid_val), Sorting_Threading(mid_val + 1, end) and combine_array(first, mid_val, end)

  • Inside the function void combine_array(int first, int mid_val, int end)

    • Declare variables as int* start to new int[mid_val - first + 1], int* last to new int[end - mid_val], temp_1 to mid_val - first + 1, temp_2 to end - mid_val, i, j, k to first.

    • Start loop FOR from i to 0 till i less than temp_1. Inside the loop, set start[i] to arr[i + first].

    • Start loop FOR from i to 0 till i less than temp_2. Inside the loop, set last[i] to arr[i + mid_val + 1]

    • Set i to j to 0. Start loop While i is less than temp_1 AND j less than temp_2. Inside the while, check IF start[i] less than last[j] then set arr[k++] to start[i++]. ELSE, set arr[k++] = last[j++]

    • Start WHILE i less than temp_1 then set arr[k++] = start[i++]. Start WHILE j less than temp_2 then set arr[k++] to last[j++]

Example

#include <iostream>
#include <pthread.h>
#include <time.h>
#define size 20
#define thread_size 4
using namespace std;
int arr[size];
int temp_val = 0;
void combine_array(int first, int mid_val, int end){
   int* start = new int[mid_val - first + 1];
   int* last = new int[end - mid_val];
   int temp_1 = mid_val - first + 1;
   int temp_2 = end - mid_val;
   int i, j;
   int k = first;
   for(i = 0; i < temp_1; i++){
      start[i] = arr[i + first];
   }
   for (i = 0; i < temp_2; i++){
      last[i] = arr[i + mid_val + 1];
   }
   i = j = 0;
   while(i < temp_1 && j < temp_2){
      if(start[i] <= last[j]){
         arr[k++] = start[i++];
      }
      else{
         arr[k++] = last[j++];
      }
   }
   while (i < temp_1){
      arr[k++] = start[i++];
   }
   while (j < temp_2){
      arr[k++] = last[j++];
   }
}
void Sorting_Threading(int first, int end){
   int mid_val = first + (end - first) / 2;
   if(first < end){
      Sorting_Threading(first, mid_val);
      Sorting_Threading(mid_val + 1, end);
      combine_array(first, mid_val, end);
   }
}
void* Sorting_Threading(void* arg){
   int set_val = temp_val++;
   int first = set_val * (size / 4);
   int end = (set_val + 1) * (size / 4) - 1;
   int mid_val = first + (end - first) / 2;
   if (first < end){
      Sorting_Threading(first, mid_val);
      Sorting_Threading(mid_val + 1, end);
      combine_array(first, mid_val, end);
   }
}
int main(){
   for(int i = 0; i < size; i++){
      arr[i] = rand() % 100;
   }
   pthread_t P_TH[thread_size];
   for(int i = 0; i < thread_size; i++){
      pthread_create(&P_TH[i], NULL, Sorting_Threading, (void*)NULL);
   }
   for(int i = 0; i < 4; i++){
      pthread_join(P_TH[i], NULL);
   }
   combine_array(0, (size / 2 - 1) / 2, size / 2 - 1);
   combine_array(size / 2, size/2 + (size-1-size/2)/2, size - 1);
   combine_array(0, (size - 1)/2, size - 1);
   cout<<"Merge Sort using Multi-threading: ";
   for (int i = 0; i < size; i++){
      cout << arr[i] << " ";
   }
   return 0;
}

Output

If we run the above code it will generate the following Output

Merge Sort using Multi-threading: 15 21 26 26 27 35 36 40 49 59 62 63 72 77 83 86 86 90 92 93

Updated on: 05-Nov-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements