0% found this document useful (0 votes)
55 views

Merge Sort

The merge sort algorithm works by recursively dividing an unsorted list into smaller sublists until each sublist contains a single element, which is therefore sorted. It then merges the sorted sublists back together into a single sorted list. The algorithm divides the list in half, recursively sorts each half by further halving it, and then merges the sorted halves back together.

Uploaded by

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

Merge Sort

The merge sort algorithm works by recursively dividing an unsorted list into smaller sublists until each sublist contains a single element, which is therefore sorted. It then merges the sorted sublists back together into a single sorted list. The algorithm divides the list in half, recursively sorts each half by further halving it, and then merges the sorted halves back together.

Uploaded by

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

MERGE SORT

ALGORITHM:
1. Divide the unsorted list into two sublists of about half the size
2. Sort each of the two sublists
3. Merge the two sorted sublists back into one sorted list
CODE:
void MergeSort(int low, int high)
// a[low : high] is a global array to be sorted.
// Small(P) is true if there is only one element to
// sort. In this case the list is already sorted.
{
if (low < high) { // If there are more than one element
// Divide P into subproblems.
// Find where to split the set.
int mid = (low + high)/2;
// Solve the subproblems.
MergeSort(low, mid);
MergeSort(mid + 1, high);
// Combine the solutions.
Merge(low, mid, high);
}
}
void Merge(int low, int mid, int high)
// a[low:high] is a global array containing two sorted
// subsets in a[low:mid] and in a[mid+1:high]. The goal
// is to merge these two sets into a single set residing

// in a[low:high]. b[] is an auxiliary global array.


{
int h = low, i = low, j = mid+1, k;
while ((h <= mid) && (j <= high)) {
if (a[h] <= a[j]) { b[i] = a[h]; h++; }
else { b[i] = a[j]; j++; } i++;
}
if (h > mid) for (k=j; k<=high; k++) {
b[i] = a[k]; i++;
}
else for (k=h; k<=mid; k++) {
b[i] = a[k]; i++;
}
for (k=low; k<=high; k++) a[k] = b[k];
}
Advantages :
1. conceptually simple
2. suited to sorting linked lists of elements because merge traverses each
linked list
3. suited to sorting external files; divides data into smaller files until can be
stored in array in memory
4. stable performance

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy