java - What 's Big O notation on this specific code? -
i want know big o notation , big omega notation (worst case, best case) of code sorting algorithm , guess has o(n)
, omega(n)
:
public static void swap(int[] a, int i,int j){ int temp = 0; temp = a[i]; a[i] = a[j]; a[j] = temp; } public static int[] myalgorithm(int[] a, int n){ boolean done = true; int j =0; while(j<=(n-2)){ if(a[j]>a[j+1]){ swap(a,j,j+1); done = false; } j = j+1; } j = n-1; while(j>=1){ if(a[j]<a[j-1]){ swap(a, j-1,j); done = false; } j = j-1; } if(done==false){ myalgorithm(a,n); } return a; }
it's o(n^2) (for list [n, n-1, ..., 1]
), omega(n) [1, 2, ..., n]
.
Comments
Post a Comment