Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Main{
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- int
- size1 = 0,
- size2 = 0;
- try{
- System.out.println("Enter the size of array 1:");
- size1 = sc.nextInt();
- System.out.println("Enter the size of array 2:");
- size2 = sc.nextInt();
- sc.nextLine();
- }
- catch(InputMismatchException ex){
- System.out.println("Please input only number!");
- return;
- }
- String arr1[] = new String [size1];
- String arr2[] = new String [size2];
- try{
- System.out.println("Enter the values to the array 1:");
- for(int i = 0; i < size1; i++)
- arr1[i] = sc.nextLine();
- System.out.println("Enter the values to the array 2:");
- for(int i = 0; i < size2; i++)
- arr2[i] = sc.nextLine();
- }
- catch(InputMismatchException ex){
- System.out.println("Please input only numbers!");
- }
- int totalSize = size1 + size2;
- int index = 0;
- boolean mergeDone = false;
- String mergedSort[] = new String[totalSize];
- for(int i = 0; i < totalSize; i++){
- if(index < size1 && mergeDone == false){
- mergedSort[i] = arr1[index];
- index++;
- }
- else if(index == size1 && mergeDone == false){
- index = 0;
- i--;
- mergeDone = true;
- }
- else if(index < size2 && mergeDone == true){
- mergedSort[i] = arr2[index];
- index++;
- }
- }
- mergedSort = OrderArray(mergedSort);
- for(int i = 0; i < totalSize; i++)
- System.out.print(mergedSort[i] +" ");
- System.out.println();
- return;
- }
- public static String[] OrderArray(String arr[]){
- String temp;
- for(int i = 0; i < arr.length; i++){
- for(int j = 0; j < arr.length; j++){
- if(i == j)
- continue;
- if(arr[i].compareTo(arr[j]) < 0){
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- return arr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement