Advertisement
Guest User

agasd

a guest
Feb 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. public static int[] MergeSort(int[] array){
  2. System.out.println("This is my length:" + array.length);
  3.  
  4.  
  5. // keys for merging and the temporary array
  6. int i = 0; int j;
  7. int [] tempArray = new int[array.length];
  8. int spiderman;
  9. // sets j
  10. if((array.length%2) == 0){
  11. j = array.length/2 ;
  12. } else{
  13. j = (array.length-1)/2;
  14. }
  15. // tells spiderman what to do
  16. if((array.length%2) == 0){
  17. spiderman = array.length/2 ;
  18. } else{
  19. spiderman = (array.length-1)/2;
  20. }
  21. // copies first and second half of the array into 2 arrays
  22.  
  23. int[] first = new int[j];
  24. int[] second = new int[array.length-j];
  25. /*
  26. int[] first = Arrays.copyOfRange(array, 0, (j-1));
  27. int[] second = Arrays.copyOfRange(array, j, (array.length-1));
  28. */
  29. for(int k = 0; k < (j-1); k++){
  30. first[k] = array[k];
  31. }
  32. for (int l = j; l < (second.length-1);l++){
  33. second[l] = array[l];
  34. }
  35.  
  36. //if lists are larger than 1, call mergesort on these lists.
  37.  
  38. if(first.length > 1){
  39. MergeSort(first);
  40. }
  41. if(second.length > 1){
  42. MergeSort(second);
  43. }
  44.  
  45. // used for counting
  46. int q = 0; int p = 0;
  47.  
  48. // used to remember where i left off, when the rest of the remaining array needs to be added
  49. int tempo = 0; int dumbo = 0;
  50.  
  51. // merges divded lists.
  52. System.out.println(first.length + " and " + second.length);
  53. while(q < first.length && p < second.length){
  54. if(first[q] <= second[p]){
  55. tempArray[i] = first[q];
  56. q++;
  57. dumbo = q;
  58. i++;
  59. } else{
  60. tempArray[i] = second[p];
  61. p++;
  62. tempo = p;
  63. i++;
  64. }
  65. }
  66. System.out.println(i);
  67. if(q == first.length && !(p==array.length)){
  68. for(int f = tempo; f < first.length ;f++){
  69. tempArray[i] = first[f];
  70. i++;
  71. }
  72. }
  73.  
  74.  
  75. if(p == second.length && !(q == spiderman)){
  76. for(int g = dumbo; g < second.length ;g++){
  77. tempArray[i] = second[g];
  78. i++;
  79. }
  80. }
  81. for(int k = 0; k<tempArray.length; k++){
  82. System.out.println("temp array at place k:" + tempArray[k]);
  83. }
  84. System.out.println(i + " Is i");
  85. // copy tempArray to array.
  86. array = Arrays.copyOfRange(tempArray, 0, (tempArray.length - 1));
  87. System.out.println(i + " Is i");
  88. return array;
  89. }
  90.  
  91. public static void main(String[] args) {
  92.  
  93.  
  94. // used to create 2 large lists to do analysis of runtime.
  95. Random r = new Random();
  96. int p = r.nextInt(50000);
  97. int[] list = new int[p];
  98. int[] list2 = new int[p];
  99. for(int q=0; q < p; q++){
  100. list[q] = r.nextInt(10000);
  101. list2[q] = r.nextInt(10000);
  102.  
  103. }
  104. /*
  105. //first array insertion
  106. int[] array = {21, 50, 30, 15, 20, 55, 92, 82, 44, 5, 10, 21};
  107. System.out.println("This is my current array: "+ Arrays.toString(array));
  108. System.out.println("Length of this array: " +array.length);
  109. InsertionSort(array);
  110. System.out.println("This is my new array: "+ Arrays.toString(array));
  111. */
  112. /*
  113. // Random array insertion
  114. System.out.println("This is my current (random)array: "+ Arrays.toString(list));
  115. System.out.println("Length of this random array: " +list.length);
  116. InsertionSort(list);
  117. System.out.println("This is my new (random)array: "+ Arrays.toString(list));
  118. */
  119.  
  120. System.out.println("TIME FOR MERGE!!");
  121. //first array merge
  122. int[] array1 = {21, 50, 30, 15, 20, 55, 92, 82, 44, 5, 10, 21};
  123. System.out.println("This is my current array: "+ Arrays.toString(array1));
  124. System.out.println("Length of this array: " +array1.length);
  125.  
  126. long n = System.currentTimeMillis();
  127. System.out.println("Time when Merge starts: "+n+" but lets call it 0.");
  128. MergeSort(array1);
  129. long m = System.currentTimeMillis();
  130. System.out.println("Time when Merge is done: "+m+" difference is: "+(m-n));
  131.  
  132. System.out.println("This is my new array: "+ Arrays.toString(array1));
  133. /*
  134. // Random array merge
  135. System.out.println("This is my current (random)array: "+ Arrays.toString(list2));
  136. System.out.println("Length of this random array: " +list2.length);
  137. MergeSort(list2);
  138. System.out.println("This is my new (random)array: "+ Arrays.toString(list2));
  139.  
  140. */
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement