Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This method previously orders the vector O(nlogn) and then it
- * looks for the mode O(n). It is O(nlogn) + O(n) -
- * O(nlogn). It is DandC because of the Quicksort sorting
- * @param v Array used to calculate the mode
- * @param mo Array with the solution (value and repetitions)
- */
- public void mode2(int[]v, int[]mo) {
- Quicksort quicksort = new Quicksort();
- quicksort.sort(v);
- int n = v.length;
- mo[0] = v[0]; //first, we consider the first element as the mode
- mo[1] = 1; //..so the mode is repeated only once at this point
- int counter = 1;
- for (int i=1; i<n; i++) { //O(n)
- if (v[i] == v[i-1]) { //all repeated elements must be one after another
- counter++;
- if (counter > mo[1]) {
- mo[0] = v[i];
- mo[1] = counter;
- }
- }
- else counter=1; //when we begin with a non-repeated element, we restart the counter
- }//for
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement