java merge sort
Java
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);
}
/* Java program for Merge Sort */
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
System.out.println("Given Array");
printArray(arr);
MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length-1);
System.out.println("\nSorted array");
printArray(arr);
}
}
/* This code is contributed by Rajat Mishra */
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++];
}
}
Also in Java:
- Title
- java code to concatinate integer
- Category
- Java
- Title
- how to convert string to double in android studio
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- array methods in java
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- java calculator code
- Category
- Java
- Title
- for loop in java stack overflow
- Category
- Java
- Title
- java split array into two
- Category
- Java
- Title
- processing play sound
- Category
- Java
- Title
- java code to get all leaf nodes of a xml file
- Category
- Java
- Title
- read lines of file randomly java
- Category
- Java
- Title
- declare bufferedreader java
- Category
- Java
- Title
- org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
- Category
- Java
- Title
- java final meaning
- Category
- Java
- Title
- how to make a text field required in android studio
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- serialversionuid java
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- transformez integer en string
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- from file to array java
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- java next permutation
- Category
- Java
- Title
- find a substring in a string java
- Category
- Java
- Title
- how to print to the console in java
- Category
- Java
- Title
- java random char a-z
- Category
- Java
- Title
- inline foreach java
- Category
- Java
- Title
- Linked List implementation
- Category
- Java
- Title
- java android development find element by id
- Category
- Java
- Title
- android java shared preferences remove key
- Category
- Java
- Title
- que es un length en java
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- 2d array java
- Category
- Java
- Title
- Category
- Java
- Title
- java remove double spaces
- Category
- Java
- Title
- how to loop trough an object java script
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- eliminar el primer caracter de un string java
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- java long to int
- Category
- Java
- Title
- query java persistence
- Category
- Java
- Title
- stack overflow recyclerview
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- official java website
- Category
- Java
- Title
- get current day java
- Category
- Java
- Title
- convert string to int java
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- iterate hashmap java
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- multiplication program java
- Category
- Java
- Title
- java uuid
- Category
- Java
- Title
- raspberry stackexchange how to install the java jdk
- Category
- Java
- Title
- java generic calling clone method
- Category
- Java
- Title
- check if a string is a substring of another string java
- Category
- Java
- Title
- how to use pow function in java
- Category
- Java
- Title
- java merge sort
- Category
- Java
- Title
- arraylist to int array java
- Category
- Java
- Title
- int java
- Category
- Java
- Title
- setting up javafx in eclipse
- Category
- Java
- Title
- java loop
- Category
- Java
- Title
- how to call a static method in java
- Category
- Java
- Title
- java 8 iterating and manipulating list
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- convert array to list java
- Category
- Java
- Title
- split every character in string into array java
- Category
- Java
- Title
- how to get all of the chldren of a panel in java
- Category
- Java
- Title
- java function for power
- Category
- Java
- Title
- java new string array
- Category
- Java
- Title
- how to find length of array in java
- Category
- Java
- Title
- cannot fit requested classes in a single dex file
- Category
- Java
- Title
- how to add a keylistener to a jframe
- Category
- Java
- Title
- java stack pop
- Category
- Java
- Title
- binary string to int java
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- initialiser une arraylist
- Category
- Java
- Title
- java how to get all threads
- Category
- Java
- Title
- big integer java
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- system.arraycopy java
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- height constraint layout guideline
- Category
- Java
- Title
- close scanner java
- Category
- Java
- Title
- read from elasticsearch in spark
- Category
- Java
- Title
- int to string java
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- lightweight java game library
- Category
- Java
- Title
- java script to detect and launch all browsers
- Category
- Java
- Title
- find frequency of each word in a string in java
- Category
- Java
- Title
- else statement java
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- google translate
- Category
- Java
- Title
- java singleton
- Category
- Java
- Title
- iterate through hashmap in java
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet
- Category
- Java
- Title
- java animated gif example
- Category
- Java