Download presentation
Presentation is loading. Please wait.
1
Chapter 2
2
Complexity Space Complexity Space Complexity Instruction Space Instruction Space Data Space Data Space Enviroment Space Enviroment Space Time Complexity Time Complexity Operation count Operation count Asymptotic Notation Asymptotic Notation
3
Instruction Space Operation a+b+b*c+(a+b-c)7(a+b)+4
4
Data Space Borland C++
5
Enviromental Space template T Rsum(T a[], int n) {// Return sum of numbers a[0:n - 1]. if (n > 0) return Rsum(a, n-1) + a[n-1]; return 0;} Recursive Program Rsum(a,n) Rsum(a,n-1) Rsum(a,n-2)... Rsum(a,1) Rsum(a,0)
6
Space Complexity Space Required Fixed PartVariable Part The Space Requirement S(P) = c + S P (instance characteristics)
7
Time Complexity Compilation Time Compilation Time Run Time Run Time n : instance characteristics
8
Example max element template int Max(T a[], int n) {// Locate the largest element in a[0:n-1]. int pos = 0; for (int i = 1; i < n; i++) if (a[pos] < a[i]) pos = i; return pos; } Total n-1 comparisons
9
Example Polynomial Evaluation template T PolyEval(T coeff[], int n, const T& x) {// Evaluate the degree n polynomial with // coefficients coeff[0:n] at the point x. T y = 1, value = coeff[0]; for (int i = 1; i <= n; i++) { // add in next term y *= x; value += y * coeff[i]; } return value; } Number of addition : n Number of Multiplication : 2n
10
Example (2) Polynomial Evaluation Number of addition : n Number of Multiplication : n Horner’s rule template T Horner(T coeff[], int n, const T& x) {// Evaluate the degree n polynomial with // coefficients coeff[0:n] at the point x. T value = coeff[n]; for (int i = 1; i <= n; i++) value = value * x + coeff[n - i]; return value; }
11
Example Rank The rank of an element in a sequence is the number of smaller elements in the sequence plus the number of equal elements that appear to its left Ex: a[4 3 9 3 7] : rank r[2 0 4 1 3] template void Rank(T a[], int n, int r[]) {// Rank the n elements a[0:n-1]. for (int i = 0; i < n; i++) r[i] = 0; //initialize // compare all element pairs for (int i = 1; i < n; i++) for (int j = 0; j < i; j++) if (a[j] <= a[i]) r[i]++; else r[j]++; } The total number of comparison : 1 + 2 + …+ n-1 = (n-1)*n/2
12
Example Rank Sort template void Rearrange(T a[], int n, int r[]) {// Rearrange the elements of a into sorted order // using an additional array u. T *u = new T [n+1]; // move to correct place in u for (int i = 0; i < n; i++) u[r[i]] = a[i]; // move back to a for (int i = 0; i < n; i++) a[i] = u[i]; delete [] u; } The number of elements moved : 2n
13
Example Selection Sort The number of comparison = 1 + 2 + 3 +… + n-1 = (n-1)*n/2 The number of elements moved : 3 (n-1) template void SelectionSort(T a[], int n) {// Sort the n elements a[0:n-1]. for (int size = n; size > 1; size--) { int j = Max(a, size); Swap(a[j], a[size - 1]); }
14
Example Buble Sort The number of comparison = 1 + 2 + 3 +… + n-1 = (n-1)*n/2 The number of elements moved : 3 (n-1) (worsed case) template void Bubble(T a[], int n) {// Bubble largest element in a[0:n-1] to right. for (int i = 0; i < n - 1; i++) if (a[i] > a[i+1]) Swap(a[i], a[i + 1]); } template void BubbleSort(T a[], int n) {// Sort a[0:n - 1] using bubble sort. for (int i = n; i > 1; i--) Bubble(a, i); }
15
What is worst case what is best case Sequential search Sequential search Best case : 1 Worst case : n On average :
16
Insertion into Sorted Array template void Insert(T a[], int & n, const T& x) {// Insert x into the sorted array a[0:n-1]. // Assume a is of size > n. int i; for (i = n-1; i >= 0 && x < a[i]; i--) a[i+1] = a[i]; a[i+1] = x; n++; // one element added to a } On average :
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.