计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院 www.jiahenglu.net.

Slides:



Advertisements
Similar presentations
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Efficiency of Algorithms
The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
The Efficiency of Algorithms
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS4413 Divide-and-Conquer
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Efficiency of Algorithms
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
CS107 Introduction to Computer Science
Chapter 3 The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Algorithms and Efficiency of Algorithms February 4th.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Chapter 3: The Efficiency of Algorithms
Algorithm Efficiency and Sorting
Efficiency of Algorithms Csci 107 Lecture 6. Last time –Algorithm for pattern matching –Efficiency of algorithms Today –Efficiency of sequential search.
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
Chapter 3: The Efficiency of Algorithms
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
CMSC 100 Efficiency of Algorithms Guest Lecturers: Clay Alberty and Kellie LaFlamme Professor Marie desJardins Tuesday, October 2, 2012 Some material adapted.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Invitation to Computer Science 6th Edition Chapter 3 The Efficiency of Algorithms.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Computer Science/Ch. Algorithmic Foundation of CS 4-1 Chapter 4 Chapter 4 Algorithmic Foundation of Computer Science.
Searching Topics Sequential Search Binary Search.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
Chapter 16: Searching, Sorting, and the vector Type.
Searching and Sorting Algorithms
Introduction to Search Algorithms
Sorting by Tammy Bailey
Design and Analysis of Algorithms
Teach A level Computing: Algorithms and Data Structures
Algorithm design and Analysis
Chapter 3: The Efficiency of Algorithms
Lecture 3 / 4 Algorithm Analysis
Chapter 3: The Efficiency of Algorithms
Algorithm Efficiency and Sorting
Invitation to Computer Science 5th Edition
Presentation transcript:

计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院

Sort Algorithms (基本计算机算法:排序)

Quick Quiz 1. In the sequential search algorithm, the minimum amount of work is done if NAME is the ____ name in the list. 2. The ____ case of an algorithm requires the minimum amount of work. 3. The ____ case of an algorithm requires the maximum amount of work. 4. The selection sort algorithm does the same amount of work no matter how the numbers are initially arranged. True or False? 5. n grows at a much faster rate than n 2. True or False? Answer: first Answer: best Answer: worst Answer: True Answer: false

QUICKSORT Get a list of n elements to sort. Partition the list with the smallest elements in the first part and the largest elements in the second part. Sort the first part using Quicksort. Sort the second part using Quicksort. Stop. High level description of quicksort:

Two Problems to Deal With: 1) What is the partitioning and how do we accomplish it? 2) How do we sort the two parts? Let ’ s deal with (2) first: –To sort a sublist, we will use the same strategy as on the entire list- i.e. –Partition the list with the smallest elements in the first part and the largest elements in the second part. –Sort the first part using Quicksort. –Sort the second part using Quicksort. Obviously when a list or sublist has length 1, it is sorted.

The First Quicksort Problem Question (1): What is the partitioning and how do we accomplish it? An element from the list called pivot is used to divide list into two sublists –We follow common practice of using the first element of list as the pivot. We use the pivot to create –A left sublist contains those elements ≤ the pivot –A right sublist contains those elements > the pivot.

Partitioning Example The left pointer moves right until a value > 3 is found Next, right pointer moves left until a value ≤ 3 is found These two values are swapped, and process repeats

Partitioning Example (cont) ≤ pivot pivot > pivot Partitioning stops when the left (white) pointer ≥ the right (blue) pointer. At this point, the list items at the pivot and right pointer are swapped.

Partitioning Algorithm 1. Set the pivot to the first element in list 2. Set the left marker L to the first element of the list 3. Set the right marker R to the last element (n th ) of the list 4. While L is less than R, do Steps While element at L is not larger than pivot and L≤n 6. Move L to the right one position 7. While element at R is larger than pivot and R≥1 8. Move R to the left one position 9. If L is left of R then exchange elements at L and R. 10. Exchange the pivot with element at R. 11. Stop

Quicksort Complexity Best case time complexity –  (n lg n) Average case time complexity –  (n lg n) Worst case running time –  (n 2 ) Worst case examples??? –A list that is already sorted –A list that is reverse sorted (largest to smallest)

Figure 3.22 Order-of-Magnitude Time Efficiency Summary

Quick Quiz 1.In the shuffle-left algorithm, the best case occurs when the list has no 0 values. True or False? 2. The best case for the converging-pointers algorithm is a list of all 0 entries. True or False? 3. The binary search algorithm works only on a list that has already been ____. Answer: True Answer: False Answer: sorted

