1public static void mergeSort(int[] a, int n) {
2 if (n < 2) {
3 return;
4 }
5 int mid = n / 2;
6 int[] l = new int[mid];
7 int[] r = new int[n - mid];
8
9 for (int i = 0; i < mid; i++) {
10 l[i] = a[i];
11 }
12 for (int i = mid; i < n; i++) {
13 r[i - mid] = a[i];
14 }
15 mergeSort(l, mid);
16 mergeSort(r, n - mid);
17
18 merge(a, l, r, mid, n - mid);
19}
20
21public static void merge(
22 int[] a, int[] l, int[] r, int left, int right) {
23
24 int i = 0, j = 0, k = 0;
25 while (i < left && j < right) {
26 if (l[i] <= r[j]) {
27 a[k++] = l[i++];
28 }
29 else {
30 a[k++] = r[j++];
31 }
32 }
33 while (i < left) {
34 a[k++] = l[i++];
35 }
36 while (j < right) {
37 a[k++] = r[j++];
38 }
39}
1public static void mergeSort(int[] a, int n) {
2 if (n < 2) {
3 return;
4 }
5 int mid = n / 2;
6 int[] l = new int[mid];
7 int[] r = new int[n - mid];
8
9 for (int i = 0; i < mid; i++) {
10 l[i] = a[i];
11 }
12 for (int i = mid; i < n; i++) {
13 r[i - mid] = a[i];
14 }
15 mergeSort(l, mid);
16 mergeSort(r, n - mid);
17
18 merge(a, l, r, mid, n - mid);
19}
20
1public static void merge(
2 int[] a, int[] l, int[] r, int left, int right) {
3
4 int i = 0, j = 0, k = 0;
5 while (i < left && j < right) {
6 if (l[i] <= r[j]) {
7 a[k++] = l[i++];
8 }
9 else {
10 a[k++] = r[j++];
11 }
12 }
13 while (i < left) {
14 a[k++] = l[i++];
15 }
16 while (j < right) {
17 a[k++] = r[j++];
18 }
19}
20
1import java.util.Random;
2import java.util.Scanner;
3
4public class MergeSort {
5 public static void main(String[] argh){
6 Scanner scanner = new Scanner(System.in);
7 System.out.println("Enter the size of you victor: ");
8 int size = scanner.nextInt();
9 int[] unSortedArr = new int[size];
10 unSortedArr = enterRandomValues(unSortedArr);
11 int[] sortedARR = loop(unSortedArr);
12 print(sortedARR);
13 }
14 public static int[] loop(int[] unSortedArr){
15 for(int start = 0 ; start < unSortedArr.length ; ++start){
16 int minIndex = findTheMiniIndex(unSortedArr,start);
17 swap(unSortedArr,minIndex,start);
18 }
19
20 return unSortedArr ;
21 }
22
23 public static int findTheMiniIndex(int[] arr, int start){
24 int index = start;
25 for(int i = start+1 ; i < arr.length ; ++i){
26 if(arr[index]>arr[i]){
27 index = i ;
28 }
29 }
30
31 return index;
32 }
33
34 public static void swap(int[] swapValOfArr,int minIndex,int start){
35 int temp = swapValOfArr[minIndex];
36 swapValOfArr[minIndex]= swapValOfArr[start] ;
37 swapValOfArr[start] = temp ;
38
39 }
40
41 public static void print(int[] sorteArr){
42 for(int i = 0 ; i < sorteArr.length ; ++i) {
43 System.out.println(sorteArr[i]);
44 }
45 }
46
47 public static int[] enterRandomValues(int[] unSortedArr){
48 Random random = new Random();
49 for(int i = 0 ; i< unSortedArr.length; ++i){
50 unSortedArr[i] =random.nextInt(1000);
51 }
52 return unSortedArr;
53 }
54}
55
1// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
2// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
3
4function merge(list, start, midpoint, end) {
5 const left = list.slice(start, midpoint);
6 const right = list.slice(midpoint, end);
7 for (let topLeft = 0, topRight = 0, i = start; i < end; i += 1) {
8 if (topLeft >= left.length) {
9 list[i] = right[topRight++];
10 } else if (topRight >= right.length) {
11 list[i] = left[topLeft++];
12 } else if (left[topLeft] < right[topRight]) {
13 list[i] = left[topLeft++];
14 } else {
15 list[i] = right[topRight++];
16 }
17 }
18}
19
20function mergesort(list, start = 0, end = undefined) {
21 if (end === undefined) {
22 end = list.length;
23 }
24 if (end - start > 1) {
25 const midpoint = ((end + start) / 2) >> 0;
26 mergesort(list, start, midpoint);
27 mergesort(list, midpoint, end);
28 merge(list, start, midpoint, end);
29 }
30 return list;
31}
32
33mergesort([4, 7, 2, 6, 4, 1, 8, 3]);
1public class JavaMergeSort
2{
3 void sorting(int[] num, int left, int main, int right)
4 {
5 // finding size of two sub arrays
6 int list1 = main - left + 1;
7 int list2 = right - main;
8 // creating temporary array
9 int[] L = new int[list1];
10 int[] R = new int[list2];
11 // copying data to temporary array
12 for(int a = 0; a < list1; ++a)
13 L[a] = num[left + a];
14 for(int b = 0; b < list2; ++b)
15 R[b] = num[main + 1+ b];
16 // existing index of first and second sub array
17 int p = 0, q = 0;
18 // existing index of merged sub array
19 int r = left;
20 while(p < list1 && q < list2)
21 {
22 if(L[p] <= R[q])
23 {
24 num[r] = L[p];
25 p++;
26 }
27 else
28 {
29 num[r] = R[q];
30 q++;
31 }
32 r++;
33 }
34 // copying remaining elements of L[] array
35 while(p < list1)
36 {
37 num[r] = L[p];
38 p++;
39 r++;
40 }
41 // copying remaining elements of R[] array
42 while(q < list2)
43 {
44 num[r] = R[q];
45 q++;
46 r++;
47 }
48 }
49 // function that sorts
50 void sort(int[] arrNum, int l, int r)
51 {
52 if(l < r)
53 {
54 // finding middle point
55 int m = (l + r) / 2;
56 // sorting first and second list
57 sort(arrNum, l, m);
58 sort(arrNum , m+1, r);
59 // merging sorted list
60 sorting(arrNum, l, m, r);
61 }
62 }
63 // display array
64 static void displayArray(int[] arr)
65 {
66 int number = arr.length;
67 for(int a = 0; a < number; ++a)
68 System.out.print(arr[a] + " ");
69 System.out.println();
70 }
71 public static void main(String[] args)
72 {
73 int[] arrNumbers = {33, 00, 55, 11, 22, 44};
74 System.out.println("Before sorting - ");
75 displayArray(arrNumbers);
76 JavaMergeSort obj = new JavaMergeSort();
77 obj.sort(arrNumbers, 0, arrNumbers.length - 1);
78 System.out.println("\nAfter sorting - ");
79 displayArray(arrNumbers);
80 }
81}