merge sorting java

Solutions on MaxInterview for merge sorting java by the best coders in the world

showing results for - "merge sorting java"
Jenna
10 Mar 2018
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}
Alex
13 Apr 2019
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
Martina
03 Feb 2018
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
Sofia
07 Oct 2019
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}
Alberto
30 Sep 2016
1public class MergeSort
2{
3public static void main(String[] args)
4{
5int i;
6int values[] = new int[11];
7for(i=0; i<values.length; i++) { values[i]=values.length-i; }
8print(values);
9sort(values,0,values.length-1);
10print(values);
11}
12public static void sort(int[] numbers, int p,int r)
13{
14int q;
15if(p<r)
16{
17q = (p+r)/2;
18sort(numbers,p,q);
19sort(numbers,q+1,r);
20merge(numbers,p,q,r);
21}
22}
23/**
24* p, mid, and r are indices into the array such that p <= mid < r.
25* The procedure assumes that the subarrays 'numbers[p..mid]' and 'numbers[mid+1..r]' are
26* in sorted order. It merge them to form a single sorted subarray that replaces
27* the current subarray 'numbers[p..r]'.
28*/
29private static void merge(int[] values, int p, int mid, int r)
30{
31int i,j,k;
32int n1 = mid - p + 1;
33int n2 = r - mid;
34int[] left = new int[n1+1];
35int[] right = new int[n2+1];
36for(i=0; i<n1; i++)
37{
38left[i] = values[ p + i ];
39}
40for(j=0; j<n2; j++)
41{
42right[j] = values[mid + j + 1];
43}
44left[n1] = Integer.MAX_VALUE;
45right[n2] = Integer.MAX_VALUE;
46i=0; j=0;
47for(k=p; k<=r; k++)
48{
49if(left[i]<=right[j])
50{
51values[k] = left[i];
52i = i + 1;
53}
54else
55{
56values[k] = right[j];
57j = j + 1;
58}
59}
60}
61public static void print(int[] numbers)
62{
63int i;
64for(i=0; i<numbers.length; i++)
65{
66System.out.print(numbers[i]+ " ");
67}
68System.out.println();
69}
70}
Abigail
23 Mar 2016
1non va bene sta cosa
queries leading to this page
merge sorting javamerge sort java algorithmmerge sort array in javamerge and merge sort javamerge sort 2 arrays javamergesort sample inputmergesort function javamerge sort javavmerge javajava sort array mergesortjava merge sort utilmerge sort recursive javajava function merge sortmerge sort in java in builtmerge sort java one methodhow to merge two arrays and sort in javamerge sort java code recursivejava merge sort inbuilt functionmerge sort algorithm java in ascending orderjava program using merge sort techniquesmerge sort explained javamerge sort function codemerge sort java librarymergesort java implementationjava mergesort examplemergesort java codemerge sort two array in javamerge sort javamerge sort in java examplemrege sort javamerge sort two arrays javamerge sort algorithm in javamerge sort array javamergesort class in javamerge sort teo array in javamerge sort program in jaagetting a value from merge sort javamerge sort java algorithmsdoes java have merge sort built inoptimized merge sort javahow to make own sort in merge sort javajava merge sortmerge sort explanation in javamerge sort algorithm java implementationmplement merge sort using array javamerge sorting program in javamerge sort example java step by stepmerge sort i javamergesort java code recursivejava merge sort recursivehow does merge sort work javajava merge sort algorithmhow t o merge three sorted aray in java merge sort in javamerge sort in descending order javamerge sort built in functions javahow does mergesort work javamerge sort java functionmerge sort algrorithm javamerge sort using recursion in javajava merge sort programhow to implement merge sort in an arraymerge sort java example codemerge sort jaarecursiv mergesort javajava unite algorithmhow to merge many sorted arrays in javajava mergesortmerge two sorted array and sort array javassimple merge sort javamergesort in javahow to sort 2 arrays together in javamerge sorted array in javamerge sort java recursivemerge sort java gfgbuiltin merge sort javamerge sort serial implementation in javamerge sort using javawhy does java use merge sort 22mergesort java 22mergesort javahungry2 way merge sort in javamerge osort in jvaa 27merge sort java arraysmerge arrays javamerge sorting in javahava merge sortmerge sort code in javajava merge sort examplemerge sort algorithm jaahow to merge sort in javamergosort javamerge sort descending order javamerge sort java codeimplementing merge sort in javahow to implement merge sort in java from scratchsimple merge sort implementation javajava merger sorthow to combined to implementation in javacode of merge sort in javamerse sort javarecursive merge sort javamerge sort algorithm implementation in javamerge sort recursive in javamerge sort java array implementationcan you call merge sort in javajava array mergemerger sort using javaimport mergesort javaimplement merge sort javahow to do merge sort alogorithim javaefficient mergesort javamerge sort a array in javamerge method of merge sort javamerger sort javamergesort algorithm code javajava array merge sortsort an array using mergesort javamerge arrays arrays sort javajava code for merge sortmerge sortnalgorithm javamerge sprt javamergesort java recursivemerge sort with one array javahow to merge arrays in javajava merge sort with only input arrayjava mergesort algorithmdoing a merge sort with arrays javamerge sort java implementationmerge sort java programmerge sort java recursive codemerge sort in java programmerge method for merge sort javawhy does java use merge sort in arraysmerge sort code javamerge sort code java for sort and reversemerge and sort two arrays in javamerge sort program in java using recursionmergesort java programmergesort java princehow to write a merge sort array method ni javajava 3a mergesortmerge sort an array in javamerge sort library in javamerge two arrays in sorted order javawhat is merge sort in javamerge sort java simplemerge sorting algorithm javamerge method javaarray merge sort javamergsort code javajava in place merge sortmerge sort code java explaiedmerge sort functiopn javamerge sort function in javamergesort java code example merge sorting in java merge sorted array solution javajava calling merge sortimplment merge sort jvamerge sort in java arraymerge arrays for merge sort javamerge sort java logicjava merge sort string arraymerge sort java merge java merge sort simplejava class merge sortinbuilt merge sort javamergesort i javamerge 2 sorted arrays function javaprogram to merge tqwo sorted array in javamerge sort on arrays javamerge sort in descending order in javaimplement merge sort in javamerge sort meth in javawhy java uses merge sorthow to merge a sorted array in javamerge sort exampe code javajava recursive merge sortmerge sort problem code in javageeks merge sort javamerge sort example javamerge sort not working in javamerge sort method javamerge sort in easy manner in javamerge sort algo javamerge sort algorithm javahow tom make a merge sort javanerge sort javamerge sort java code explanationhow to do a merge sort in java merge sort implementation in javamerge sort program with merge and mergesort functions in javamerge sort program in java java2novicemerge sort how to do in javamerge sort java optimizationmerge sort in java gfgjava mergesort codemergesort javamergesort java explainedmarge sort javamerge sort implementation javamerge sort code using recursion in javamerge function merge sort in javasorting array merge example in javamerge sort algorithm recursive javamerge sort for strings javamerge sort using array in javamerge sort in kavamerge two unsorted arrays in sorted order javamerge function in java in mergesortjava simple merge sortmerge sort java source codemerge sort examplemerge sort javasorting algorithms java merge sortsimple merge sort program in javamerge sort simple javamergesort function in javamergesort implementation javasimple merge sort java codemerge sort jqvahow to do a merge sort in java comparatormerge sort program in javahow to merge sorted arrays in javahow to implement merge sort javamerge sort java runtimemerge sorting java