Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院 www.jiahenglu.net."— Presentation transcript:

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

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

3 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

4 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:

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

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

7 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 3 4 5 1 6 8 7 3 0 3 0 5 1 6 8 7 3 4 3 0 3 1 6 8 7 5 4

8 Partitioning Example (cont) 3 0 3 1 6 8 7 5 4 1 0 3 3 6 8 7 5 4 ≤ 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.

9 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 5-9 5. 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

10 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)

11 Figure 3.22 Order-of-Magnitude Time Efficiency Summary

12 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

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

14 Figure 3.27 A Comparison of Four Orders of Magnitude

15 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

16 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)

17 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

18 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?)

19 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

20 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

21 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

22 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?

23

24 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

25 Figure 3.1 Sequential Search Algorithm

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

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

28 Figure 3.4 Work = cn for Various Values of c

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

30 Figure 3.18 Binary Search Algorithm (list must be sorted)

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

32 Figure 3.21 A Comparison of n and lg n

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

34 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

35 Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup

36 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 )

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

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

39 Figure 3.15 The Copy-Over Algorithm for Data Cleanup

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

41 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

42 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

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

44 Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup

45 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)

46 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

47 Figure 3.17 Analysis of Three Data Cleanup Algorithms


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

Similar presentations


Ads by Google