0% found this document useful (0 votes)
140 views10 pages

Ds 7-Merge Sort

Merge sort is a divide and conquer sorting algorithm that works as follows: 1) Divide the unsorted list into halves until you have lists with single elements 2) Merge the single element lists back together sorting them in the process. It does this by comparing elements in the left and right lists and copying the smaller element into the sorted list. 3) The overall runtime of merge sort is O(n log n) making it one of the most efficient sorting algorithms for large lists.

Uploaded by

Awaneesh Raj
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)
140 views10 pages

Ds 7-Merge Sort

Merge sort is a divide and conquer sorting algorithm that works as follows: 1) Divide the unsorted list into halves until you have lists with single elements 2) Merge the single element lists back together sorting them in the process. It does this by comparing elements in the left and right lists and copying the smaller element into the sorted list. 3) The overall runtime of merge sort is O(n log n) making it one of the most efficient sorting algorithms for large lists.

Uploaded by

Awaneesh Raj
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/ 10

 Merge Sort

 Radix Sort
Merge Sort
 Divide and Conquer
 Recursive in structure
 Divide the problem into sub-problems that are similar
to the original but smaller in size
 Conquer the sub-problems by solving them recursively.
If they are small enough, just solve them in a
straightforward manner.
 Combine the solutions to create a solution to the
original problem
An Example: Merge Sort
Sorting Problem: Sort a sequence of n elements into
non-decreasing order.

 Divide: Divide the n-element sequence to be


sorted into two subsequences of n/2 elements each
 Conquer: Sort the two subsequences recursively
using merge sort.
 Combine: Merge the two sorted subsequences to
produce the sorted answer.
Merge Sort – Example
Original Sequence Sorted Sequence

18 26 32 6 43 15 9 1 1 6 9 15 18 26 32 43

18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43

18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9

18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1

18 26 32 6 43 15 9 1
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers

MergeSort (A, p, r) // sort A[p..r] by divide & conquer


1 if p < r
2 then q  (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]

Initial Call: MergeSort(A, 1, n)


Procedure Merge
Merge(A, p, q, r)
1 n1  q – p + 1
2 n2  r – q Input: Array containing sorted subarrays
3 for i  1 to n1 A[p..q] and A[q+1..r].
4 do L[i]  A[p + i – 1]
5 for j  1 to n2
Output: Merged sorted subarray in A[p..r].
6 do R[j]  A[q + j]
7 L[n1+1]  
8 R[n2+1]  
9 i1
10 j1
11 for k p to r
12 do if L[i]  R[j]
13 then A[k]  L[i]
14 ii+1
15 else A[k]  R[j]
16 jj+1
Merge – Example

A … 61 8
6 26
8 32
9 1
26 9
32 42 43 …

k k k k k k k k k

L 6 8 26 32  
R 1 9 42 43

i i i i i j j j j j
Analysis of Merge Sort
 Running time T(n) of Merge Sort:
 Divide: computing the middle takes (1)
 Conquer: solving 2 sub-problems takes 2T(n/2)
 Combine: merging n elements takes (n)
 Total:
T(n) = (1) if n = 1
T(n) = 2T(n/2) + (n) if n > 1
T(n) = 2 T(n/2) + n
= 2 ((n/2)log(n/2) + (n/2)) + n
= n (log(n/2)) + 2n
= n log n – n + 2n
= n log n + n
= O(n log n )
Comparing the Algorithms
Best Average Worst
Case Case Case
Bubble Sort O(n) O(n2) O(n2)
Insertion Sort O(n) O(n2) O(n2)
Selection Sort O(n2) O(n2) O(n2)
Merge Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(n log n) O(n log n) O(n2)
Heap Sort O(n log n) O(n log n) O(n log n)
Thank You

You might also like