Title: Merge Sort
1Merge Sort
2Merge Sort
- In plain English if the size of the array gt 1,
split the array into two halves, and recursively
sort both halves when the sorts return, merge
the two halves - Pseudocode for function mergesort
- if array size equals 1
- return
- copy first half into leftArray
- copy second half into rightArray
- sort (leftArray)
- sort (rightArray)
- merge leftArray with rightArray
3Execution Example
7 2 9 4 3 8 6 1 ? 1 2 3 4 6 7 8 9
4Execution Example (cont.)
- Recursive call, partition
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 8 6
5Execution Example (cont.)
- Recursive call, partition
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 8 6
7 ? 2 ? 2 7
9 4 ? 4 9
3 8 ? 3 8
6 1 ? 1 6
6Execution Example (cont.)
- Recursive call, base case
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 8 6
7 ? 7
2 ? 2
9 ? 9
4 ? 4
3 ? 3
8 ? 8
6 ? 6
1 ? 1
7Execution Example (cont.)
- Recursive call, base case
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 8 6
7 ? 2 ? 2 7
9 4 ? 4 9
3 8 ? 3 8
6 1 ? 1 6
7 ? 7
2 ? 2
9 ? 9
4 ? 4
3 ? 3
8 ? 8
6 ? 6
1 ? 1
8Execution Example (cont.)
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 8 6
7 ? 2 ? 2 7
9 4 ? 4 9
3 8 ? 3 8
6 1 ? 1 6
7 ? 7
2 ? 2
9 ? 9
4 ? 4
3 ? 3
8 ? 8
6 ? 6
1 ? 1
9Execution Example (cont.)
- Recursive call, , base case, merge
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 8 6
7 ? 2 ? 2 7
9 4 ? 4 9
3 8 ? 3 8
6 1 ? 1 6
7 ? 7
2 ? 2
3 ? 3
8 ? 8
6 ? 6
1 ? 1
9 ? 9
4 ? 4
10Execution Example (cont.)
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 8 6
7 ? 2 ? 2 7
9 4 ? 4 9
3 8 ? 3 8
6 1 ? 1 6
7 ? 7
2 ? 2
9 ? 9
4 ? 4
3 ? 3
8 ? 8
6 ? 6
1 ? 1
11Execution Example (cont.)
- Recursive call, , merge, merge
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 6 8
7 ? 2 ? 2 7
9 4 ? 4 9
3 8 ? 3 8
6 1 ? 1 6
7 ? 7
2 ? 2
9 ? 9
4 ? 4
3 ? 3
8 ? 8
6 ? 6
1 ? 1
12Execution Example (cont.)
7 2 9 4 ? 3 8 6 1 ? 1 2 3 4 6 7 8 9
7 2 ? 9 4 ? 2 4 7 9
3 8 6 1 ? 1 3 6 8
7 ? 2 ? 2 7
9 4 ? 4 9
3 8 ? 3 8
6 1 ? 1 6
7 ? 7
2 ? 2
9 ? 9
4 ? 4
3 ? 3
8 ? 8
6 ? 6
1 ? 1
13Assume We Have a merge Method
void mergeSort ( int A100, int i, int j) int
m if ( i lt j ) m ( i j )/2 mergeSort (A,
i, m) mergeSort (A, m1, j) merge (A, i, m,
j)
main( ) int A100 int size / read array A
and its size / mergeSort(A , 0, size-1)
14Merge Method
- Merge algorithm in plain English while left
right arrays still have elements, copy the lower
element from either into the merged array when
either left or right array is exhausted, copy the
remainder of the other array into the merged
array - In pseudocode
- while neither array exhausted
- if element of left array lt element of right
array - copy left element into merged array
increment index - else
- copy right element into merged array
increment index - copy balance of unexhausted array into merged
array
15Function merge
void merge ( int i1, int j1, int j2 ) int i2,
k1, k2, k int tmpArray100 i2 j1 1 k1
i1 k2 i2 k 0
while ((k1 lt j1) (k2 lt j2)) if (k1 gt
j1) / Left half is exhausted / / Copy from
the right half / tmpArray k Ak2 k2
Contd..
else if (k2 gt j2) /Right half is
exhausted/ / Copy from the left half /
tmpArray k Ak1 k1
16Contd
Contd..
else if (Ak1 lt Ak2) / Left indx has a
smaller value / / Copy from the left half /
tmpArrayk Ak1 k1 else /
Right indx has a smaller value / / Copy from
the right half / tmpArrayk Ak2 k2
k / Advance indx for writing /
/ Copy temporary array back to the original
array / --k / has size of tempArray /
while (k gt 0) Ai1k tmpArrayk
--k
17Insertion Sort
- In plain English for each element starting with
the second, pull the element, then look at all
earlier elements and shift larger ones to the
right, then insert the element - In pseudocode
- for each element from second to last
- save the element
- for each earlier element that is larger
- shift it right
- insert current element
18Selection Sort
4 5 3 1 2
19Insertion Sort
4 5 3 1 2
20Insertion Sort
4 5 3 1 2
21Insertion Sort
4 5 3 1 2
4 5 1 2
22Insertion Sort
4 5 3 1 2
4 5 1 2
4 5 1 2
23Insertion Sort
4 5 3 1 2
4 5 1 2
4 5 1 2
3 4 5 1 2
24Insertion Sort
4 5 3 1 2
3 4 5 1 2
4 5 1 2
4 5 1 2
3 4 5 1 2
25Insertion Sort
4 5 3 1 2
3 4 5 1 2
4 5 1 2
3 4 5 2
4 5 1 2
3 4 5 1 2
26Insertion Sort
4 5 3 1 2
3 4 5 1 2
4 5 1 2
3 4 5 2
4 5 1 2
3 4 5 2
3 4 5 1 2
27Insertion Sort
4 5 3 1 2
3 4 5 1 2
4 5 1 2
3 4 5 2
4 5 1 2
3 4 5 2
3 4 5 2
3 4 5 1 2
28Insertion Sort
4 5 3 1 2
3 4 5 1 2
4 5 1 2
3 4 5 2
4 5 1 2
3 4 5 2
3 4 5 2
3 4 5 1 2
1 3 4 5 2
29Insertion Sort
4 5 3 1 2
3 4 5 1 2
1 3 4 5 2
4 5 1 2
3 4 5 2
4 5 1 2
3 4 5 2
3 4 5 2
3 4 5 1 2
1 3 4 5 2
30Insertion Sort
4 5 3 1 2
3 4 5 1 2
1 3 4 5 2
4 5 1 2
3 4 5 2
1 3 4 5
4 5 1 2
3 4 5 2
3 4 5 2
3 4 5 1 2
1 3 4 5 2
31Insertion Sort
4 5 3 1 2
3 4 5 1 2
1 3 4 5 2
4 5 1 2
3 4 5 2
1 3 4 5
4 5 1 2
3 4 5 2
1 3 4 5
3 4 5 2
3 4 5 1 2
1 3 4 5 2
32Insertion Sort
4 5 3 1 2
3 4 5 1 2
1 3 4 5 2
4 5 1 2
3 4 5 2
1 3 4 5
4 5 1 2
3 4 5 2
1 3 4 5
3 4 5 2
1 3 4 5
3 4 5 1 2
1 3 4 5 2
33Insertion Sort
4 5 3 1 2
3 4 5 1 2
1 3 4 5 2
4 5 1 2
3 4 5 2
1 3 4 5
4 5 1 2
3 4 5 2
1 3 4 5
3 4 5 2
1 3 4 5
3 4 5 1 2
1 3 4 5 2
1 2 3 4 5
34for (i1 iltn i) / Consider Ai / /
Search for the correct insertion location of Ai
/ t Ai / Store Ai in a temporary
variable / j 0 / Initialize search
location / while (t gt Aj) j / Skip
smaller entries / / Here j holds the desired
insertion location / / Shift forward the
remaining entries each by one location / for
(ki-1 kgtj --k) Ak1 Ak / Finally
insert the old Ai at the j-th location / Aj
t