Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. package math;
  2. import java.util.Arrays;
  3.  
  4. public class Sort {
  5. public static double[] merge(double[] l, double[] h) {
  6. double[] q = new double[l.length + h.length];
  7. int cInL = 0;
  8. int cInH = 0;
  9. for(int i = 0; i < q.length; i++) {
  10. if(cInL != l.length && (cInH == h.length || l[cInL] < h[cInH])) {
  11. q[i] = l[cInL];
  12. cInL++;
  13. } else {
  14. q[i] = h[cInH];
  15. cInH++;
  16. }
  17. }
  18. return q;
  19. }
  20.  
  21. public static double[] mergeSort(double[] list) {
  22. if(list.length == 1) return list;
  23.  
  24. int med = list.length/2;
  25. double[] l = mergeSort(Arrays.copyOfRange(list, 0, med));
  26. double[] h = mergeSort(Arrays.copyOfRange(list, med, list.length));
  27.  
  28. return merge(l, h);
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement