Merging Mergesort divideandconquer n Divide array into two

  • Slides: 23
Download presentation
Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted smallest auxiliary array 12/19/2021 Copyright 2000, Kevin Wayne 1

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted smallest auxiliary array 12/19/2021 Copyright 2000, Kevin Wayne 2

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 3

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 4

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 5

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 6

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 7

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 8

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 9

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 10

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 11

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 12

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 13

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 14

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 15

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 16

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 17

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 18

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 19

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n 12/19/2021 Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted Copyright 2000, Kevin Wayne 20

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted first half exhausted 12/19/2021 Copyright 2000, Kevin Wayne 21

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted first half exhausted 12/19/2021 Copyright 2000, Kevin Wayne 22

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately.

Merging Mergesort (divide-and-conquer) n Divide array into two halves. n Sort each half separately. n Merge two halves to make sorted whole. – keep track of smallest element in each half – smallest of two elements inserted into auxiliary array – repeat until all elements exhausted first half exhausted 12/19/2021 second half exhausted Copyright 2000, Kevin Wayne 23