notes21

notes21 - CS 251 Lecture October 20 & October 22 Merge...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Merge Sort Sorting mechanism that uses Divide and Conquer Divide and Conquer is an algorithm strategy that divides a problem into smaller sub-problems Then it solves these sub-problems by dividing them and so on until the solution is trivial Then it puts the solutions back together into the full solution Steps of Divide and Conquer 1. Divide . Divide problem into smaller sub-problems 2. Recursively solve the sub-problems 3. Conquer . Put together the solution to sub-problems into a bigger solution Merge Sort Sorting algorithm that uses Divide and Conquer At each step, it divides the array to be sorted into two sets: S 1 and S 2 such that the sizes of S 1 and S 2 differ by a maximum of 1 Apply recursively merge sort to S 1 and S 2 . Continue to divide until the number of elements in S 1 and S 2 is at most 1 At this point, the two sets are sorted because a set containing at most one element is sorted. Merge sets S 1 and S 2 by placing them in the desired sorted order For example, let S = { 3, 8, 15, 23, 6, 1, 10 } Let red = S 1 , green = S 2 : { 3, 8, 15, 23, 6, 1, 10 } -----------Divide----------
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
{ 3, 8, 15, 23 } { 6, 1, 10 } { 3, 8 } { 15, 23 } { 6, 1 } { 10 } { 3 } { 8 } { 15 } { 23 } { 6 } { 1 } { 10 } { } ------------Conquer----------- { 3, 8 } { 15, 23 } { 1, 6 } { 10 } { 3, 8, 15, 23 } { 1, 6, 10 } { 1, 3, 6, 8, 10, 15, 23 } The number of copy operations (divide operations) for one element from one
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

notes21 - CS 251 Lecture October 20 & October 22 Merge...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online