Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)

Similar presentations


Presentation on theme: "Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)"— Presentation transcript:

1 Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)

2 Parameter passing call by value and reference: What are the outputs of the following program? #include using namespace std; int f(int x) { x++; cout << x << endl; return x*x; } int main() { int x = 5; cout << f(x) << endl; cout << x << endl; } #include using namespace std; int f(int& x) { x++; cout << x << endl; return x*x; } int main() { int x = 5; cout << f(x) << endl; cout << x << endl; }

3 Parameter passing In c++, all parameter passing is done by value unless & is used preceding the name of the variable. Advantage of call by value: safer easier to understand and debug Disadvantage of call by value: not efficient (copying costs both in time and space) especially a problem when a large object is passed as parameter. call by reference is complementary

4 Parameter passing call by const reference: getting advantages of both conventions should be used when passing objects (especially when it is large) call by const reference: getting advantages of both conventions should be used when passing objects (especially when it is large) Constant return value from a function (The returned object can’t be modified.) Section 1.6 templates

5 Implementation of a list class list class with the following functionality: list is singly-linked, with a head pointer constructor(s) functions: insert(int item, int posn) length() delete(int posn) search(int item)

6 Implementing insert To insert a key x as the k-th key a k g c x = ‘b’ and k = 5 a k g c b

7 Recursive version of insert void insert(int x, int k) { // insert x as the k-th item of the list // assume that the list has at least k – 1 items Node* tmp = new Node(k); if (k == 1) { tmp->next = head; head = tmp; } else { List temp(head->next); temp.insert(x, k – 1); }

8 Insertion sorting At the start of the k-th cycle: A[0.. k–1] is sorted. At the end of the k-th cycle: A[k] is inserted into correct place so that A[0.. k] is sorted. Example: k = 5 Before: 1 5 9 13 21 11 23 20 4 31 8 After: 1 5 9 11 13 21 23 20 4 31 8

9 Insertion step temp = A[j]; for (k = j – 1; k >= 0 && A[k] > temp; --k) A[k+1] = A[k]; A[k+1] = temp;

10 Insertion sorting Repeat the insertion cycle for j = 1, 2, 3, …, n – 1. The complete code is as follows: for (j = 1; j < n; ++j) { temp = A[j]; for (k = j – 1; k >= 0 && A[k] > temp; --k) A[k+1] = A[k]; A[k+1] = temp; }

11 The following link contains an applet to animate various sorting (and other) algorithms: http://math.hws.edu/TMCM/java/xSortLab/ A screen shot is shown below:

12 Selection Sort Selection Sorting Algorithm: During the j-th pass (j = 0, 1, …, n – 2), we will examine the elements of the array a[j], a[j+1], …, a[n- 1] and determine the index min of the smallest key. Swap a[min] and a[j]. Re selection_sort(vector a) { if (a.size() == 1) return; for (int j = 0; j < n – 1; ++j) { min = j; for (int k= j+1; k<=n-1; ++k) if (a[k] < a[min]) min = k; swap a[min] and a[j]; } Selection sorting animation can be found in the same site.

13 Algorithm analysis Analysis is the process of estimating the number of computational steps performed by a program (usually as a function of the input size). Useful to compare different approaches. Can be done before coding. Does not require a computer (paper and pencil). Order of magnitude (rough) estimate is often enough. We will introduce a notation to convey the estimates. (O notation).

14 Insertion sorting - analysis for (j = 1; j < n; ++j) { temp = A[j]; for (k = j – 1; k >= 0 && A[k] > temp; --k) A[k+1] = A[k]; A[k+1] = temp; } How many comparisons are performed? Consider the j-th iteration of the inner loop. In the worst-case, the loop will be iterated j times. Each time, two comparisons are performed. Total = 2j

15 First iteration performs 2 x 1 comparisons Second iteration performs 2 x 2 comparisons.... n – 1 th iteration performs 2 x (n – 1) comparisons Total number of comparisons performed by insertion sorting = 2 x (1 + 2 + … + n – 1) = n (n – 1) ~ n 2 comparisons Note the approximation we used: When n = 1000, n 2 = one million, n 2 – n = 999000 so the error in our estimate is 0.1%

16 Analysis of selection sorting Consider the program to find the min number in an array: min = a[0]; for (j = 1; j < n; ++j) if (A[j] > min) min = A[j]; The number of comparisons performed is n – 1. loop starts with j = 1 and ends with j = n so the number of iterations = n – 1. In each iteration, one comparison is performed.

17 Selection sorting – analysis The inner loop: n – 1 comparisons during the first iteration of the inner loop n – 2 comparisons during the 2nd iteration of the inner loop.... 1 comparison during the last iteration of the inner loop Total number of comparisons = 1 + 2 + … + (n – 1) = n(n – 1)/ 2

18 General rule If there is a nested loop, analyze the inner loop and calculate the number of operations performed by the inner loop. Suppose the j-th iteration of the inner loop performs f(j) operations Suppose the value of j in the outer loop goes from 1 to m, then the total number of operations is f(1) + f(2) + … + f(m)

19 Exercise: What is the number of operations (additions) by the following program? for (j = 100; j < 200; ++j) if (j % 2 == 0) sum+= A[j];

20 Exercise: What is the number of operations (additions) by the following program? for (j = 100; j < 200; ++j) if (j % 2 == 0) sum+= A[j]; Answer: The number of iterations of the loop = 100, But the addition is performed in every other iteration so the number of additions performed = 50.

21 O (order) notation Definition: Let f(n) and g(n) be two functions defined on the set of integers. If there is a c > 0 such that f(n) <= c g(n) for all large enough n. Then, we say f(n) = O(g(n)). Example: n 2 + 2n – 15 is O(n 2 ) Rule: When the expression involves a sum, keep only the term with the highest power, drop the rest. You can also drop constant multiplying terms. (3n 2 + 2 n + 1) (4 n – 5) is O(n 3 )

22 Max Subsequence Problem w Given a sequence of integers A1, A2, …, An, find the maximum possible value of a subsequence Ai, …, Aj. w Numbers can be negative. w You want a contiguous chunk with largest sum. w Example: -2, 11, -4, 13, -5, -2 w The answer is 20 (subsequence A2 through A4). w We will discuss three different algorithms, with time complexities O(n 3 ), O(n 2 ), and O(n). w With n = 10 6, algorithm 1 may take > 10 years; algorithm 3 will take a fraction of a second!

23 int maxSum = 0; for( int i = 0; i < a.size( ); i++ ) for( int j = i; j < a.size( ); j++ ) { int thisSum = 0; for( int k = i; k <= j; k++ ) thisSum += a[ k ]; if( thisSum > maxSum ) maxSum = thisSum; } return maxSum; Algorithm 1 for Max Subsequence Sum w Given A 1,…,A n, find the maximum value of A i +A i+1 +···+A j 0 if the max value is negative n Time complexity: O(n 3 )

24 Algorithm 2 w Idea: Given sum from i to j-1, we can compute the sum from i to j in constant time. w This eliminates one nested loop, and reduces the running time to O(n 2 ). into maxSum = 0; for( int i = 0; i < a.size( ); i++ ) int thisSum = 0; for( int j = i; j < a.size( ); j++ ) { thisSum += a[ j ]; if( thisSum > maxSum ) maxSum = thisSum; } return maxSum;

25 25 Algorithm 3 int maxSum = 0, thisSum = 0; for( int j = 0; j < a.size( ); j++ ) { thisSum += a[ j ]; if ( thisSum > maxSum ) maxSum = thisSum; else if ( thisSum < 0 ) thisSum = 0; } return maxSum n The algorithm resets whenever prefix is < 0. Otherwise, it forms new sums and updates maxSum in one pass.


Download ppt "Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)"

Similar presentations


Ads by Google