What is complexity of this code? (Big O) Is that linear?
问题 for(int i=0; i<array.length -1; i++){ if(array[i] > array[i+1]){ int temp = array[i]; array[i] = array[i+1]; array[i+1]=temp; i=-1; } } I think the code sorts the input array and that its worst case complexity is O(n). What is the correct big-O complexity of this code? 回答1: It's O(n^3), and it's an inefficient version of bubble sort. The code scans through the array looking for the first adjacent pair of out-of-order elements, swaps them, and then restarts from the beginning of the array. In