Merge Sort Merge Sort In plain English if

  • Slides: 34
Download presentation
Merge Sort

Merge Sort

Merge Sort • In plain English: if the size of the array > 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 the two halves • Pseudocode for function mergesort: if array size equals 1 return copy first half into left. Array copy second half into right. Array sort (left. Array) sort (right. Array) merge left. Array with right. Array

Execution Example • Partition 7 2 9 4 3 8 6 1 1 2

Execution Example • 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 8 6 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Recursive call, partition 7 2 9 4 3 8

Execution 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 8 6 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Recursive call, partition 7 2 9 4 3 8

Execution 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 8 6 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Recursive call, base case 7 2 9 4 3

Execution 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 8 6 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Recursive call, base case 7 2 9 4 3

Execution 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 8 6 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Merge 7 2 9 4 3 8 6 1

Execution Example (cont. ) • 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 8 6 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Recursive call, …, base case, merge 7 2 9

Execution 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 8 6 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Merge 7 2 9 4 3 8 6 1

Execution Example (cont. ) • 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 8 6 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Recursive call, …, merge 7 2 9 4 3

Execution Example (cont. ) • Recursive call, …, 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 6 8 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Execution Example (cont. ) • Merge 7 2 9 4 3 8 6 1

Execution Example (cont. ) • 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 7 2 2 7 7 7 2 2 3 8 6 1 1 3 6 8 9 4 4 9 9 9 4 4 3 8 3 3 8 8 6 1 1 6 6 6 1 1

Assume We Have a merge Method void merge. Sort ( int A[100], int i,

Assume We Have a merge Method void merge. Sort ( int A[100], int i, int j) { int m; if ( i < j ){ m = ( i + j )/2; merge. Sort (A, i, m); merge. Sort (A, m+1, j); merge (A, i, m, j); } main( ) { int A[100]; int size; /* read array A and its size */ merge. Sort(A[ ], 0, size-1); }

Merge Method • Merge algorithm in plain English: while left & right arrays still

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 index copy balance of unexhausted array into merged array

Function merge void merge ( int i 1, int j 2 ) { int

Function merge void merge ( int i 1, int j 2 ) { int i 2, k 1, k 2, k; int tmp. Array[100]; i 2 = j 1 + 1; k 1 = i 1; k 2 = i 2; k = 0; while ((k 1 <= j 1) || (k 2 <= j 2)) { if (k 1 > j 1) { /* Left half is exhausted */ /* Copy from the right half */ tmp. Array [k] = A[k 2]; ++k 2; } Contd. . else if (k 2 > j 2) { /*Right half is exhausted*/ /* Copy from the left half */ tmp. Array [k] = A[k 1]; ++k 1; }

Contd… else if (A[k 1] < A[k 2]) { /* Left indx has a

Contd… else if (A[k 1] < A[k 2]) { /* Left indx has a smaller value */ /* Copy from the left half */ Contd. . /* Copy temporary array back to the original array */ --k; /* has size of temp. Array */ tmp. Array[k] = A[k 1]; ++k 1; } while (k >= 0) else { /* Right indx has a smaller value */ /* Copy from the right half */ tmp. Array[k] = A[k 2]; ++k 2; } ++k; /* Advance indx for writing */ } { A[i 1+k] = tmp. Array[k]; --k; } }

Insertion Sort • In plain English: for each element starting with the second, “pull”

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

Selection Sort 45312

Selection Sort 45312

Insertion Sort 45312

Insertion Sort 45312

Insertion Sort 45312

Insertion Sort 45312

Insertion Sort 45312 4 512

Insertion Sort 45312 4 512

Insertion Sort 45312 4 512 4512

Insertion Sort 45312 4 512 4512

Insertion Sort 45312 4 512 4512 34512

Insertion Sort 45312 4 512 4512 34512

Insertion Sort 45312 4 512 4512 34512

Insertion Sort 45312 4 512 4512 34512

Insertion Sort 45312 34512 4 512 34 52 4512 34512

Insertion Sort 45312 34512 4 512 34 52 4512 34512

Insertion Sort 45312 34512 4 512 34 52 4512 3 452 34512

Insertion Sort 45312 34512 4 512 34 52 4512 3 452 34512

Insertion Sort 45312 34512 4 512 34 52 4512 3 452 34512 3452

Insertion Sort 45312 34512 4 512 34 52 4512 3 452 34512 3452

Insertion Sort 45312 34512 4 512 34 52 4512 3 452 34512 3452 13452

Insertion Sort 45312 34512 4 512 34 52 4512 3 452 34512 3452 13452

Insertion Sort 45312 34512 4 512 34 52 4512 3 452 34512 3452 13452

Insertion Sort 45312 34512 4 512 34 52 4512 3 452 34512 3452 13452

Insertion Sort 45312 34512 13452 4 512 34 52 134 5 4512 3 452

Insertion Sort 45312 34512 13452 4 512 34 52 134 5 4512 3 452 34512 3452 13452

Insertion Sort 45312 34512 13452 4 512 34 52 134 5 4512 3 452

Insertion Sort 45312 34512 13452 4 512 34 52 134 5 4512 3 452 13 45 34512 3452 13452

Insertion Sort 45312 34512 13452 4 512 34 52 134 5 4512 3 452

Insertion Sort 45312 34512 13452 4 512 34 52 134 5 4512 3 452 13 45 34512 3452 1 345 13452

Insertion Sort 45312 34512 13452 4 512 34 52 134 5 4512 3 452

Insertion Sort 45312 34512 13452 4 512 34 52 134 5 4512 3 452 13 45 34512 3452 1 345 13452 12345

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

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; }