Merge Sort. In plain English: if the size of the array 1, split the array into two halves, and...

34
Merge Sort

description

Execution Example Partition       1 67  72  29  94  43  38  86  61  

Transcript of Merge Sort. In plain English: if the size of the array 1, split the array into two halves, and...

Page 1: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Merge Sort

Page 2: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Merge Sort•In plain English: if the size of the array > 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 returncopy first half into leftArraycopy second half into rightArraysort (leftArray)sort (rightArray)merge leftArray with rightArray

Page 3: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example

• Partition

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 4: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Recursive call, partition

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 5: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Recursive call, partition

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 6: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Recursive call, base case

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 7: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Recursive call, base case

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 8: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Merge

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 9: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Recursive call, …, base case, merge

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

9 9 4 4

Page 10: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Merge

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 11: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Recursive call, …, merge, merge

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 12: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Execution Example (cont.)

• Merge

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

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 13: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Assume We Have a merge Method

void mergeSort ( int A[100], int i, int j){int m;if ( i < j ){

m = ( i + j )/2;

mergeSort (A, i, m);

mergeSort (A, m+1, j);

merge (A, i, m, j);}

main( ){int A[100];int size;/* read array A and its size */ mergeSort(A[ ], 0, size-1);}

Page 14: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Merge 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 < element of right array copy left element into merged array & increment index else copy right element into merged array & increment indexcopy balance of unexhausted array into merged array

Page 15: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

void merge ( int i1, int j1, int j2 ) { int i2, k1, k2, k; int tmpArray[100];

i2 = j1 + 1; k1 = i1; k2 = i2; k = 0;

Function merge

while ((k1 <= j1) || (k2 <= j2)) {

if (k1 > j1) { /* Left half is exhausted */ /* Copy from the right half */

tmpArray [k] = A[k2]; ++k2; }

else if (k2 > j2) { /*Right half is exhausted*//* Copy from the left half */

tmpArray [k] = A[k1]; ++k1; }

Contd..

Page 16: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

else if (A[k1] < A[k2]) { /* Left indx has a smaller value *//* Copy from the left half */

tmpArray[k] = A[k1]; ++k1; }

else { /* Right indx has a smaller value */ /* Copy from the right half */

tmpArray[k] = A[k2]; ++k2; }

++k;

/* Advance indx for writing */ }

Contd…/* Copy temporary array back to the original array */

--k; /* has size of tempArray */

while (k >= 0)

{

A[i1+k] = tmpArray[k];

--k; }

}

Contd..

Page 17: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion 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

Page 18: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Selection Sort

4 5 3 1 2

Page 19: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

Page 20: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

Page 21: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

Page 22: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

Page 23: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

Page 24: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

Page 25: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

Page 26: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

3 4 5 2

Page 27: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

3 4 5 2

3 4 5 2

Page 28: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

3 4 5 2

3 4 5 2

1 3 4 5 2

Page 29: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

3 4 5 2

3 4 5 2

1 3 4 5 2

1 3 4 5 2

Page 30: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

3 4 5 2

3 4 5 2

1 3 4 5 2

1 3 4 5 2

1 3 4 5

Page 31: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

3 4 5 2

3 4 5 2

1 3 4 5 2

1 3 4 5 2

1 3 4 5

1 3 4 5

Page 32: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

3 4 5 2

3 4 5 2

1 3 4 5 2

1 3 4 5 2

1 3 4 5

1 3 4 5

1 3 4 5

Page 33: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

Insertion Sort

4 5 3 1 2

4 5 1 2

4 5 1 2

3 4 5 1 2

3 4 5 1 2

3 4 5 2

3 4 5 2

3 4 5 2

1 3 4 5 2

1 3 4 5 2

1 3 4 5

1 3 4 5

1 3 4 5

1 2 3 4 5

Page 34: Merge Sort. In plain English: if the size of the array  1, split the array into two halves, and recursively sort both halves; when the sorts return,

for (i=1; i<n; ++i) { /* Consider A[i] */ /* Search for the correct insertion location of A[i] */

t = A[i]; /* Store A[i] in a temporary variable */

j = 0; /* Initialize search location */

while (t > A[j]) ++j; /* Skip smaller entries */ /* Here j holds the desired insertion location */

/* Shift forward the remaining entries each by one location */ for (k=i-1; k>=j; --k) A[k+1] = A[k];

/* Finally insert the old A[i] at the j-th location */ A[j] = t;

}