当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

麻省理工学院:《算法导论》(英文版)Lecture 4 Prof. charles e. leiserson

资源类别:文库,文档格式:PDF,文档页数:47,文件大小:247.06KB,团购合买
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)
点击下载完整版文档(PDF)

Introduction to algorithms 6.046J/18.401J/SMA5503 Lecture 4 Prof. charles e. leiserson

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

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.2

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)

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 xs elements in upper subarray X X x 2. Conquer: Recursively sort the two subarrays 3. Combine: Trivial Key: Linear-time partitioning subroutine Day 6 Introduction to Algorithms L4.3

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. ≤≤xx xx ≥≥xx Key: Linear-time partitioning subroutine

Partitioning subroutine PARTITION(A, P, DAlp. q xA exchange Alp<> Ai return i variant:x≤x Day 6 Introduction to Algorithms

Day 6 Introduction to Algorithms L4.4 xx Running time = O(n) for n elements. Running time = O(n) for n elements. Partitioning subroutine PARTITION(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 ≤≤xx ≥≥xx ?? pi q j Invariant:

Example of partitioning 61013583211 Day 6 Introduction to Algorithms L4.5

Day 6 Introduction to Algorithms L4.5 Example of partitioning i j 66 1010 1313 55 88 33 22 1111

Example of partitioning 61013583211 Day 6 Introduction to Algorithms L46

Day 6 Introduction to Algorithms L4.6 Example of partitioning i j 66 1010 1313 55 88 33 22 1111

Example of partitioning 61013583211 Day 6 Introduction to Algorithms 4.7

Day 6 Introduction to Algorithms L4.7 Example of partitioning i j 66 1010 1313 55 88 33 22 1111

Example of partitioning 66 10135 3211 51310 88 3211 Day 6 Introduction to Algorithms L4.8

Day 6 Introduction to Algorithms L4.8 Example of partitioning 66 1010 1313 55 88 33 22 1111 i j 66 55 1313 1010 88 33 22 1111

Example of partitioning 66 10135 3211 51310 88 3211 Day 6 Introduction to Algorithms

Day 6 Introduction to Algorithms L4.9 Example of partitioning 66 1010 1313 55 88 33 22 1111 i j 66 55 1313 1010 88 33 22 1111

Example of partitioning 66 10135 3211 51310 88 3211 Day 6 Introduction to Algorithms L4.10

Day 6 Introduction to Algorithms L4.10 Example of partitioning 66 1010 1313 55 88 33 22 1111 i j 66 55 1313 1010 88 33 22 1111

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共47页,可试读16页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有