# lec08 - 6.006-Introduction toAlgorithmsLecture 8Prof Piotr...

This preview shows pages 1–29. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 6.006-Introduction toAlgorithmsLecture 8Prof. Piotr IndykMenu• Sorting!– Insertion Sort– Merge Sort• Recurrences– Master theoremThe problem of sortingInput:arrayA[1…n]of numbers.Output:permutationB[1…n]ofAsuchthatB[1]£B[2]£…£B[n].How can we do it efficiently ?e.g. A = [7, 2, 5, 5, 9.6] → B = [2, 5, 5, 7, 9.6]A:1nInsertion sortINSERTION-SORT(A,n)⊳A[1 . .n]forj← 2tonjkeysortedinsert keyA[j]into the (already sorted) sub-arrayA[1 ..j-1].Illustration of iterationjinew location of keyby pairwise key-swaps down to its right positionExample of insertion sort824936Example of insertion sort824936Example of insertion sort824936284936Example of insertion sort824936284936Example of insertion sort824936284936248936Example of insertion sort824936284936248936Example of insertion sort824936284936248936248936Example of insertion sort824936284936248936248936Example of insertion sort824936284936248936248936234896Example of insertion sort824936284936248936248936234896Example of insertion sort824936284936248936248936234896234689doneRunning time?Θ(n2)e.g. when input is A = [n, n − 1, n − 2, . . . , 2, 1]Meet Merge SortMERGE-SORTA[1 . .n]1.Ifn= 1, done (nothing to sort).Key subroutine:MERGEdivide andconquer2.Otherwise, recursively sortA[ 1 . .n/2 ]andA[n/2+1 . .n].3. “Merge”the two sorted sub-arrays.201372121191Merging two sorted arraysMerging two sorted arrays2013721211911Merging two sorted arrays201372121191120137212119Merging two sorted arrays2013721211911201372121192Merging two sorted arrays20137212119112013721211922013712119Merging two sorted arrays201372121191120137212119220137121197Merging two sorted arrays201372121191120137212119220137121197201312119Merging two sorted arrays2013721211911201372121192201371211972013121199Merging two sorted arrays201372121191120137212119220137121197201312119920131211Merging two sorted arrays20137212119112013721211922013712119720131211992013121111Merging two sorted arrays20137212119112013721211922013712119720131211992013121111201312Merging two sorted arrays2013721211911201372121192201371211972013121199201312111120131212Merging two sorted arrays...
View Full Document

{[ snackBarMessage ]}

### Page1 / 52

lec08 - 6.006-Introduction toAlgorithmsLecture 8Prof Piotr...

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

View Full Document
Ask a homework question - tutors are online