Merge and sort two arrays

Write a program to merge and sort two arrays.

import java.util.*;

public class Main{
    public static void main(String [] args){
         int[]a = {1,2,3,4};
      int n = a.length;
      int[]b = {4,16,1,2,3,22};
      int m = b.length;
      int[]ar = new int[n+m];
      int size = ar.length;
        merge(a,b,ar,n,m);
        selectionSort(ar,size);
        printArray(ar,size);
    }
    public static void merge(int a[],int b[],int ar[],int n,int m){
        int i = 0, j = 0, k = 0;
        while (i < n) {
            ar[k] = a[i];
            i++;
            k++;
        }
         
        while (j < m) {
            ar[k] = b[j];
            j++;
            k++;
        }
    }
public static void selectionSort(int ar[],int n){
        int i,j,min_index;
        for(i=0;i<n;i++){
            min_index = i;
            for(j=i+1;j<n;j++){
                if(ar[j]<ar[min_index]){
                    min_index =j;
                }
            }
            int swap = ar[min_index];
            ar[min_index] = ar[i];
            ar[i] = swap;
        }
    }

     public static void printArray(int ar[],int n)
    {
        for(int i=0;i<n;i++)
        {
            System.out.print(ar[i]+" ");
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *