sorting - What is the relative running time of this bubble sort derivative? -


public class sort {     public static void sort(int[] arr) {         (int = 0; < arr.length - 1; i++) {             if (arr[i] > arr[i + 1]) {                 /* swap */                 int temp = arr[i];                 arr[i] = arr[i + 1];                 arr[i + 1] = temp;                 /* make == -1 because @ end of loop, increments go 0 */                 = -1;             }         }     } } 

this different traditional bubble sort. have worse running time?

i believe still o(n^2), not see how worse...

funny beast.

lets take worst case, anti-sorted input:

9 8 7 6 5 4 3 2 1 

how many comparisons needed move 9 first last position?
1+2+3+4+5+6+7+8 = (n-1)*(n-2)/2

what sending 8 second last position?
1+2+3+4+5+6+7 = (n-2)*(n-3)/2

so, algorithm cubic in comparisons performed!

still, there's light @ end of tunnel:
still quadratic in number of swaps performed, standard bubble-sort.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -