Advertisement
Jaydeep999997

Multi Threaded Merge Sort

Oct 27th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | Source Code | 0 0
  1. import java.util.Random;
  2.  
  3. class Demontration {
  4.   public static void main(String[] args) throws Exception {
  5.     MultiThreadedMergeSort.runTest();
  6.   }
  7. }
  8.  
  9. class MultiThreadedMergeSort {
  10.  
  11.   private final int[] duplicate;
  12.  
  13.   public MultiThreadedMergeSort(int size) {
  14.     duplicate = new int[size];
  15.   }
  16.  
  17.   public void sort(int left, int right, int[] array) throws InterruptedException {
  18.     if (left >= right) {
  19.       return;
  20.     }
  21.     int mid = (left + right) >> 1;
  22.  
  23.     Thread sortLeft =
  24.         new Thread(
  25.             () -> {
  26.               try {
  27.                 sort(left, mid, array);
  28.               } catch (InterruptedException e) {
  29.  
  30.               }
  31.             });
  32.  
  33.     Thread sortRight =
  34.         new Thread(
  35.             new Runnable() {
  36.  
  37.               @Override
  38.               public void run() {
  39.                 try {
  40.                   sort(mid + 1, right, array);
  41.                 } catch (InterruptedException e) {
  42.  
  43.                 }
  44.               }
  45.             });
  46.  
  47.     sortLeft.start();
  48.     sortRight.start();
  49.  
  50.     sortLeft.join();
  51.     sortRight.join();
  52.  
  53.     for (int k = left; k <= right; k++) {
  54.       duplicate[k] = array[k];
  55.     }
  56.  
  57.     int i = left;
  58.     int j = mid + 1;
  59.     int index = left;
  60.  
  61.     while (i <= mid && j <= right) {
  62.       if (duplicate[i] < duplicate[j]) {
  63.         array[index++] = duplicate[i++];
  64.       } else {
  65.         array[index++] = duplicate[j++];
  66.       }
  67.     }
  68.  
  69.     while (i <= mid) {
  70.       array[index++] = duplicate[i++];
  71.     }
  72.  
  73.     while (j <= right) {
  74.       array[index++] = duplicate[j++];
  75.     }
  76.   }
  77.  
  78.   public static void runTest() throws InterruptedException {
  79.     Random random = new Random(System.currentTimeMillis());
  80.     int size = 10;
  81.  
  82.     MultiThreadedMergeSort multiThreadedMergeSort = new MultiThreadedMergeSort(size);
  83.     int[] array = new int[size];
  84.     for (int i = 0; i < size; i++) {
  85.       array[i] = random.nextInt(100);
  86.     }
  87.     System.out.println("Before sorting");
  88.     printArray(array);
  89.  
  90.     multiThreadedMergeSort.sort(0, size - 1, array);
  91.  
  92.     System.out.println("After sorting");
  93.     printArray(array);
  94.   }
  95.  
  96.   private static void printArray(int[] array) {
  97.     for (int i = 0; i < array.length; i++) {
  98.       System.out.print(array[i] + " ");
  99.     }
  100.     System.out.println();
  101.   }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement