Slide 1

Slide 1 text

Chapter 2 Getting Started Sun-Yuan Hsieh 謝孫源 教授 成功大學資訊工程學系

Slide 2

Slide 2 text

2 2.1 Insertion sort Example: Sorting problem Input: A sequence of n numbers a1 , a2 ,…, an  Output: A permutation   of the input sequence such that . The number that we wish to sort are known as the keys. ' ' 2 ' 1 ,..., , n a a a ' ' 2 ' 1 n a a a    

Slide 3

Slide 3 text

3 Pseudocode Insertion sort Insertion-sort(A) 1. for j  2 to length[A] 2. do key  A[j] 3. *Insert A[j] into the sorted sequence A[1,…, j – 1] 4. i  j – 1 5. while i > 0 and A[i] > key 6. do A[i + 1]  A[i] 7. i  i – 1 8. A[i + 1]  key

Slide 4

Slide 4 text

4 The operation of Insertion-Sort 1 2 3 4 5 6 5 2 4 6 1 3 1 2 3 4 5 6 2 5 4 6 1 3 1 2 3 4 5 6 2 4 5 6 1 3 1 2 3 4 5 6 2 4 5 6 1 3 1 2 3 4 5 6 1 2 4 5 6 3 1 2 3 4 5 6 1 2 3 4 5 6 (a) (c) (e) (b) (d) (f)

Slide 5

Slide 5 text

5 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. Loop invariant: At the start of each iteration of the for loop of line 1-8, the subarray A[1,…, j – 1] consists of the elements originally in A[1,…, j – 1] but in sorted order.

Slide 6

Slide 6 text

6 2.2 Analyzing algorithms Analyzing an algorithm has come to mean predicting the resources that the algorithm requires. Resources: memory, communication, bandwidth, logic gate, time. Assumption: one processor, RAM constant-time instruction: arithmetic (add, subtract, multiply, divide, remainder, floor, ceiling); data movement (load, store, copy); control (conditional and unconditional bramch, subroutine call and return) Date type: integer and floating point Limit on the size of each word of data

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

8 Analysis of insertion sort Insertion-sort(A) 1. for j  2 to length[A] 2. do key  A[j] 3. *Insert A[j] into the sorted sequence A[1,…, j – 1] 4. i  j – 1 5. while i > 0 and A[i] > key 6. do A[i + 1]  A[i] 7. i  i – 1 8. A[i + 1]  key tj : the number of times the while loop test in line 5 is executed for the value of j. cost c1 c2 0 c4 c5 c6 c7 c8 cost n n – 1 n – 1 n – 1   n j j t 2    n j j t 2 ) 1 (    n j j t 2 ) 1 (

Slide 9

Slide 9 text

9 Analysis of insertion sort The running time T(n) = c1 n + c2 (n – 1) + c4 (n – 1) + c5 + c6 + c7 + c8 (n – 1) tj = 1, for j = 2, 3,…, n: Linear function on n T(n) = c1 n + c2 (n – 1) + c4 (n – 1) + c5 (n – 1) + c8 (n – 1) = (c1 + c2 + c4 + c5 + c8 )n – (c2 + c4 + c5 + c8 )   n j j t 2    n j j t 2 ) 1 (    n j j t 2 ) 1 (

Slide 10

Slide 10 text

10 Analysis of insertion sort tj = j, for j = 2, 3,…, n: Quadratic function on n T(n) = c1 n + c2 (n – 1) + c4 (n – 1) + c5 + c6 + c7 + c8 (n – 1) = n2 – (c1 + c2 + c4 + + c8 )n – (c2 + c4 + c5 + c8 ) ) 1 2 ) 1 ( (   n n ) 2 ) 1 ( (  n n ) 2 ) 1 ( (  n n ) 2 ( 7 6 5 c c c   ) 2 ( 7 6 5 c c c  

Slide 11

Slide 11 text

11 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. The average case is often as bad as the worst case. For example, the insertion sort. Again, quadratic function.

Slide 12

Slide 12 text

12 Order of growth In some particular cases, we shall be interested in average-case, or expect running time of an algorithm. It is the rate of growth, or order of growth, of the running time that really interests us.

Slide 13

Slide 13 text

13 2.3 Designing algorithms There are many ways to design algorithms: Incremental approach: insertion sort Divide-and-conquer: merge sort recursive: divide conquer combine

Slide 14

Slide 14 text

14 Pseudocode Merge sort Merge(A, p, q, r) 1. n1  q – p + 1 2. n2  r – q 3. create array L[1,…, n1 + 1] and R[1,…, n2 + 1] 4. for i  1 to n1 5. do L[i]  A[p + i – 1] 6. for j  1 to n2 7. do R[j]  A[q + j] 8. L[n1 + 1]   9. R[n2 + 1]  

Slide 15

Slide 15 text

15 Pseudocode 10. i  1 11. j  1 12. for k  p to r 13. do if L[i]  R[j] 14. then A[k]  L[i] 15. i  i + 1 16. else A[k]  R[j] 17. j  j + 1

Slide 16

Slide 16 text

16 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 2 4 5 7 1 2 3 6 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (a)

Slide 17

Slide 17 text

17 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 1 4 5 7 1 2 3 6 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (b)

Slide 18

Slide 18 text

18 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 1 2 5 7 1 2 3 6 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (c)

Slide 19

Slide 19 text

19 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 1 2 2 7 1 2 3 6 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (d)

Slide 20

Slide 20 text

20 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 1 2 2 3 1 2 3 6 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (e)

Slide 21

Slide 21 text

21 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 1 2 2 3 4 2 3 6 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (f)

Slide 22

Slide 22 text

22 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 1 2 2 3 4 5 3 6 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (g)

Slide 23

Slide 23 text

23 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 1 2 2 3 4 5 6 6 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (h)

Slide 24

Slide 24 text

24 The operation of lines 10-17 8 9 10 11 12 13 14 15 16 17 A … 1 2 2 3 4 5 6 7 … k 1 2 3 4 5 L 2 4 5 7  i 1 2 3 4 5 R 1 2 3 6  j (i)

Slide 25

Slide 25 text

25 Pseudocode 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)

Slide 26

Slide 26 text

26 The operation of Merge sort initial sequence 5 2 4 7 1 3 2 6

Slide 27

Slide 27 text

26 The operation of Merge sort initial sequence 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6

Slide 28

Slide 28 text

26 The operation of Merge sort initial sequence 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 5 2 4 7

Slide 29

Slide 29 text

26 The operation of Merge sort initial sequence 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 5 2 2 6 4 7 1 3

Slide 30

Slide 30 text

26 The operation of Merge sort initial sequence 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 5 2 2 6 4 7 1 3 5 2

Slide 31

Slide 31 text

26 The operation of Merge sort initial sequence 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 5 2 2 6 4 7 1 3 5 2 4 7

Slide 32

Slide 32 text

26 The operation of Merge sort initial sequence 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 5 2 2 6 4 7 1 3 5 2 4 7 1 3

Slide 33

Slide 33 text

26 The operation of Merge sort initial sequence 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 5 2 2 6 4 7 1 3 5 2 4 7 1 3 2 6

Slide 34

Slide 34 text

26 The operation of Merge sort 2 5 initial sequence merge 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 2 6 4 7 1 3 5 2 4 7 1 3 2 6

Slide 35

Slide 35 text

26 The operation of Merge sort 2 5 4 7 initial sequence merge merge 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 2 6 1 3 5 2 4 7 1 3 2 6

Slide 36

Slide 36 text

26 The operation of Merge sort 2 5 4 7 1 3 initial sequence merge merge merge 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 2 6 5 2 4 7 1 3 2 6

Slide 37

Slide 37 text

26 The operation of Merge sort 2 5 2 6 4 7 1 3 initial sequence merge merge merge merge 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6

Slide 38

Slide 38 text

26 The operation of Merge sort 2 4 5 7 2 5 2 6 4 7 1 3 initial sequence merge merge merge merge merge 5 2 4 7 1 3 2 6 1 3 2 6 5 2 4 7 1 3 2 6

Slide 39

Slide 39 text

26 The operation of Merge sort 2 4 5 7 1 2 3 6 2 5 2 6 4 7 1 3 initial sequence merge merge merge merge merge merge 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6

Slide 40

Slide 40 text

26 The operation of Merge sort 1 2 2 3 4 5 6 7 2 4 5 7 1 2 3 6 2 5 2 6 4 7 1 3 sorted sequence merge merge merge merge merge merge merge 5 2 4 7 1 3 2 6

Slide 41

Slide 41 text

27 Analysis of Merge sort Analyzing divide-and-conquer algorithms See Chapter 4. Analysis of merge sort T(n) = (nlogn) otherwise if ) ( ) ( ) / ( ) 1 ( ) ( c n n C n D b n aT n T         1 if 1 if ) ( ) 2 / ( 2 ) 1 ( ) (          n n n n T n T

Slide 42

Slide 42 text

28 Analysis of Merge sort where the constant c represents the time require to solve problem of size 1 as well as the time per array element of the divide and combine steps. 1 if 1 if ) 2 / ( 2 ) (        n n cn n T c n T

Slide 43

Slide 43 text

29 The construction of a recursion tree T(n) cn T(n/2) T(n/2) c(n/2) T(n/4) T(n/4) c(n/2) T(n/4) T(n/4) cn (a) (b) (c) c(n/2) c(n/4) c(n/4) c(n/2) c(n/4) c(n/4) cn c c c c c c c c lg n n cn cn cn cn Total: cn lg n + cn (d)

Slide 44

Slide 44 text

30 Outperforms insertion sort!