I start C ++ and study the analysis algorithm: I write a method that returns the row number from a 2d array, has at most 1, each row from the input array is sorted and removes 0 when all 1 are sorted by the front, as
1,1,1,0,0 1,1,0,0,0 1,1,1,1,0 1,0,0,0,0 1,1,1,1,1
the method will return 5 from this array and here is the code:
int countone(int a[][]){ int count = 0, column = 0, row = 0, current = 0, max; bool end = true; do{ if(a[row][column]==1) { current++; column++; } if(a[row][column]==0) { column=0; if(count<current) { count = current; max = row; current = 0; } row++; if(a[row][column] != 1 && a[row][column] != 0) { end = false; return max; } } while(end)
the code has not yet been tested, so it may contain errors and errors, but this is not the main thing. I would like to know the cost of this method, but I have no idea how to calculate it.
The cost I want is T (n) runtime and Big-Oh record. If possible, the method should work in O (n) time (not O (n ^ 2))