0% found this document useful (0 votes)
21 views5 pages

Merge Sort

The document provides an implementation of the Merge Sort algorithm in C++, including the code for merging two sorted subarrays and recursively sorting the array. It also includes pseudocode that outlines the steps of the Merge Sort process: dividing the array, conquering through recursion, and combining the sorted halves. The main function allows user input for the array elements and outputs the sorted array.

Uploaded by

brotin2503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Merge Sort

The document provides an implementation of the Merge Sort algorithm in C++, including the code for merging two sorted subarrays and recursively sorting the array. It also includes pseudocode that outlines the steps of the Merge Sort process: dividing the array, conquering through recursion, and combining the sorted halves. The main function allows user input for the array elements and outputs the sorted array.

Uploaded by

brotin2503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MERGE SORT:

CODE:

#include <iostream>

#include <vector>

// Function to merge two sorted subarrays

void merge(std::vector<int> &arr, int left, int mid, int right) {

int n1 = mid - left + 1; // Size of the left subarray

int n2 = right - mid; // Size of the right subarray

// Create temporary arrays

std::vector<int> leftArray(n1);

std::vector<int> rightArray(n2);

// Copy data to temporary arrays

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

leftArray[i] = arr[left + i];

for (int j = 0; j < n2; ++j)

rightArray[j] = arr[mid + 1 + j];

// Merge the temporary arrays back into the original array

int i = 0, j = 0, k = left;

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

if (leftArray[i] <= rightArray[j]) {

arr[k] = leftArray[i];

++i;

} else {

arr[k] = rightArray[j];

++j;

++k;

// Copy any remaining elements of leftArray


while (i < n1) {

arr[k] = leftArray[i];

++i;

++k;

// Copy any remaining elements of rightArray

while (j < n2) {

arr[k] = rightArray[j];

++j;

++k;

// Function to implement Merge Sort

void mergeSort(std::vector<int> &arr, int left, int right) {

if (left < right) {

int mid = left + (right - left) / 2; // Calculate mid-point

// Recursively sort the first and second halves

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

// Merge the sorted halves

merge(arr, left, mid, right);

int main() {

int n;

std::cout << "Enter the number of elements: ";

std::cin >> n;

std::vector<int> arr(n);
std::cout << "Enter the elements:\n";

for (int i = 0; i < n; ++i) {

std::cin >> arr[i];

// Apply Merge Sort

mergeSort(arr, 0, n - 1);

// Output the sorted array

std::cout << "Sorted array: ";

for (const int &num : arr) {

std::cout << num << " ";

std::cout << std::endl;

return 0;

PSEUDOCODE:::::

MERGE_SORT(arr, left, right):

IF left >= right:

RETURN

mid = (left + right) / 2

// Divide

MERGE_SORT(arr, left, mid)

MERGE_SORT(arr, mid + 1, right)

// Conquer and Combine

MERGE(arr, left, mid, right)

MERGE(arr, left, mid, right):

Create temporary arrays:

leftArray = arr[left to mid]

rightArray = arr[mid+1 to right]


i = 0, j = 0, k = left

WHILE i < size of leftArray AND j < size of rightArray:

IF leftArray[i] <= rightArray[j]:

arr[k] = leftArray[i]

i += 1

ELSE:

arr[k] = rightArray[j]

j += 1

k += 1

// Copy remaining elements from leftArray

WHILE i < size of leftArray:

arr[k] = leftArray[i]

i += 1

k += 1

// Copy remaining elements from rightArray

WHILE j < size of rightArray:

arr[k] = rightArray[j]

j += 1

k += 1

Merge Sort Algorithm

1. Divide

 If the array has more than one element:

o Divide the array into two halves: left and right.

2. Conquer

 Recursively apply Merge Sort to the left half and the right half.

3. Combine

 Merge the two sorted halves into a single sorted array:

o Compare elements from the two halves.

o Place the smaller element into the result array.

o Continue until all elements from both halves are merged.

You might also like