Advertisement
Guest User

hopeful

a guest
Feb 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. public static void mergeSort(int[] unsorted) {
  2. int[] copy = new int[unsorted.length];
  3. for (int size=1; size<unsorted.length; size*=2) {
  4. for (int i=0; i<unsorted.length; i+=2*size) {
  5. merge(unsorted, copy, i, i+size, i+2*size);
  6. }
  7. }
  8. }
  9.  
  10.  
  11.  
  12. public static void merge(int[] unsorted, int[] copy, int low, int mid, int high) {
  13. if (mid >= unsorted.length) {
  14. return;
  15. }
  16. if (high > unsorted.length) {
  17. high = unsorted.length;
  18. }
  19. int i = low, j = mid;
  20. for (int k = low; k < high; k++) {
  21. if (i == mid) {
  22. copy[k] = unsorted[j++];
  23. }
  24. else if (j == high) {
  25. copy[k] = unsorted[i++];
  26. }
  27. else if (unsorted[j] < unsorted[i]) {
  28. copy[k] = unsorted[j++];
  29. }
  30. else {
  31. copy[k] = unsorted[i++];
  32. }
  33. }
  34. for (int k = low; k < high; k++)
  35. {
  36. unsorted[k] = copy[k];
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement