I got a insertion sort algorithm below, it includes the nested for loop:
public InsertionSort( AnyType [] a ){
int m;
for( int n = 1; n < a.length; n++ ){
AnyType temp = a[n];
for( m = n; m > 0 && tmp.compareTo( a[ m - 1] ) <= 0; m-- )
a[ m ] = a[ m - 1 ];
a[ m ] = temp;
}
}
The first loop has running time O(N) and the second is O(N^2), so the total running time in nested loop should be O(N*N^2=N^3), am I right? But I know the fact that worst case should be O(N^2) in insertion sort, however my teacher changed this segment of code that book provides a little bit by replacing a[ m - 1] ) <= 0, instead of using a[ m - 1] ) < 0. So I'm confused that how come I calculate a case worse than the worst case. Anyone helps? Thanks in advance.