Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch 2: Getting Started Ming-Te Chi

Similar presentations


Presentation on theme: "Ch 2: Getting Started Ming-Te Chi"— Presentation transcript:

1 Ch 2: Getting Started Ming-Te Chi
Algorithms Ch 2: Getting Started Ming-Te Chi Ch2 Getting Started

2 2.1 Insertion sort Example: Sorting problem
Input: A sequence of n numbers Output: A permutation of the input sequence such that The number that we wish to sort are known as the keys. Ch2 Getting Started

3 Sorting a hand of cards Ch2 Getting Started

4 Pseudocode Ch2 Getting Started

5 The operation of Insertion-Sort
5, 2, 4, 6, 1, 3 Ch2 Getting Started

6 Sorted in place The numbers are rearranged within the array A, with at most a constant number of them sorted outside the array at any time Ch2 Getting Started

7 Loop invariant Initialization: Maintenance: Termination:
It is true prior to the first iteration of the loop Maintenance: If it is true before an iteration of loop, it remains true before next iteration. Termination: When the loop terminates, the invariant gives us a useful property that show that the alogorithm is correct. Ch2 Getting Started

8 2.2 Analyzing algorithms Analyzing an algorithm has come to mean predicting the resources that the algorithm requires. Resources: memory, communication bandwidth, logic gate(hardware), and time. Assumption(Model):RAM(Random Access Model). single processor (We shall have occasion to investigate models for parallel computers and digital hardware.) Ch2 Getting Started

9 2.2 Analyzing algorithms The best notion for input size depends on the problem being studied. The running time of an algorithm on a particular input is the number of primitive operations or “steps” executed. It is convenient to define the notion of step so that it is as machine-independent as possible. Ch2 Getting Started

10 Analyzing insertion sort
Ch2 Getting Started

11 the running time Ch2 Getting Started

12 The best case running time
for j = 2, 3, …, n : Linear function on n Ch2 Getting Started

13 The worst case running time
for j = 2,3,…,n : quadratic function on n. Ch2 Getting Started

14 Worst-case and average-case analysis
Usually, we concentrate on finding only on the worst-case running time Reason: It is an upper bound on the running time. The worst case occurs fair often for some algorithm. The average case is often as bad as the worst case. For example, the insertion sort. Again, quadratic function. (Why?) Ch2 Getting Started

15 In some particular cases, we shall be interested in average-case, or expect running time of an algorithm. Ch2 Getting Started

16 Order of growth The leading term of formula (ex: )
It is the rate of growth, or order of growth, of the running time that really interests us. (More on this subject in a later lecture.) Probabilistic analysis. Ch2 Getting Started

17 2.3 Designing algorithms There are many ways to design algorithms:
Incremental approach: insertion sort Divide-and-conquer: merge sort recursive: Divide the problem into a number of subproblems. Conquer the subproblems by solving them recursively. Combine the solutions to the subproblems into the solution for the original problem. Ch2 Getting Started

18 Merge sort Key operation of Merge sort Merge two sorted subsequences
Use an auxiliary procedure Merge(A, p, q, r) A: an array p, q, r: indices with p <= q < r Ch2 Getting Started

19 Ch2 Getting Started

20 Example of Merge(A,p,q,r)
Ch2 Getting Started

21 Ch2 Getting Started

22 MERGE-SORT(A,p,r) 1 if p < r 2 then q = (p+r)/2
3 MERGE-SORT(A,p,q) 4 MERGE-SORT(A,q+1,r) 5 MERGE(A,p,q,r) Ch2 Getting Started

23 Example of merge sort Ch2 Getting Started

24 Analysis of merge sort Use recurrence to analyze divide-and-conquer algorithms Analysis of merge sort Ch2 Getting Started

25 Analysis of merge sort where the constant c represents the time require to solve problems of size 1 as well as the time per array element of the divide and combine steps. Ch2 Getting Started

26 Recursion tree Ch2 Getting Started

27 Ch2 Getting Started

28 Ch2 Getting Started

29 Outperforms insertion sort!
Merge sort Outperforms insertion sort! Ch2 Getting Started


Download ppt "Ch 2: Getting Started Ming-Te Chi"

Similar presentations


Ads by Google