1 / 9

1. PROCEDURE MERGE SORT (list, first, last) If (first < last) middle = (first + last) div 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

halla-morin
Download Presentation

1. PROCEDURE MERGE SORT (list, first, last) If (first < last) middle = (first + last) div 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. F L 1 2 3 4 44 76 43 23 1. Merge_Sort (list, first, last)

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 23 43 44 76 MERGE SORT COMPLETE

More Related