Merge Sort for 1D Array

PHOTO EMBED

Mon Mar 03 2025 02:56:16 GMT+0000 (Coordinated Universal Time)

Saved by @Rohan@99

#include <iostream>
#include <vector>
using namespace std;

void Merge(int a[], int low, int mid, int high)
{
  vector<int> temp;
  int left = low, right = mid+1;
  
  while(left <= mid && right <= high)
  {
    if(a[left] <= a[right])
    {
      temp.push_back(a[left]);
      ++left;
    }
    else
    {
      temp.push_back(a[right]);
      ++right;
    }
  }
  
  while(left <= mid)
  {
    temp.push_back(a[left]);
    ++left;
  }
  
  while(right <= high)
  {
    temp.push_back(a[right]);
    ++right;
  }
  
  for(int i = low; i <= high; ++i)
  {
    a[i] = temp[i-low];
  }
}

void MergeSort(int a[], int low, int high)
{
  if(low == high)
    return;
    
  int mid = (low + high) / 2;
  MergeSort(a, low, mid);
  MergeSort(a, mid+1, high);
  
  Merge(a, low, mid, high);
}

int main() 
{
  int n;
  cin >> n;
  
  int a[n];
  for(int i = 0; i < n; ++i)
    cin >> a[i];
    
  MergeSort(a, 0, n-1);
  
  for(int i = 0; i < n; ++i)
    cout << a[i] << " ";
  
  return 0;
}
content_copyCOPY