Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MergeSort
//Title of this code //'main' method must be in a class 'Rextester'. //Compiler version 1.7.0_51 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { int[] data={1, 2,5,7, 8, 3, 10}; int[] res=mergeSort(data, 0, data.length-1); for (int i=0;i<data.length;i++) { System.out.println(res[i]); } return; } private static int[] mergeSort(int[] data, int start, int end) { if (data==null||data.length==0) return data; int[] res=new int[end-start+1]; if (start==end) { res[0]=data[start]; return res; } //divide int mid=(start+end)/2; int[] data1=mergeSort(data, start, mid); int[] data2=mergeSort(data, mid+1, end); //merge int cur1=0; int cur2=0; int curn=0; while (cur1<=data1.length-1&&cur2<=data2.length-1) { if (data1[cur1]<=data2[cur2]) { res[curn]=data1[cur1]; cur1++; } else { res[curn]=data2[cur2]; cur2++; } curn++; } if (cur1==data1.length) { for (int i=cur2;i<data2.length;i++) { res[curn]=data2[i]; curn++; } } else { for (int i=cur1;i<data1.length;i++) { res[curn]=data1[i]; curn++; } } return res; } }
run
|
edit
|
history
|
help
0
Parse and flatten string
Ridhiverma
Abhay
Add Two Numbers saved in linked lists
Hilbert Curve
dimond of stars
Bubble Sort Java
Alllocate and release memory
JAVA regex for only allow numbers
else if statement