merge sort java

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

showing results for - "merge sort java"
Celia
22 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}
Maelly
04 Jun 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
Joaquín
15 Jan 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
Romane
04 Jul 2018
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
Helena
25 Mar 2020
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]);
Mila
14 Apr 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}
queries leading to this page
merge two arrays in sorted order javamerge sort cppjava in place merge sortmerge sort pseudocodejava mergesort codemerge sort source codemerge sort a array in javamerge sort amerge sort pythonmergesort algorithm code javahow to merge sorted arrays in javahow tom make a merge sort javahow t o merge three sorted aray in java java class merge sorthow does mergesort work javaimplement merge sort javamerge sort cmerge function merge sort in javamerge sort javavmerge sort c 2b 2bmerge sort not working in javamerge two unsorted arrays in sorted order javamergesortmerge and sort two arrays in javamerge method of merge sort javamerge sort program in jaamerge sort expressed mathematicallymergesort function in javamerge sort two arrays javac merge sort void 2aspace complexity of merge sortimplment merge sort jvamerge sort array in javac 2b 2b code for merge sorthow to merge a sorted array in javamerge sort code using recursion in javawhat is merge sort used forjava recursive merge sortbuiltin merge sort javajava sort array mergesortrecursiv mergesort javahow to sort in merge sortmerge sort using auxiliarymerge sorting algorithm javamerge sort baeldungmergesort java programmergsort code javamerge sort descending order javahow to merge many sorted arrays in javamerge sort definitionmerge sort workingmerge sort java librarymergesort in cmerger sort javamerge sort more efficent wayarray merge sort javamerge sort java source codemerged sortmerge sort algo javamerge sort algorithm examplemergesort java code examplemarge sort in cmerge sort java algorithmsimplement following merge sort algorithm using recursion print passes of merging algorithm merge sort in java source codewhy does java use merge sortmerge sort algorithm geekforgeeks 3fmerge sort example with stepsmerge soring c 2b 2bmerge sort algorithm explainedimplementing merge sort in javamerge sort in javamerge sort tutorialcode of merge sort in javafunction mergesort 28nums 29 7b 2f 2f write merge sort code here java program using merge sort techniquesmerge sort program in javamerge sort example javajava merger sortjava unite algorithmrecursive merge sort cppfor the merge sort algorithm discussed in class 2c if the following change is made 2c the worst case runtime would beimplement merge sort in javamerge sort codemerge sort function in javahow does merge sort workmerge sort psuedocodemerge sort explanation in javamerge sort array algomerge sort big omerge sort java array implementationmerge sort 5cjava array mergemerge sort and sort in c 2b 2bmerge sort python complexitybig o notation merge sortuse of merge sortmerge sort algrorithm javamergosort javamerge sort using javamergesort sample inputmerge sort method javasort an array using mergesort javamerge sort in c 2b 2bmerge sorrmerge sort java recursive codejava merge sort recursivemerge sort in chow merge sort works 3fmerge sort complexitymerge sort using recursion in javamerge sort java codeoptimized merge sort javain merge sort 2c you createhow to merge arrays in javamerge sort code in javamerge sort time and space complexitymerge method javac 2b 2b merge sort recursive merge sort sort complexitymerge sort explanationmerge sort in cpphow to perform a merge sortmerge sort in kavacan you call merge sort in javamerge sort c 2b 2b programmergesort codemerge sort algorithmjava merge sort inbuilt functionmerger sort using javamerge sort using array in javamerge sort functiopn javanerge sort javamerge sort java runtimemerge sort algorithm jaamergesort function in c 2b 2bmerge sprt javawhat is merge sort algorithm2 way merge sort in javamerge javahow does merge sort work java merge sorting in java java array merge sortmerge sort in easy manner in javahow does a merge sort workmerge sort java recursionhow to write a merge sort array method ni javamerge sort in descending order javamerge sort example java step by stephow to merge two arrays and sort in javamerge sort meth in javamerge sortinghow to do a merge sort in java merge sort in java arrayhow does the mergesort workmerge sort algorithm 3fmerge sort algorithm javamerge sort in c 2b 2b programmerge sort javacode for merge sort in cmergesort cpp codewhere we use merge sortmerge sort i javamerge sort merge sort an array in javahow merge sort worksmerge sort forsimple merge sort javamerge sort recursive in javawhat is merge sort in javacode for merge sortsorting array merge example in javamerge sort 2 arrays javamerge sort wikimerging sortingmerge sorting in javamarge sort javamerge sort demomerge sort algorithmmerge sorting javajava merge sortjava merge sort algorithmmergesort java codemerge sort java programmerge sort solveexplain merge sortgetting a value from merge sort javamerge sort cominbuilt merge sort javamerge two sorted array and sort array javasmerge sorted array in javadef merge sortmerge sort in java examplemerge sort in cpp codegeeksforgeeks merge sorthow to implement merge sort in java from scratchhow to do a merge sort in java comparatormergesort i javamerge sort in placeaverage tc of merge sortmerge sort simple javamerge sort algorithm implementation in javadoes java have merge sort built inc 2b 2b merge sort merge sort exampe code javahow to implement merge sort javawhy does java use merge sort in arraysc 2b 2b merge sort codejava merge sort string arraymerge sortmerge sort jqvamerge arrays arrays sort javamerge sort javamergesort javahungrymerge arrays javamerge sort implementation in cmerge sort program with merge and mergesort functions in javamerge sort program in c 2b 2bmerge sort explained javamerge sort ascending c 2b 2bmerge sort examplemerge sort code in c 2b 2bmerge sort in java gfgmerge sort in java programrecursive merge sort javalet p be a mergesort program to sort numbers in ascendinng order on a unknown data structure which take o 28n 5e2 29 time to find the mid element 2c rest property is unknown then recurrence relation for the same is 3fmergesort in javasimple merge sort implementation javahow to do merge sort alogorithim javajava merge sort examplejava code for merge sortmerge sort code java explaiedhow to merge sortimplementation of merge sort algorithm in c 2b 2bmerge sort implementation in javamergesort implementation javamerge sort java merge merge sort java example codehow to implement merge sort in an arrayhow to merge sort in javamerge sort code javamerge sortehow to sort 2 arrays together in javamerge sort function cerge sort codemerge sort implementmerge sort java code explanationmplement merge sort using array javajava merge sort with only input arraymerge sort built in functions javamerge sorted array solution javamerge sort code java for sort and reversemerge sort java optimizationjava merge sort utilmerge sort java implementationmerge sort algorithm in placejava mergesortmerge sort serial implementation in javaalgorithm merge sortmerge and sort algorithmmerge method for merge sortmerge sort with one array javamerge sort java code recursivejava calling merge sortwhat is merge sortmerger sort cmerge sort in matrixmerge sort function codemerge sort nedirmerge 2 sorted arrays function javasorting algorithms java merge sortmerge sort theorymerge sortnalgorithm javamerge sort java functionsimple merge sort program in javamergesort java implementationmerge and merge sort javamerge sort program in java java2noviceprogram to merge tqwo sorted array in javamergesort complexity in already ascending orderwhat is merge sort 3fmergesort function javamerge sort in descending order in javamerge function in java in mergesortmerge sort teo array in javamerge sort algorithm recursive javamergesort 5cmergesort java recursivemerge sort algorithm a c code for merge sort use tabbing with binary search in the merging process java function merge sortmerge method for merge sort javamergesort complexityapplication of mergesort where gfgmerge sort ascending orderhava merge sortwhat is a merge sortmergesort java code recursivemerge sorted arrayjava merge sort programhow to combined to implementation in javamerge sprt doing a merge sort with arrays javamergesort cmerge sort java logicmerge sort jaaselection insertion merge sort c 2b 2bhow to indicate order in merge sortunderstanding merge sortmerse sort javamrege sort javamerge sort is in placemerge sort in java in builtmerge sort algorithm in javamerge sort java one methodmerge sort array javadetermine the appropriate sorting algorithm corresponding to the below function 3a function 28m 2cn 29 7bif 28m 3cn 29 7bmid 3d 28m 2bn 29 2f2 3b sort 28m 2cmiddle 29 3b sort 28middle 2b1 2cn 29 3b sort 28m 2cmiddle 2cn 29 3b 7d 7d 2amerge sort algorithm java implementationmerge sort implementation javamerge sortymerge sort library in javajava simple merge sort 22mergesort java 22merge sort in java codemerge sortzmergesort javamergesort java explainedmerge sorting program in java9 running merge sort on an array of size n which is already sorted is 2amerge sort on arrays javamerge sort to sort arraymerge sort explainedmerge sort recursive c 2b 2b codemerge sort java algorithmaverage complexity of merge sortwhy java uses merge sortmerge sort how to do in javamerge sort algorithm defmerge arrays for merge sort javahow to implement merge sortmergesort wikimerge sort algohow to make own sort in merge sort javac 2b 2b merge sortmerge osort in jvaa 27java 3a mergesortmerge sort java arraysmerge sort function in c 2b 2bmerge sort recursive javasimple merge sort java codemergesort class in javamerge sort java simplejava mergesort algorithmmergesort implementation c 2b 2bmerge sort java recursivemerge sort code in cmergesort pythongeeks for geeks merge sort recursivefull implementation merge sort c 2b 2bsorting string merge sortmerge sort two array in javamerge sort imerge sort algorithmmerge sort program in java using recursionjava merge sort simplemerge sort algorithm java in ascending ordermerge sort java