Download presentation
Presentation is loading. Please wait.
1
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2
2
Last example: recursion and linked list Suppose we want to insert a key x as a last item of a list. a k g c x = ‘b’ a k g c b
3
void insertLast(Node* &head, int k) { // insert k as the last item of the list whose // header is pointed to by head if (head == NULL){ Node* tmp = new Node(); tmp->key = x; tmp->next = NULL; head = tmp;} else insertLast(head->next, k); }
4
Algorithm analysis (Chapter 2) Analysis is the process of estimating the number of computational steps performed by a program (as a function of the input size). Useful to compare different algorithms. 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).
5
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
6
Insertion step temp = A[j]; for (k = j – 1; k >= 0 && A[k] > temp; k--) A[k+1] = A[k]; A[k+1] = temp;
7
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; }
8
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:
9
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]. 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]; }
10
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
11
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 is one million, n 2 – n = 999000 so the error in our approximation is 0.1%
12
Analysis of assignment operation Other operation: assignment How many assignments are performed in the worst-case? The inner-loop contains two assignments per iteration so during the j-th iteration, it performs 2j assignments. Total number of assignments: 2( 2 + 3 + … + n) = n(n+1) – 2 ~ n 2
13
Best and average cases Insertion sorting performs n 2 comparisons and n 2 assignments in the worst-case. What is the worst-case input? What about the best-case? What about the average? (Over all possible input orders). These will be discussed in the lab.
14
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.
15
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
16
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)
17
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];
18
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.