2.3 Time Complexity Example 2.7(program1.31) finding the largest number in a0 n-1 template<class t> int Max(t all, int n) f/locate the largest element in a[0: n -11 int pos=0 for(int F-l; K<n; 1++) if(alposkali] pos=i; return pos
2.3 Time Complexity Example 2.7(program1.31) finding the largest number in a[0:n-1] template<class T> 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; }
2. 3 Time Complexity Analysis of max function each iteration of the for loop makes one comparison, so the total number of element comparison is n-1
2.3 Time Complexity Analysis of Max function: each iteration of the for loop makes one comparison, so the total number of element comparison is n-1
2.3 Time Complexity Example 2. 11(program 2.7) selection sort template<class T> void Selection Sort(T all, int n) (//sort the n number in a0 n-1 for(int size=n; size>1; size--) fint j=Max(a, size) swap(all, a[size-ID)
2.3 Time Complexity Example 2.11(program 2.7) selection sort template<class T> void SelectionSort(T a[],int n) {//sort the n number in a[0:n-1]. for(int size=n;size>1;size--) {int j=Max(a,size); swap(a[j],a[size-1]); } }