fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. private int[] A;
  8. private int[] B;
  9. private int[] helperA;
  10. private int[] helperB;
  11.  
  12. private int length;
  13.  
  14. public static void main (String[] args){
  15. int[] As = {2,9,5,3};
  16. int[] Bs = {3,11,6,10};
  17. new Ideone().sort(As,Bs);
  18. }
  19.  
  20. public void sort(int[] As , int[] Bs) {
  21. A = As;
  22. B = Bs;
  23. length = A.length;
  24. this.helperA = new int[length];
  25. this.helperB = new int[length];
  26. mergesort(0, length - 1);
  27. for(int i = 0 ; i<length ; i++)
  28. System.out.println("(" + A[i] + "," + B[i]+ ")");
  29. }
  30.  
  31. private void mergesort(int low, int high) {
  32. // check if low issmaller then high, if not then the array is sorted
  33. if (low < high) {
  34. // Get the index of the element which is in the middle
  35. int middle = low + (high - low) / 2;
  36. // Sort the left side of the array
  37. mergesort(low, middle);
  38. // Sort the right side of the array
  39. mergesort(middle + 1, high);
  40. // Combine them both
  41. merge(low, middle, high);
  42. }
  43. }
  44.  
  45. private void merge(int low, int middle, int high) {
  46.  
  47. // Copy both parts into the helper array
  48. for (int i = low; i <= high; i++) {
  49. helperA[i] = A[i];
  50. helperB[i] = B[i];
  51. }
  52.  
  53. int i = low;
  54. int j = middle + 1;
  55. int k = low;
  56. // Copy the smallest values from either the left or the right side back
  57. // to the original array
  58. while (i <= middle && j <= high) {
  59. if (helperA[i] <= helperA[j]) {
  60. A[k] = helperA[i];
  61. B[k] = helperB[i];
  62. i++;
  63. } else {
  64. A[k] = helperA[j];
  65. B[k] = helperB[j];
  66. j++;
  67. }
  68. k++;
  69. }
  70. // Copy the rest of the left side of the array into the target array
  71. while (i <= middle) {
  72. A[k] = helperA[i];
  73. B[k] = helperB[i];
  74. k++;
  75. i++;
  76. }
  77. }
  78. }
Success #stdin #stdout 0.09s 380224KB
stdin
Standard input is empty
stdout
(2,3)
(3,10)
(5,6)
(9,11)