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

9
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

description

PROCEDURE MERGE While (L1 not empty) AND (L2 not empty) Compare the next pair of items IF (item from L1)

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

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

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 MERGEWhile (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

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

234376444321

LF

1. Merge_Sort (list, first, last)

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

1 2 1

F L1 2 3 4

44 76 43 23

If (first < last)

middle = (first + last) div 2

1 < 4 YES

MIDDLE = 1 + 4 = 5

5 DIV 2 = 2

MIDDLE = 2

21

MLF2. Merge Sort (list, first, middle)

241

MLF1. Merge Sort (list, first, last)

1 < 2 YES

MIDDLE = 1 + 2 = 3

3 DIV 2 = 1

MIDDLE = 1

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

F L M1 4 2

1 2 1

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)

F L1 2 3 4

44 76 43 23

2 2

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

PROCEDURE MERGEWhile (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: 44 76

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

F L M1 4 2

1 2 1

1 1 44

76

3 4 3

3 3 43

23

LIST 1

LIST 2

LIST 1

LIST 2

2 2

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

F L1 2 3 4

44 76 43 23

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

PROCEDURE MERGEWhile (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: 23 43

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

Newlist 2: 23 43

Newlist 1: 44 76PROCEDURE MERGEWhile (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

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

76444323

MERGE SORT COMPLETE

MERGE SORT COMPLETE