mguMod 5-3 Mergesort

65
DATA STRUCTURES AND ALGORITHMS MODULE 5 MERGE SORT

description

mguMod 5-3 Mergesort

Transcript of mguMod 5-3 Mergesort

  • DATA STRUCTURES AND ALGORITHMS

    MODULE 5

    MERGE SORT

  • MERGESORTProcessDivide phase:The list of N elements is first divided into two sublists of N/2 elements further divided into sublists containing N/4 elements and so on until each sublist contains only one element.Conquer phaseEach half is sorted independentlyThe 2 sorted halves are merged to a sorted sequence

  • N/4 elementsN/2 elementsN/2 elementsN elementsN/4 elementsN/4 elementsN/4 elements1 element1 element1 element1 element1 element1 elementDIVIDE PHASE

  • CONQUER PHASENow merge adjacent sublist of one element taking two sublists at a time.Repeat this process until there is only one array remaining of size NN/4 elementsN/2 elementsN/2 elementsN elementsN/4 elementsN/4 elementsN/4 elements1 element1 element1 element1 element1 element1 element

  • Eg: DIVIDE

    1271822516410

  • Eg: DIVIDE

    1271822516410

    516410

    1271822

  • Eg: DIVIDE

    1271822516410

    516410

    1271822

    127

    1822

    516

    410

  • Eg: DIVIDE

    1271822516410

    516410

    1271822

    127

    1822

    516

    410

    7

    22

    12

    18

    16

    5

    10

    4

  • Eg: CONQUER

    7

    22

    12

    18

    16

    5

    10

    4

  • Eg: CONQUER

    712

    1822

    516

    410

    7

    22

    12

    18

    16

    5

    10

    4

  • Eg: CONQUER

    451016

    7121822

    712

    1822

    516

    410

    7

    22

    12

    18

    16

    5

    10

    4

  • Eg: CONQUER

    4571012161822

    451016

    7121822

    712

    1822

    516

    410

    7

    22

    12

    18

    16

    5

    10

    4

  • HOW IS IT DONE ??

  • LEFT=0RIGHT=8INPUT ARRAY

    11753815216

  • LEFT=0RIGHT=8MID = (LEFT + RIGHT ) /2 = (0 +8 ) /2 = 4FIND MID POSITION

    11753815216012345678

  • LEFT=0RIGHT=8MID = (LEFT + RIGHT ) /2 = (0 +8 ) /2 = 4MID=4FIND MID POSITION

    11753815216012345678

  • LEFT=0RIGHT=8MID=4LEFT=0RIGHT=8MID+1=5SPLIT THE ARRAY INTO TWO

    11753815216012345678

    11753801234

    152165678

  • MID=4LEFT=0RIGHT=8MID+1=5SORT INDEPENDENTLY

    35781101234

    126155678

  • MID=4LEFT=0RIGHT=8MID+1=5MERGELEFT=0RIGHT=8MID+1=5MID=4

    35781101234

    126155678

    35781112615012345678

  • Algorithm Mergesort(left,right)

    if (left

  • HOW TO MERGE 2 SORTED ARRAYS?LEFTMIDMID +1RIGHTTemporary ArrayLEFT

    357811

    12615

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    1

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    1

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    12

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    12

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    123

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    123

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1Temporary ArrayTemporary ArrayRIGHTLEFT

    357811

    12615

    1235

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    12356

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1Temporary ArrayTemporary ArrayRIGHTLEFTLEFT

    357811

    12615

    12356

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    123567

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTLEFTLEFT

    357811

    12615

    1235678

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    1235678

  • Process of Merging 2 Sorted ArraysLEFTMID +1RIGHTTemporary ArrayTemporary ArrayLEFTMID

    357811

    12615

    123567811

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    12356781115

  • Process of Merging 2 Sorted ArraysLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFT

    357811

    12615

    12356781115

  • Copy from temporary Array to original ArrayLEFTMIDMID +1RIGHTTemporary ArrayTemporary ArrayLEFTRIGHTLEFTMID + 1MID

    12356781115

    12356781115

  • Algorithm: Merge(left,mid,right)

    set i=left, j=mid+1, k=left

    while (i

  • Why set i=left,j=mid+1, k=left

  • MS(0,5)Left=0,Right=5Mid=2

    1271822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5

    1271822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2return

    1271822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2returnMS(0,0)Left=0,Right=0returnMS(1,1)Left=1,Right=1returnMerge(0,0,1)Left=0, Mid=0, Right=1

    1271822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2returnMS(0,0)Left=0,Right=0returnMS(1,1)Left=1,Right=1returnMerge(0,0,1)Left=0, Mid=0, Right=1

    1271822516012345

    120

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2returnMS(0,0)Left=0,Right=0returnMS(1,1)Left=1,Right=1returnMerge(0,0,1)Left=0, Mid=0, Right=1

    120

    71

    12718225161271822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2returnMS(0,0)Left=0,Right=0returnMS(1,1)Left=1,Right=1returnMerge(0,0,1)Left=0, Mid=0, Right=1

    120

    71

    71201

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2return

    71201

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2return

    182

    71201

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2return

    182

    71201

    71218012

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5

    71218012

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5return

    71218012

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5returnMS(3,3)Left=3,Right=3returnMS(4,4)Left=4,Right=4returnMerge(3,3,4)Left=3, Mid=3, Right=4

    71218012

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5returnMS(3,3)Left=3,Right=3returnMS(4,4)Left=4,Right=4returnMerge(3,3,4)Left=3, Mid=3, Right=4

    223

    71218012

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5returnMS(3,3)Left=3,Right=3returnMS(4,4)Left=4,Right=4returnMerge(3,3,4)Left=3, Mid=3, Right=4

    223

    54

    71218012

    12718225167121822516012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5returnMS(3,3)Left=3,Right=3returnMS(4,4)Left=4,Right=4returnMerge(3,3,4)Left=3, Mid=3, Right=4

    223

    54

    71218012

    52234

    12718225167121852216012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5return

    71218012

    52234

    12718225167121852216012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5return

    71218012

    52234

    165

    12718225167121852216012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5return

    71218012

    52234

    165

    12718225167121851622012345

    51622345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5

    71218012

    12718225167121851622012345

    51622345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5

    71218012

    12718225167121851622012345

    51622345

    7121851622012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5

    12718225167121851622012345

    7121851622012345

  • MS(0,5)Left=0,Right=5Mid=2MS(0,2)Left=0,Right=2Mid=1MS(3,5)Left=3,Right=5Mid=4Merge(0,2,5)Left=0, Mid=2,Right=5MS(0,1)Left=0,Right=1Mid=0Merge(0,1,2)Left=0, Mid=1, Right=2MS(2,2)Left=2,Right=2returnMS(3,4)Left=3,Right=4Mid=3Merge(3,4,5)Left=0, Mid=3, Right=7MS(5,5)Left=5,Right=5returnMS(0,0)Left=0,Right=0returnMS(1,1)Left=1,Right=1returnMerge(0,1,1)Left=0, Mid=0, Right=1MS(3,3)Left=3,Right=3returnMS(4,4)Left=4,Right=4returnMerge(3,3,4)Left=3, Mid=3, Right=4

    12718225165712161822012345

  • SORT 9, 8, 7, 6, 5, 4, 3, 2, 1

  • SortAverageWorstComparisonExtra MemorybubbleO (N2)O (N2)poorNoselectionO (N2)O (N2)fairNoinsertionO (N2)O (N2)goodNoquicksortO (N log N)O (N2)goodNomergesortO (N log N)O (N log N)fairYes

    ****************************************