Created
December 2, 2019 01:23
-
-
Save davidauza-engineer/ea2cc4832e9218572ea298b24ef50206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implementación algoritmo Bucket Sort. | |
import java.util.Arrays; | |
class Main { | |
// Este método implementa el algoritmo Bucket Sort para ordenar un arreglo de números enteros. | |
private static int[] bucketSort(int[] arreglo) { | |
int max = obtenerMaximo(arreglo); | |
int[] bucket = new int[max + 1]; | |
for (int i = 0; i < arreglo.length; i++) { | |
bucket[arreglo[i]]++; | |
} | |
for (int i = 0, j = 0; i <= max; i++) { | |
while (bucket[i] > 0) { | |
arreglo[j++] = i; | |
bucket[i]--; | |
} | |
} | |
return arreglo; | |
} | |
// Este método retorna el número más grande en un arreglo de números enteros. | |
private static int obtenerMaximo(int[] arreglo) { | |
int max = arreglo[0]; | |
for(int i = 1; i < arreglo.length; i++) { | |
if (arreglo[i] > max) { | |
max = arreglo[i]; | |
} | |
} | |
return max; | |
} | |
// Método main que ejecuta el programa. | |
public static void main(String[] args) { | |
int[] arreglo = {94, 25, 65, 12, 63, 32}; | |
System.out.println("Se ordenará el arreglo " + Arrays.toString(arreglo)); | |
System.out.println("El arreglo ordenado es " + Arrays.toString(bucketSort(arreglo))); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment