Bangeev

merge sort na obj

Apr 4th, 2022
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package course.java.algorithms.sorting;
  2.  
  3. import java.util.StringJoiner;
  4.  
  5. class MyIntMergeSort implements Comparable<MyIntMergeSort> {
  6.  
  7.     int n;
  8.     int val;
  9.  
  10.     public MyIntMergeSort(int n, int val) {
  11.         this.n = n;
  12.         this.val = val;
  13.     }
  14.  
  15.  
  16.     @Override
  17.     public int compareTo(MyIntMergeSort other) {
  18.         return Integer.compare(this.n, other.n);
  19.     }
  20.  
  21.     @Override
  22.     public String toString() {
  23.         return new StringJoiner(", ", MyInt.class.getSimpleName() + "[", "]")
  24.                 .add("" + n)
  25.                 .add("" + val)
  26.                 .toString();
  27.     }
  28. }
  29.  
  30.  
  31. public class MergeSort {
  32.  
  33.     public static void main(String[] args) {
  34.         int[] a = {34, 4565, 4, 345, 34, 67, 4, 56, 345, 73, 986, 53, 12, 7, 85};
  35.         MyIntMergeSort[] myIntMergeSorts = new MyIntMergeSort[a.length];
  36.         for (int i = 0; i < a.length; i++) {
  37.             myIntMergeSorts[i] = new MyIntMergeSort(a[i], i);
  38.         }
  39.         //insertionSort(myInts);
  40.         /*bubbleSort(myInts);
  41.         System.out.println(Arrays.toString(myInts));*/
  42.         mergeSort(myIntMergeSorts);
  43.         for (MyIntMergeSort element : myIntMergeSorts){
  44.             System.out.println(element);
  45.         }
  46.     }
  47.  
  48.     private static void mergeSort(MyIntMergeSort[] myIntMergeSorts) {
  49.         int inputLength = myIntMergeSorts.length;
  50.         if (inputLength < 2) {
  51.             return;
  52.         }
  53.  
  54.         int midIndex = inputLength / 2;
  55.  
  56.         MyIntMergeSort[] leftHalf = new MyIntMergeSort[midIndex];
  57.         MyIntMergeSort[] rightHalf = new MyIntMergeSort[inputLength - midIndex];
  58.  
  59.         for (int i = 0; i < midIndex; i++) {
  60.             leftHalf[i] = myIntMergeSorts[i];
  61.         }
  62.         for (int i = midIndex; i < inputLength; i++) {
  63.             rightHalf[i - midIndex] = myIntMergeSorts[i];
  64.         }
  65.  
  66.         mergeSort(leftHalf);
  67.         mergeSort(rightHalf);
  68.        finalMerge(myIntMergeSorts, leftHalf, rightHalf);
  69.  
  70.     }
  71.  
  72.     private static void finalMerge(MyIntMergeSort[] myIntMergeSorts, MyIntMergeSort[] leftHalf, MyIntMergeSort[] rightHalf) {
  73.         int leftLength = leftHalf.length;
  74.         int rightLength = rightHalf.length;
  75.  
  76.         int i = 0;
  77.         int j = 0;
  78.         int k = 0;
  79.  
  80.         while (i < leftLength && j < rightLength) {
  81.             if (leftHalf[i].compareTo(rightHalf[j]) <= 0) {
  82.  
  83.                 myIntMergeSorts[k] = leftHalf[i];
  84.                 i++;
  85.             }else {
  86.                 myIntMergeSorts[k] = rightHalf[j];
  87.                 j++;
  88.             }
  89.             k++;
  90.         }
  91.         while (i < leftLength){
  92.             myIntMergeSorts[k] = leftHalf[i];
  93.             i++;
  94.             k++;
  95.         }
  96.         while (j < rightLength){
  97.             myIntMergeSorts[k] = rightHalf[j];
  98.             j++;
  99.             k++;
  100.         }
  101.  
  102.     }
  103.  
  104.  
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment