list[p]=key; } System.out.println(“The sorted list is “); for(i=0;i<list.length;i++) System.out.println(list[i]+” }} “); Merge Sort Merging means combining two sorted lists into one sorted list. For this the elements from both the sorted lists are compared. The smaller of both the elements is then stored in the third array. The sorting is complete when all the elements from both the list are placed in the third list. Example: sort*******************/ import java.io.*; mergesort { public static void main(String args[]) throws IOException { DataInputStream in=new DataInputStream(System.in); int a[]=new int[5]; int b[]=new int[5]; int c[]=new int[10]; int i,j,k; System.out.println("Enter elements of fist array "); for(i=0;i<5;i++) a[i]=Integer.parseInt(in.readLine()); System.out.println("Enter elements of second array "); for(i=0;i<5;i++) b[i]=Integer.parseInt(in.readLine()); int t=0; for(i=0;i<4;i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } if(b[i]>b[j]) { t=b[i]; b[i]=b[j]; b[j]=t; } } } for(i=j=k=0;i<=9;) { if(a[i]<=b[j]) c[k++]=a[i++]; else c[k++]=b[j++];
list[p]=key; } System.out.println(“The sorted list is “); for(i=0;i<list.length;i++) System.out.println(list[i]+” “); }} Merge Sort Merging means combining two sorted lists into one sorted list. For this the elements from both the sorted lists are compared. The smaller of both the elements is then stored in the third array. The sorting is complete when all the elements from both the list are placed in the third list. Example: sort*******************/ import java.io.*; mergesort { public static void main(String args[]) throws IOException { DataInputStream in=new DataInputStream(System.in); int a[]=new int[5]; int b[]=new int[5]; int c[]=new int[10]; int i,j,k; System.out.println("Enter elements of fist array "); for(i=0;i<5;i++) a[i]=Integer.parseInt(in.readLine()); System.out.println("Enter elements of second array "); for(i=0;i<5;i++) b[i]=Integer.parseInt(in.readLine()); int t=0; for(i=0;i<4;i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } if(b[i]>b[j]) { t=b[i]; b[i]=b[j]; b[j]=t; } } } for(i=j=k=0;i<=9;) { if(a[i]<=b[j]) c[k++]=a[i++]; else c[k++]=b[j++];
Java%20Notes_Class_9.docx