Ejemplo 1: java de clasificación de burbujas
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
Ejemplo 2: clasificación de burbujas de programa Java en orden ascendente y descendente
// Bubble sort descending order
import java.util.Scanner;
public class BubbleSortDescending
{
public static void main(String[] args)
{
int number, a, b, temp;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter number of integers: ");
number = sc.nextInt();
int[] arrInput = new int[number];
System.out.println("Enter " + number + " integers: ");
for(a = 0; a < number; a++)
arrInput[a] = sc.nextInt();
for(a = 0; a < (number - 1); a++)
{
for(b = 0; b < number - a - 1; b++)
{
// logic to sort in descending order
if(arrInput[b] < arrInput[b + 1])
{
temp = arrInput[b];
arrInput[b] = arrInput[b + 1];
arrInput[b + 1] = temp;
}
}
}
sc.close();
System.out.println("Sorted integers: ");
for(a = 0; a < number; a++)
System.out.println(arrInput[a]);
}
}
Ejemplo 3: java de clasificación de burbujas
public class DemoBubbleSort
{
void bubbleSort(int[] arrNum)
{
int num = arrNum.length;
for(int a = 0; a < num - 1; a++)
{
for(int b = 0; b < num - a - 1; b++)
{
if(arrNum[b] > arrNum[b + 1])
{
int temp = arrNum[b];
arrNum[b] = arrNum[b + 1];
arrNum[b + 1] = temp;
}
}
}
}
void printingArray(int[] arrNum)
{
int number = arrNum.length;
for(int a = 0; a < number; ++a)
{
System.out.print(arrNum[a] + " ");
System.out.println();
}
}
public static void main(String[] args)
{
DemoBubbleSort bs = new DemoBubbleSort();
int[] arrSort = {65, 35, 25, 15, 23, 14, 95};
bs.bubbleSort(arrSort);
System.out.println("After sorting array: ");
bs.printingArray(arrSort);
}
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)