0% found this document useful (0 votes)
14 views13 pages

Merge Sort

Merge sort is a divide-and-conquer sorting algorithm that recursively divides an array into smaller subarrays, sorts them, and merges them back together. The process continues until the entire array is sorted. The document includes pseudocode and a C program implementation of the merge sort algorithm.

Uploaded by

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

Merge Sort

Merge sort is a divide-and-conquer sorting algorithm that recursively divides an array into smaller subarrays, sorts them, and merges them back together. The process continues until the entire array is sorted. The document includes pseudocode and a C program implementation of the merge sort algorithm.

Uploaded by

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

Merge sort is a sorting algorithm that follows the divide-and-conquer approach.

It works by recursively dividing the input array into smaller subarrays and sorting those subarrays then
merging them back together to obtain the sorted array.

In simple terms, we can say


that the process of merge
sort is to divide the array
into two halves, sort each
half, and then merge the
sorted halves back together.
This process is repeated
until the entire array is
sorted.
Pseudocode for merge sort function merge(left, right)
function mergeSort(array) result = empty array
if length of array <= 1 leftIndex = 0
return array rightIndex = 0

mid = length of array / 2 while leftIndex < length of left and rightIndex < length of right
leftHalf = array[0...mid-1] if left[leftIndex] <= right[rightIndex]
rightHalf = array[mid...end] append left[leftIndex] to result
leftIndex = leftIndex + 1
sortedLeft = mergeSort(leftHalf) else
sortedRight = mergeSort(rightHalf) append right[rightIndex] to result
rightIndex = rightIndex + 1
return merge(sortedLeft, sortedRight)
while leftIndex < length of left
append left[leftIndex] to result
This pseudocode breaks down the Merge Sort leftIndex = leftIndex + 1
algorithm into two main functions: mergeSort
and merge. The mergeSort function recursively while rightIndex < length of right
splits the array into halves until it reaches arrays append right[rightIndex] to result
of length 1 or 0. The merge function then rightIndex = rightIndex + 1
combines these smaller arrays back together in
sorted order. return result
PROGRAM TO MPLEMENT MERGE SORT

#include <stdio.h> // Merge the temporary arrays back into arr[left..right]


i = 0; // Initial index of first sub-array
void merge(int arr[], int left, int mid, int right) { j = 0; // Initial index of second sub-array
int i, j, k; k = left; // Initial index of merged sub-array
int n1 = mid - left + 1; while (i < n1 && j < n2) {
int n2 = right - mid; if (L[i] <= R[j]) {
arr[k] = L[i];
// Create temporary arrays i++;
int L[n1], R[n2]; } else {
arr[k] = R[j];
// Copy data to temporary arrays L[] and R[] j++;
for (i = 0; i < n1; i++) }
L[i] = arr[left + i]; k++;
for (j = 0; j < n2; j++) }
R[j] = arr[mid + 1 + j];
// Copy the remaining elements of L[], if there are any
void printArray(int arr[], int size) {
while (i < n1) {
for (int i = 0; i < size; i++)
arr[k] = L[i];
printf("%d ", arr[i]);
i++;
printf("\n");
k++;
}
}
// Copy the remaining elements of R[], if there are any
int main() {
while (j < n2) {
int arr[] = {12, 11, 13, 5, 6, 7};
arr[k] = R[j];
int arr_size = sizeof(arr) / sizeof(arr[0]);
j++;
k++;
printf("Given array is: \n");
}
printArray(arr, arr_size);
}
void mergeSort(int arr[], int left, int right) {
mergeSort(arr, 0, arr_size - 1);
if (left < right) {
// Find the middle point
printf("\nSorted array is: \n");
int mid = left + (right - left) / 2;
printArray(arr, arr_size);
// Sort first and second halves
return 0;
mergeSort(arr, left, mid);
}
mergeSort(arr, mid + 1, right);
// Merge the sorted halves
OUTPUT:
merge(arr, left, mid, right);
Given array is: 12 11 13 5 6 7
}
Sorted array is: 5 6 7 11 12 13
}

You might also like