90 likes | 212 Vues
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
E N D
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 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
F L 1 2 3 4 44 76 43 23 1. Merge_Sort (list, first, last)
1. Merge Sort (list, first, last) F L M 1 4 2 2. Merge Sort (list, first, middle) F L M 1 2 1 < 4 YES MIDDLE = 1 + 4 = 5 5 DIV 2 = 2 MIDDLE = 2 If (first < last) middle = (first + last) div 2 1 < 2 YES MIDDLE = 1 + 2 = 3 3 DIV 2 = 1 MIDDLE = 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 LIST 1 LIST 2 F L M 1 4 2 • 1 2 1 1 1 44 2 2 76
LIST 1 LIST 2 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 Newlist 1: 44 76
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 LIST 1 LIST 2 LIST 1 LIST 2 F L M 1 4 2 • 1 2 1 1 1 44 2 2 76 • 3 4 3 3 3 43 23 4 4
LIST 1 LIST 2 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 Newlist 2: 23 43
Newlist 1: 44 76 Newlist 2: 23 43 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 23 43 44 76
23 43 44 76 MERGE SORT COMPLETE