Running Time For Insertion Sort
问题 for (int p=1; p < a.size(); p++) { int tmp = a[p]; for(j=p; j>0 && tmp < a[j-1]; j--) { a[j] = a[j-1]; } a[j] = tmp; } I'm having trouble figuring out the worse case for Insertion sort. So, the array given is in descending order, and we want to sort it in ascending order. The outer loop goes through the array. So, it runs (n times). O(n) int tmp=a[p] ---- This statement gets executed n times. O(n) The inner loop get executed (1+2+3+4+5+6+.... +n-1) times. O(n^2) a[j]= tmp -------- This