Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Merge Sort in Java
import java.util.Scanner; class Rextester { private static Scanner sc = new Scanner (System.in); private static void mergeSort (int[] a, int l, int r) { if (l < r) { int mid = l + (r - l) / 2; mergeSort (a, l, mid); mergeSort (a, mid + 1, r); int i = l, j = mid + 1; int k = 0; int[] merge = new int[r - l + 1]; while (i <= mid && j <= r) merge[k++] = a[i] < a[j] ? a[i++] : a[j++]; while (i <= mid) merge[k++] = a[i++]; while (j <= r) merge[k++] = a[j++]; for (i = l; i <= r; ++i) a[i] = merge[i - l]; } } public static void main (String[] args) { int[] a = new int[sc.nextInt()]; for (int i = 0; i < a.length; ++i) a[i] = sc.nextInt(); mergeSort (a, 0, a.length - 1); for (int i = 0; i < a.length; ++i) System.out.print (a[i] + " "); } }
run
|
edit
|
history
|
help
0
1.6
Frequecy count hashmap
Vasanth Selvaraj
Hi
Even Numbers
1a
Job and Trait Generator
bikeride
batman
Size and signedness of Java ints and longs