Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. /**
  2. * This method previously orders the vector O(nlogn) and then it
  3. * looks for the mode O(n). It is O(nlogn) + O(n) -
  4. * O(nlogn). It is DandC because of the Quicksort sorting
  5. * @param v Array used to calculate the mode
  6. * @param mo Array with the solution (value and repetitions)
  7. */
  8. public void mode2(int[]v, int[]mo) {
  9. Quicksort quicksort = new Quicksort();
  10. quicksort.sort(v);
  11.  
  12. int n = v.length;
  13. mo[0] = v[0]; //first, we consider the first element as the mode
  14. mo[1] = 1; //..so the mode is repeated only once at this point
  15. int counter = 1;
  16. for (int i=1; i<n; i++) { //O(n)
  17. if (v[i] == v[i-1]) { //all repeated elements must be one after another
  18. counter++;
  19. if (counter > mo[1]) {
  20. mo[0] = v[i];
  21. mo[1] = counter;
  22. }
  23. }
  24. else counter=1; //when we begin with a non-repeated element, we restart the counter
  25. }//for
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement