Advertisement
Ronnie72428

MergeSort

Apr 2nd, 2021
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Main{
  4.    
  5.     public static void main(String args[]){
  6.        
  7.         Scanner sc = new Scanner(System.in);
  8.         int
  9.             size1 = 0,
  10.             size2 = 0;
  11.            
  12.         try{
  13.             System.out.println("Enter the size of array 1:");
  14.             size1 = sc.nextInt();
  15.            
  16.             System.out.println("Enter the size of array 2:");
  17.             size2 = sc.nextInt();
  18.             sc.nextLine();
  19.         }  
  20.         catch(InputMismatchException ex){
  21.             System.out.println("Please input only number!");
  22.             return;
  23.         }
  24.        
  25.         String arr1[] = new String [size1];
  26.         String arr2[] = new String [size2];
  27.        
  28.         try{
  29.             System.out.println("Enter the values to the array 1:");
  30.             for(int i = 0; i < size1; i++)
  31.                 arr1[i] = sc.nextLine();
  32.                
  33.             System.out.println("Enter the values to the array 2:");
  34.             for(int i = 0; i < size2; i++)
  35.                 arr2[i] = sc.nextLine();
  36.         }
  37.         catch(InputMismatchException ex){
  38.             System.out.println("Please input only numbers!");
  39.         }
  40.        
  41.         int totalSize = size1 + size2;
  42.         int index = 0;
  43.         boolean mergeDone = false;
  44.        
  45.         String mergedSort[] = new String[totalSize];
  46.        
  47.         for(int i = 0; i < totalSize; i++){
  48.             if(index < size1 && mergeDone == false){
  49.                 mergedSort[i] = arr1[index];
  50.                 index++;
  51.             }
  52.             else if(index == size1 && mergeDone == false){
  53.                 index = 0;
  54.                 i--;
  55.                 mergeDone = true;
  56.             }
  57.             else if(index < size2 && mergeDone == true){
  58.                 mergedSort[i] = arr2[index];
  59.                 index++;
  60.             }
  61.         }
  62.        
  63.         mergedSort = OrderArray(mergedSort);
  64.        
  65.         for(int i = 0; i < totalSize; i++)
  66.             System.out.print(mergedSort[i] +" ");
  67.            
  68.         System.out.println();
  69.        
  70.         return;
  71.     }
  72.    
  73.     public static String[] OrderArray(String arr[]){
  74.         String temp;
  75.        
  76.         for(int i = 0; i < arr.length; i++){
  77.             for(int j = 0; j < arr.length; j++){
  78.                 if(i == j)
  79.                     continue;
  80.                
  81.                 if(arr[i].compareTo(arr[j]) < 0){
  82.                     temp = arr[i];
  83.                     arr[i] = arr[j];
  84.                     arr[j] = temp;
  85.                 }
  86.             }
  87.         }
  88.    
  89.         return arr;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement