Java
xxxxxxxxxx
import java.util.Arrays;
class Main {
void merge(int array[], int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int L[] = new int[n1];
int M[] = new int[n2];
for (int i = 0; i < n1; i++)
L[i] = array[p + i];
for (int j = 0; j < n2; j++)
M[j] = array[q + 1 + j];
int i, j, k;
i = 0;
j = 0;
k = p;
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
array[k] = L[i];
i++;
} else {
array[k] = M[j];
j++;
}
k++;
}
while (i < n1) {
array[k] = L[i];
i++;
k++;
}
while (j < n2) {
array[k] = M[j];
j++;
k++;
}
}
void mergeSort(int array[], int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
merge(array, left, mid, right);
}
}
public static void main(String args[]) {
int[] array = { 6, 5, 12, 10, 9, 1 };
Main ob = new Main();
ob.mergeSort(array, 0, array.length - 1);
System.out.println("Sorted Array:");
System.out.println(Arrays.toString(array));
}
}
Unsorted Array: [6, 5, 12, 10, 9, 1] Sorted Array: [1, 5, 6, 9, 10, 12]
Linux - Comment installer anc-api-tools
Linux - Comment installer curvedns
Python - Vérifier si un nombre est positif, négatif ou 0
Linux - Comment installer zenmap
C - Compter le nombre de chiffres dans un entier
C++ - Vérifier l'année bissextile
C++ - Vérifier si un nombre est premier ou non
JavaScript - Vérifier si une chaîne commence et se termine par certains caractères
C - Trouver le plus grand nombre à l'aide de l'allocation de mémoire dynamique
Linux - Comment installer xbitmaps
Linux - Comment installer zstd
Kotlin - Inverser un nombre
Kotlin - Arrondir un nombre à n décimales
Java - Inverser un nombre
Python - Écriture de fichiers Excel (XLSX)
C++ - Trouver le quotient et le reste
Java - Créer une pyramide et un motif
Linux - Comment installer zfs-test
We have been online since 2021 and 1 millions of people around the globe have visited our website since then
More visitors every month