why is Insertion sort best case big O complexity O(n)?
问题 Following is my insertion sort code: void InsertionSort(vector<int> & ioList) { int n = ioList.size(); for (int i = 1 ; i < n ; ++i) { for (int j = 0 ; j <= i ; ++j) { //Shift elements if needed(insert at correct loc) if (ioList[j] > ioList[i]) { int temp = ioList[j]; ioList[j] = ioList[i]; ioList[i] = temp; } } } } The average complexity of the algorithm is O(n^2). From my understanding of big O notation, this is because we run two loops in this case(outer one n-1 times and inner one 1,2,..