Guest User

Geier

a guest
Jan 15th, 2020
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static void mergesort(int[] arr, int tiefe) {
  2.  
  3. String prae = "";
  4. for (int i = 0; i < tiefe; i++){
  5. prae += "\t"; }
  6.  
  7.  
  8.  
  9.  
  10. System.out.println("Tiefe: "+ tiefe +prae + Arrays.toString(arr));
  11. if(arr.length == 0 || arr.length == 1) {return;}
  12.  
  13. int half = arr.length/2;
  14. int[] left_arr = new int[half], right_arr= new int[arr.length-half];
  15.  
  16. /* fuelle left_arr und rigth_arr */
  17. for(int i=0;i<arr.length;i++) {
  18. if(i<half) {
  19. left_arr[i]=arr[i];}
  20. else { right_arr[i-half]=arr[i];}
  21. }
  22.  
  23. mergesort(left_arr,++tiefe);
  24. mergesort(right_arr,tiefe--);
  25.  
  26.  
  27. /* fuege (sortierte Arrays left_arr und right_arr in arr zusammen */
  28. for(int i=0, j=0, k=0; i<arr.length; i++) {
  29.  
  30. arr[i]=(j==left_arr.length)? right_arr[k++]:
  31. (k==right_arr.length) ? left_arr[j++]:
  32. (left_arr[j]<right_arr[k])? left_arr[j++]:
  33. right_arr[k++];
  34.  
  35. }
  36.  
  37. System.out.println("Tiefe: "+ tiefe + prae + Arrays.toString(arr));
  38. }
  39.  
  40. public static void main(String[] args) {
  41. int[] arr = new int[] {1,33,4,15,-2,52,249,24,25};
  42. System.out.println("Array vor der Sortierung: " +Arrays.toString(arr)+'\n');
  43. mergesort(arr,0);
  44. System.out.println("\nArray nach der Sortierung: " +Arrays.toString(arr));
  45.  
  46. }
RAW Paste Data