lecture04

# lecture04 - Introduction to Algorithms...

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

Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 4 Prof. Charles E. Leiserson

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

View Full Document
Day 6 Introduction to Algorithms L4.2 Quicksort Proposed by C.A.R. Hoare in 1962. Divide-and-conquer algorithm. Sorts “in place” (like insertion sort, but not like merge sort). Very practical (with tuning).
Day 6 Introduction to Algorithms L4.3 Divide and conquer Quicksort an n -element array: 1. Divide: Partition the array into two subarrays around a pivot x such that elements in lower subarray x elements in upper subarray. 2. Conquer: Recursively sort the two subarrays. 3. Combine: Trivial. x x x x x x Key: Linear-time partitioning subroutine.

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

View Full Document
Day 6 Introduction to Algorithms L4.4 x x Running time = O ( n ) for n elements. Running time = O ( n ) for n elements. Partitioning subroutine P ARTITION ( A , p , q ) A [ p . . q ] x A [ p ] pivot = A [ p ] i p for j p + 1 to q do if A [ j ] x then i i + 1 exchange A [ i ] A [ j ] exchange A [ p ] A [ i ] return i x x x x ? ? pi q j Invariant:
Day 6 Introduction to Algorithms L4.5 Example of partitioning ij 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11

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

View Full Document
Day 6 Introduction to Algorithms L4.6 Example of partitioning ij 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11
Day 6 Introduction to Algorithms L4.7 Example of partitioning ij 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11

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

View Full Document
Day 6 Introduction to Algorithms L4.8 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11
Day 6 Introduction to Algorithms L4.9 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11

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

View Full Document
Day 6 Introduction to Algorithms L4.10 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11
Day 6 Introduction to Algorithms L4.11 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 3 3 10 10 8 8 13 13 2 2 11 11 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11

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

View Full Document
Day 6 Introduction to Algorithms L4.12 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 3 3 10 10 8 8 13 13 2 2 11 11 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11
Day 6 Introduction to Algorithms L4.13 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 6 6 5 5 3 3 10 10 8 8 13 13 2 2 11 11 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11 ij 6 6 5 5 3 3 2 2 8 8 13 13 10 10 11 11

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

View Full Document
Day 6 Introduction to Algorithms L4.14 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 6 6 5 5 3 3 10 10 8 8 13 13 2 2 11 11 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11 ij 6 6 5 5 3 3 2 2 8 8 13 13 10 10 11 11
Day 6 Introduction to Algorithms L4.15

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.

## lecture04 - Introduction to Algorithms...

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

View Full Document
Ask a homework question - tutors are online
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.coursehero.com/file/5455709/lecture04/", "fetch_time": 1481336335000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2016-50/segments/1480698542932.99/warc/CC-MAIN-20161202170902-00430-ip-10-31-129-80.ec2.internal.warc.gz", "warc_record_offset": 907842035, "warc_record_length": 563234, "token_count": 1437, "char_count": 3772, "score": 4.125, "int_score": 4, "crawl": "CC-MAIN-2016-50", "snapshot_type": "longest", "language": "en", "language_score": 0.700594425201416, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00021-of-00064.parquet"}