2.2-1:
O(n^3)
2.2-2:
Selection-Sort(A):
for( i=0 ; i<A.size()-1 ; i++){
max = A[i]; index = 0;
for(j=i+1; j<A.size(); j++){
if(max < A[j]){
max = A[j];
index = j;
}
}
// then swap the next max and the current element
A[j] = A[i];
A[i] = max;
}
2.2-3:
Say we're sorting n elements
Worst Case: n
Best Case: 1
Average: (1 + 2 + … + n)/n = (n+1)/2
2.2-4:
Know exactly what's the best case, then print it.
(I remember there was an UVA problem simply asking for an answer)