Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
merge sort by java
//'main' method must be in a class 'Rextester'. //openjdk version '11.0.5' import java.util.*; import java.lang.*; class Rextester { static class Info { public String admno; public String name; public int pack; Info(String admno, String name, int pack) { this.admno=admno; this.name=name; this.pack=pack; } } static void merge(Info a[],int l,int mid,int r) { int xx=mid - l + 1; int yy=r - mid; Info larr[]=new Info[xx]; Info rarr[]=new Info[yy]; for(int i=0;i<xx;i++) { larr[i].admno=a[l+i].admno; larr[i].name=a[l+i].name; larr[i].pack=a[l+i].pack; } for(int i=0;i<yy;i++) { rarr[i].admno=a[mid+1+i].admno; rarr[i].name=a[mid+1+i].name; rarr[i].pack=a[mid+1+i].pack; } int i=0,j=0; int k=l; while(i < xx && j < yy) { if(larr[i].pack <= rarr[j].pack) { a[k]=larr[i]; i++; k++; } else { a[k]=rarr[j]; j++; k++; } } while( i < xx) { a[k++]=larr[i]; i++; } while( j < yy) { a[k++]=rarr[j]; j++; } } static void sort(Info a[], int l,int r) { if(l < r) { int mid=l + (r-l) /2; sort(a,l,mid); sort(a,mid+1,r); merge(a,l,mid,r); } } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int k=sc.nextInt(); Info a[]=new Info[k]; String x,y; int z; for(int i=0;i<k;i++) { x=sc.next(); y=sc.next(); z=sc.nextInt(); a[i]=new Info(x,y,z); } sort(a,0,k-1); for(int i=0;i<k;i++) { System.out.print(a[i].admno+" "); System.out.print(a[i].name+" "); System.out.println(a[i].pack); } } }
run
|
edit
|
history
|
help
0
Table for 20
4.b
Abhay
hello_world
4c
Alphabets
Display Page
JAVA İLE CELCIUS HESAPLAMA - Fahrenheit to Celsius with Java
pow x^n
1