250 likes | 350 Views
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2). Parameter passing call by value and reference:
E N D
Lecture 4 Sept 4 • Goals: • chapter 1 (completion) • 1-d array examples • Selection sorting • Insertion sorting • Max subsequence sum • Algorithm analysis (Chapter 2)
Parameter passing • call by value and reference: • What are the outputs of the following program? • #include <iostream> • 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 <iostream> 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; }
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
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
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)
Implementing insert To insert a key x as the k-th key a c g k x = ‘b’ and k = 5 a c g k b
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); } }
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
Insertion step temp = A[j]; for (k = j – 1; k >= 0 && A[k] > temp; --k) A[k+1] = A[k]; A[k+1] = temp;
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; }
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:
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]. • Reselection_sort(vector<int> 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.
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).
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
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) ~ n2 comparisons Note the approximation we used: When n = 1000, n2 = one million, n2 – n = 999000 so the error in our estimate is 0.1%
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.
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
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)
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];
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.
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: n2 + 2n – 15 is O(n2) • 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. • (3n2 + 2 n + 1) (4 n – 5) is O(n3)
Max Subsequence Problem • Given a sequence of integers A1, A2, …, An, find the maximum possible value of a subsequence Ai, …, Aj. • Numbers can be negative. • You want a contiguous chunk with largest sum. • Example: -2, 11, -4, 13, -5, -2 • The answer is 20 (subsequence A2 through A4). • We will discuss three different algorithms, with time complexities O(n3), O(n2), and O(n). • With n = 106, algorithm 1 may take > 10 years; algorithm 3 will take a fraction of a second!
Algorithm 1 for Max Subsequence Sum 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; • Given A1,…,An , find the maximum value of Ai+Ai+1+···+Aj 0 if the max value is negative • Time complexity: O(n3)
Algorithm 2 • Idea: Given sum from i to j-1, we can compute the sum from i to j in constant time. • This eliminates one nested loop, and reduces the running time to O(n2). 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;
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 • The algorithm resets whenever prefix is < 0. Otherwise, it forms new sums and updates maxSum in one pass.