You are on page 1of 6

10/30/22, 10:21 PM DAA_mini project_matrix.

cpp - Colaboratory

// CPP Program to implement merge sort using

// multi-threading

#include <iostream>

#include <pthread.h>

#include <time.h>

// number of elements in array

#define MAX 20

// number of threads

#define THREAD_MAX 4

using namespace std;

// array of size MAX

int a[MAX];

int part = 0;

// merge function for merging two parts

void merge(int low, int mid, int high)

    int* left = new int[mid - low + 1];

    int* right = new int[high - mid];

    // n1 is size of left part and n2 is size

    // of right part

    int n1 = mid - low + 1, n2 = high - mid, i, j;

    // storing values in left part

    for (i = 0; i < n1; i++)

        left[i] = a[i + low];

    // storing values in right part

    for (i = 0; i < n2; i++)

        right[i] = a[i + mid + 1];

    int k = low;

    i = j = 0;

    // merge left and right in ascending order

    while (i < n1 && j < n2) {

        if (left[i] <= right[j])

            a[k++] = left[i++];

        else

            a[k++] = right[j++];

    }

    // insert remaining values from left

    while (i < n1) {

        a[k++] = left[i++];

https://colab.research.google.com/drive/15uqAOmpBteagM6NzIltiyWa7NjsbH-Pv#scrollTo=Y2ZrCWAgzcOQ&printMode=true 1/6
10/30/22, 10:21 PM DAA_mini project_matrix.cpp - Colaboratory

    }

    // insert remaining values from right

    while (j < n2) {

        a[k++] = right[j++];

    }

// merge sort function

void merge_sort(int low, int high)

    // calculating mid point of array

    int mid = low + (high - low) / 2;

    if (low < high) {

        // calling first half

        merge_sort(low, mid);

        // calling second half

        merge_sort(mid + 1, high);

        // merging the two halves
        merge(low, mid, high);

    }

// thread function for multi-threading

void* merge_sort(void* arg)

    // which part out of 4 parts

    int thread_part = part++;

    // calculating low and high

    int low = thread_part * (MAX / 4);

    int high = (thread_part + 1) * (MAX / 4) - 1;

    // evaluating mid point

    int mid = low + (high - low) / 2;

    if (low < high) {

        merge_sort(low, mid);

        merge_sort(mid + 1, high);

        merge(low, mid, high);

    }

// Driver Code

int main()

    // generating random values in array

    for (int i = 0; i < MAX; i++)

        a[i] = rand() % 100;

https://colab.research.google.com/drive/15uqAOmpBteagM6NzIltiyWa7NjsbH-Pv#scrollTo=Y2ZrCWAgzcOQ&printMode=true 2/6
10/30/22, 10:21 PM DAA_mini project_matrix.cpp - Colaboratory

    // t1 and t2 for calculating time for

    // merge sort

    clock_t t1, t2;

    t1 = clock();

    pthread_t threads[THREAD_MAX];

    // creating 4 threads

    for (int i = 0; i < THREAD_MAX; i++)

        pthread_create(&threads[i], NULL, merge_sort,

                                        (void*)NULL);

    // joining all 4 threads

    for (int i = 0; i < 4; i++)

        pthread_join(threads[i], NULL);

    // merging the final 4 parts

    merge(0, (MAX / 2 - 1) / 2, MAX / 2 - 1);

    merge(MAX / 2, MAX/2 + (MAX-1-MAX/2)/2, MAX - 1);

    merge(0, (MAX - 1)/2, MAX - 1);

    t2 = clock();

    // displaying sorted array

    cout << "Sorted array: ";

    for (int i = 0; i < MAX; i++)

        cout << a[i] << " ";

    // time taken by merge sort in seconds

    cout << "Time taken: " << (t2 - t1) /

            (double)CLOCKS_PER_SEC << endl;

    return 0;

File "<ipython-input-3-11b29e29ff62>", line 9

int matA[MAX][MAX];

SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

// C++ program for Merge Sort
#include <iostream>
using namespace std;

