0% found this document useful (0 votes)
104 views3 pages

Merge Sort Algorithm: Data Structures

The document discusses the merge sort algorithm. It begins by explaining that merge sort follows the "divide and conquer" approach. It divides the unsorted list into single element sublists, then repeatedly merges the sublists to create sorted sublists until one fully sorted list remains. Merge sort has a time complexity of O(n log n) and is a stable sort that maintains the relative order of equal elements. The document then provides pseudocode to demonstrate how merge sort works by recursively dividing and merging sublists. It analyzes merge sort's time and space complexities, noting it always runs in O(n log n) time and O(n) space.

Uploaded by

Abhishek Singla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
104 views3 pages

Merge Sort Algorithm: Data Structures

The document discusses the merge sort algorithm. It begins by explaining that merge sort follows the "divide and conquer" approach. It divides the unsorted list into single element sublists, then repeatedly merges the sublists to create sorted sublists until one fully sorted list remains. Merge sort has a time complexity of O(n log n) and is a stable sort that maintains the relative order of equal elements. The document then provides pseudocode to demonstrate how merge sort works by recursively dividing and merging sublists. It analyzes merge sort's time and space complexities, noting it always runs in O(n log n) time and O(n) space.

Uploaded by

Abhishek Singla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 3

(http://www.studytonight.

com/)

DATA STRUCTURES

SEE THE INDEX

Merge Sort Algorithm


Merge Sort follows the rule of Divide and Conquer. In merge sort the unsorted list is divided into N sublists, each having one element, because a list
consisting of one element is always sorted. Then, it repeatedly merges these sublists, to produce new sorted sublists, and in the end, only one sorted
list is produced.

Merge Sort is quite fast, and has a time complexity of O(n log n). It is also a stable sort, which means the "equal" elements are ordered in the same
order in the sorted list.

How Merge Sort Works

Like we can see in the above example, merge sort first breaks the unsorted list into sorted sublists, each having one element, because a list of one
element is considered sorted and then it keeps merging these sublists, to finally get the complete sorted list.

Sorting using Merge Sort Algorithm


/* a[] is the array, p is starting index, that is 0,
and r is the last index of array. */

//Lets take a[5] = {32, 45, 67, 2, 7} as the array to be sorted.

void mergesort(int a[], int p, int r)


{
int q;
if(p < r)
{
q = floor( (p+r) / 2);
mergesort(a, p, q);
mergesort(a, q+1, r);
merge(a, p, q, r);
}
}

void merge(int a[], int p, int q, int r)


{
int b[5]; //same size of a[]
int i, j, k;
k = 0;
i = p;
j = q+1;
while(i <= q && j <= r)
{
if(a[i] < a[j])
{
b[k++] = a[i++]; // same as b[k]=a[i]; k++; i++;
}
else
{
b[k++] = a[j++];
}
}

while(i <= q)
{
b[k++] = a[i++];
}

while(j <= r)
{
b[k++] = a[j++];
}

for(i=r; i >= p; i--)


{
a[i] = b[--k]; // copying back the sorted list to a[]
}

Complexity Analysis of Merge Sort

Worst Case Time Complexity : O(n log n)


Best Case Time Complexity : O(n log n)
Average Time Complexity : O(n log n)
Space Complexity : O(n)

Time complexity of Merge Sort is O(n Log n) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and

take linear time to merge two halves.


It requires equal amount of additional space as the unsorted list. Hence its not at all recommended for searching large unsorted lists.

It is the best Sorting technique used for sorting Linked Lists.

← Prev (quick-sort) Next → (heap-sort)

You might also like