how to write a merge sort array method ni java
Java
public static void merge(
int[] a, int[] l, int[] r, int left, int right) {
int i = 0, j = 0, k = 0;
while (i < left && j < right) {
if (l[i] <= r[j]) {
a[k++] = l[i++];
}
else {
a[k++] = r[j++];
}
}
while (i < left) {
a[k++] = l[i++];
}
while (j < right) {
a[k++] = r[j++];
}
}
public static void mergeSort(int[] a, int n) {
if (n < 2) {
return;
}
int mid = n / 2;
int[] l = new int[mid];
int[] r = new int[n - mid];
for (int i = 0; i < mid; i++) {
l[i] = a[i];
}
for (int i = mid; i < n; i++) {
r[i - mid] = a[i];
}
mergeSort(l, mid);
mergeSort(r, n - mid);
merge(a, l, r, mid, n - mid);
}
Also in Java:
- Title
- spigot kill entity
- Category
- Java
- Title
- Scanner library showing element not found exception
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- java setter
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- count number of matches in two strings java
- Category
- Java
- Title
- set password visible in android
- Category
- Java
- Title
- compile java to jar in mac
- Category
- Java
- Title
- iterate hashmap java
- Category
- Java
- Title
- scan a string using scanner class in java
- Category
- Java
- Title
- java read public key from keystore
- Category
- Java
- Title
- convert every character in string to arraylist jva
- Category
- Java
- Title
- input java
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- sort array java
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java
- Title
- java recursion
- Category
- Java
- Title
- java map string to list
- Category
- Java
- Title
- prime number program in java
- Category
- Java
- Title
- how to add multiple filter condition in Java stream filter chain
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- how to add strings together
- Category
- Java
- Title
- get sha key android
- Category
- Java
- Title
- iterate trough linked list java
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- print a letter in java
- Category
- Java
- Title
- java hash password
- Category
- Java
- Title
- replace regex group() java
- Category
- Java
- Title
- android dynamically create layer-list with item and shape site:stackoverflow.com
- Category
- Java
- Title
- kotlin android intent pass data
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- java print treemap
- Category
- Java
- Title
- java linked list functions
- Category
- Java
- Title
- change number into array in java
- Category
- Java
- Title
- how to take space separated input in java
- Category
- Java
- Title
- determine if a given binary tree is a valid bst
- Category
- Java
- Title
- java android edit text set value
- Category
- Java
- Title
- java check if a line is enclosed in quotation marks
- Category
- Java
- Title
- bukkit java get player count
- Category
- Java
- Title
- java observable
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- file to image javafx
- Category
- Java
- Title
- how to read file from assets folder in android
- Category
- Java
- Title
- decomposer chaine de caractère java
- Category
- Java
- Title
- how to get the max value of an array java
- Category
- Java
- Title
- fastapi list query parameter
- Category
- Java
- Title
- 2d array java
- Category
- Java
- Title
- expression régulière téléphone java
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- deleting elements of an array in java
- Category
- Java
- Title
- java how to iterate through a arraylist
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- java calculator code
- Category
- Java
- Title
- how do i print text in java
- Category
- Java
- Title
- java sql question mark
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- how to print the map in java
- Category
- Java
- Title
- java startActivity crash
- Category
- Java
- Title
- find a substring in a string java
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- print hashtable in java
- Category
- Java
- Title
- How to print in console java
- Category
- Java
- Title
- java string indexof
- Category
- Java
- Title
- spring framework iterate
- Category
- Java
- Title
- java generate list of random element
- Category
- Java
- Title
- read and write files pattern java
- Category
- Java
- Title
- java make arraylist
- Category
- Java
- Title
- java random integer
- Category
- Java
- Title
- java string format .2f
- Category
- Java
- Title
- get material of block bukkit
- Category
- Java
- Title
- two dimensional array in java example program
- Category
- Java
- Title
- récupérer les données d un liste java
- Category
- Java
- Title
- java go troght loop object
- Category
- Java
- Title
- linux pyspark select java version
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- how to get the width and height of a string in java
- Category
- Java
- Title
- how to draw a line in java
- Category
- Java
- Title
- how to use the this keyword in java
- Category
- Java
- Title
- java how to stop code
- Category
- Java
- Title
- casting java
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- android notification addaction example
- Category
- Java
- Title
- inheritance setter and getter in java
- Category
- Java
- Title
- jbutton open jframe java
- Category
- Java
- Title
- scanner in java
- Category
- Java
- Title
- java pass array as method parameter
- Category
- Java
- Title
- java random boolean
- Category
- Java
- Title
- java code to save excel data to mysql
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- java stack push
- Category
- Java
- Title
- how to remove spaces from an array in java
- Category
- Java
- Title
- how to make a copy of an array java
- Category
- Java
- Title
- how to create a subclass in java
- Category
- Java
- Title
- spigot run task later
- Category
- Java
- Title
- transition java fx
- Category
- Java
- Title
- how to remove a certain string in a arraylist java
- Category
- Java
- Title
- spigot custom mob
- Category
- Java
- Title
- spigot get player from UUID
- Category
- Java