// Merges two subarrays of array[].
// First subarray is arr[begin..mid]
// Second subarray is arr[mid+1 end]
https://colab.research.google.com/drive/15uqAOmpBteagM6NzIltiyWa7NjsbH-Pv#scrollTo=Y2ZrCWAgzcOQ&printMode=true 3/6
10/30/22, 10:21 PM DAA_mini project_matrix.cpp - Colaboratory
// Second subarray is arr[mid+1..end]
void merge(int array[], int const left, int const mid,
    int const right)
{
  auto const subArrayOne = mid - left + 1;
  auto const subArrayTwo = right - mid;

  // Create temp arrays
  auto *leftArray = new int[subArrayOne],
    *rightArray = new int[subArrayTwo];

  // Copy data to temp arrays leftArray[] and rightArray[]
  for (auto i = 0; i < subArrayOne; i++)
    leftArray[i] = array[left + i];
  for (auto j = 0; j < subArrayTwo; j++)
    rightArray[j] = array[mid + 1 + j];

  auto indexOfSubArrayOne
    = 0, // Initial index of first sub-array
    indexOfSubArrayTwo
    = 0; // Initial index of second sub-array
  int indexOfMergedArray
    = left; // Initial index of merged array

  // Merge the temp arrays back into array[left..right]
  while (indexOfSubArrayOne < subArrayOne
    && indexOfSubArrayTwo < subArrayTwo) {
    if (leftArray[indexOfSubArrayOne]
      <= rightArray[indexOfSubArrayTwo]) {
      array[indexOfMergedArray]
        = leftArray[indexOfSubArrayOne];
      indexOfSubArrayOne++;
    }
    else {
      array[indexOfMergedArray]
        = rightArray[indexOfSubArrayTwo];
      indexOfSubArrayTwo++;
    }
    indexOfMergedArray++;
  }
  // Copy the remaining elements of
  // left[], if there are any
  while (indexOfSubArrayOne < subArrayOne) {
    array[indexOfMergedArray]
      = leftArray[indexOfSubArrayOne];
    indexOfSubArrayOne++;
    indexOfMergedArray++;
  }
  // Copy the remaining elements of
  // right[], if there are any
  while (indexOfSubArrayTwo < subArrayTwo) {
    array[indexOfMergedArray]
https://colab.research.google.com/drive/15uqAOmpBteagM6NzIltiyWa7NjsbH-Pv#scrollTo=Y2ZrCWAgzcOQ&printMode=true 4/6
10/30/22, 10:21 PM DAA_mini project_matrix.cpp - Colaboratory
      = rightArray[indexOfSubArrayTwo];
    indexOfSubArrayTwo++;
    indexOfMergedArray++;
  }
  delete[] leftArray;
  delete[] rightArray;
}

// begin is for left index and end is
// right index of the sub-array
// of arr to be sorted */
void mergeSort(int array[], int const begin, int const end)
{
  if (begin >= end)
    return; // Returns recursively

  auto mid = begin + (end - begin) / 2;
  mergeSort(array, begin, mid);
  mergeSort(array, mid + 1, end);
  merge(array, begin, mid, end);
}

// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
  for (auto i = 0; i < size; i++)
    cout << A[i] << " ";
}

// Driver code
int main()
{
  int arr[] = { 12, 11, 13, 5, 6, 7 };
  auto arr_size = sizeof(arr) / sizeof(arr[0]);

  cout << "Given array is \n";
  printArray(arr, arr_size);

  mergeSort(arr, 0, arr_size - 1);

  cout << "\nSorted array is \n";
  printArray(arr, arr_size);
  return 0;
}

// This code is contributed by Mayank Tyagi
// This code was revised by Joshua Estes

https://colab.research.google.com/drive/15uqAOmpBteagM6NzIltiyWa7NjsbH-Pv#scrollTo=Y2ZrCWAgzcOQ&printMode=true 5/6
10/30/22, 10:21 PM DAA_mini project_matrix.cpp - Colaboratory

Colab paid products


-
Cancel contracts here

error 0s completed at 10:01 PM

https://colab.research.google.com/drive/15uqAOmpBteagM6NzIltiyWa7NjsbH-Pv#scrollTo=Y2ZrCWAgzcOQ&printMode=true 6/6

You might also like