Download presentation
Presentation is loading. Please wait.
Published byEliana Tuck Modified over 10 years ago
1
1. PROCEDURE MERGE SORT (list, first, last) If (first < last) middle = (first + last) div 2 2. Merge Sort (list, first, middle) 3. Merge Sort (list, middle+1, last) 4. Merge (list, first, middle, last) endif PROCEDURE MERGE While (L1 not empty) AND (L2 not empty) Compare the next pair of items IF (item from L1) <= (item from L2) Then store item from L1 to newlist and get next item from L1 Else store item from L2 to newlist and get next item from L2 Endif Endwhile If (L1 empty) AND (L2 not empty) then copy items in L2 to Newlist Endif If (L1 not empty) AND (L2 empty) then copy items in L1 to newlist endif
2
23437644 4321 LF 1. Merge_Sort (list, first, last)
3
121 FL 1234 44764323 If (first < last) middle = (first + last) div 2 1 < 4 YES MIDDLE = 1 + 4 = 5 5 DIV 2 = 2 MIDDLE = 2 21 MLF 2. Merge Sort (list, first, middle) 241 MLF 1. Merge Sort (list, first, last) 1 < 2 YES MIDDLE = 1 + 2 = 3 3 DIV 2 = 1 MIDDLE = 1
4
FLM142FLM142 1 2 1 1 44 76 LIST 1 LIST 2 If (first < last) middle = (first + last) div 2 2. Merge Sort (list, first, middle) 3. Merge Sort (list, middle+1, last) 4. Merge (list, first, middle, last) endif 1. PROCEDURE MERGE SORT (list, first, last) FL 1234 44764323 2
5
PROCEDURE MERGE While (L1 not empty) AND (L2 not empty) Compare the next pair of items IF (item from L1) <= (item from L2) Then store item from L1 to newlist and get next item from L1 Else store item from L2 to newlist and get next item from L2 Endif Endwhile If (L1 empty) AND (L2 not empty) then copy items in L2 to Newlist Endif If (L1 not empty) AND (L2 empty) then copy items in L1 to newlist endif 44 76 LIST 1 LIST 2 Newlist 1: 4476
6
FLM142FLM142 1 2 1 1 44 76 3 4 3 3 43 23 LIST 1 LIST 2 LIST 1 LIST 2 2 4 If (first < last) middle = (first + last) div 2 2. Merge Sort (list, first, middle) 3. Merge Sort (list, middle+1, last) 4. Merge (list, first, middle, last) endif 1. PROCEDURE MERGE SORT (list, first, last) FL 1234 44764323
7
PROCEDURE MERGE While (L1 not empty) AND (L2 not empty) Compare the next pair of items IF (item from L1) <= (item from L2) Then store item from L1 to newlist and get next item from L1 Else store item from L2 to newlist and get next item from L2 Endif Endwhile If (L1 empty) AND (L2 not empty) then copy items in L2 to Newlist Endif If (L1 not empty) AND (L2 empty) then copy items in L1 to newlist endif 43 23 LIST 1 LIST 2 Newlist 2: 2343
8
Newlist 2: 2343 Newlist 1: 4476 PROCEDURE MERGE While (L1 not empty) AND (L2 not empty) Compare the next pair of items IF (item from L1) <= (item from L2) Then store item from L1 to newlist and get next item from L1 Else store item from L2 to newlist and get next item from L2 Endif Endwhile If (L1 empty) AND (L2 not empty) then copy items in L2 to Newlist Endif If (L1 not empty) AND (L2 empty) then copy items in L1 to newlist endif NEWLIST 23434476
9
444323 MERGE SORT COMPLETE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.