viernes, 28 de octubre de 2016

Método de ordenamiento de burbuja (bubble sort)






BUBBLE SHORT 

THE BEST WAY TO LEARN IS THE PRACTICE (LA MEJOR FORMA DE APRENDER ES LA PRACTICA)

import java.util.Scanner;

class BubbleSort {

  public static void main(String []args) {

    int n, c, d, swap; //swap para intercambiar
    Scanner in = new Scanner(System.in);

    System.out.println("Input number of integers to sort");//introducir numeros enteros a  ordenar
    n = in.nextInt();

    int array[] = new int[n];

    System.out.println("Enter " + n + " integers");

    for (c = 0; c < n; c++)
      array[c] = in.nextInt();

    for (c = 0; c < ( n - 1 ); c++) {
      for (d = 0; d < n - c - 1; d++) {
        if (array[d] > array[d+1]) /* For descending order use < */
        {
          swap       = array[d];
          array[d]   = array[d+1];
          array[d+1] = swap;
        }
      }
    }

    System.out.println("Sorted list of numbers");// lista ordenada de numeros

    for (c = 0; c < n; c++)
      System.out.println(array[c]);
  }
}

--------------------Configuration: <Default>--------------------
Input number of integers to sort
10
Enter 10 integers
13
24
34
56
77
55
654
67
45
87
Sorted list of numbers
13
24
34
45
55
56
67
77
87
654

Process completed.

No hay comentarios:

Publicar un comentario