Figure 3.25 Comparisons of lg n, n, n 2, and 2 n

Figure 3.27 A Comparison of Four Orders of Magnitude

When Things Get Out Of Hand: Hamiltonian Circuits A path that begins and ends at the same node and goes through all other nodes exactly once following the edges. A B CD

Satisfiability Problem (A or B) and ( ¬ B or ¬ C) (A or B) and (B or C) and ( ¬ A or ¬ B) (A or B) and ( ¬ B or ¬ C) and ( ¬ A) (A or B) and ( ¬ A or ¬ C) and ( ¬ B or C) and (A or B or ¬ C)

Bin Packing Problem Given: –an unlimited number of bins of volume 1 unit –N objects of volume between 0.0 and 1.0 Find: –The minimum number of bins to store the n objects

Approximation Algorithms How would you go about solving the bin- packing problem quickly even if it is the best solution? (Ever see how luggage is packed on an airplane?)

An Algorithm is A well-ordered collection of Unambiguous and Effectively computable operations that, when executed Produces a result and Halts in a finite amount of time Textbook definition

Properties of Algorithms Correctness Does the algorithm always work Clarity Is the algorithm easy to understand Elegance (Style) Example: Gauss asked to add numbers from 1 to 100 Efficiency Speed of algorithm Space required to run algorithm

Efficiency of Algorithms How long does it take an algorithm to run? –Depends on computer –Depends on input data Benchmarking –Run same algorithm and same input on difference machines –Runs same algorithm on same machine using difference input Need a method of measuring inherent efficiency of algorithm, independent of machine or input Count how many times “ work unit ” is executed

Game Time Guess My Number 20 Questions Guess Who? For Each of these games understand different strategies for finding the answer. What is the best strategy?

Telephone Book Example Task –Find a particular person ’ s number from a list of telephone subscribers, given the name Sequential Search Algorithm outline –Start with the first entry and check its name, then repeat the process for all entries

Figure 3.1 Sequential Search Algorithm

How Efficient is the Algorithm? Best Case: Worst Case: Average Case: 1 Comparison N Comparisons N/2 Comparisons

Order of Magnitude Total number of operations performed is some constant number of operations times N 2N 3N ½*N Linear  (n)

Figure 3.4 Work = cn for Various Values of c

Telephone Book Example 2 Task –Find a particular person ’ s number from a sorted list of telephone subscribers, given the name Binary Search Algorithm outline –Always select the middle name. If the persons name is greater than selected name then search top half, otherwise search bottom half of remaining names.

Figure 3.18 Binary Search Algorithm (list must be sorted)

How Efficient is the Algorithm? Best Worst Average 1 Comparison Log N Comparisons  (Log n) About Log N Comparisons  (Log n)

Figure 3.21 A Comparison of n and lg n

Data Cleanup Algorithms Given a collection of numbers, find and remove all zeros Possible algorithms –Shuffle-left –Copy-over –Converging-pointers

The Shuffle-Left Algorithm Scan list from left to right –When a zero is found, shift all values to its right one slot to the left

Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup

The Shuffle-Left Algorithm (continued) Time efficiency –Count examinations of list values and shifts –Best case No shifts, n examinations  (n) –Worst case Shift at each pass, n passes n 2 shifts plus n examinations  (n 2 )

The Shuffle-Left Algorithm (continued) Space efficiency –n slots for n values, plus a few local variables –  (n)

The Copy-Over Algorithm Use a second list –Copy over each nonzero element in turn

Figure 3.15 The Copy-Over Algorithm for Data Cleanup

The Copy-Over Algorithm Time efficiency –Count examinations and copies –Best case All zeros n examinations and 0 copies  (n)

The Copy-Over Algorithm (continued) Time efficiency (continued) –Worst case No zeros n examinations and n copies  (n) Space efficiency –2n slots for n values, plus a few extraneous variables

The Copy-Over Algorithm (continued) Time/space tradeoff –Algorithms that solve the same problem offer a tradeoff One algorithm uses more time and less memory Its alternative uses less time and more memory

The Converging-Pointers Algorithm Swap zero values from left with values from right until pointers converge in the middle

Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup

The Converging-Pointers Algorithm Swap zero values from left with values from right until pointers converge in the middle Time efficiency –Count examinations and swaps –Best case n examinations, no swaps  (n)

The Converging-Pointers Algorithm (continued) Time efficiency (continued) –Worst case n examinations, n swaps  (n) Space efficiency –n slots for the values, plus a few extra variables

Figure 3.17 Analysis of Three Data Cleanup Algorithms