Sort arrangement from lowest to highest

1
public static void main(String[] args) {     
    int [] a  = {7,2,4,8,3,9,1,5,10,6};
    int menor;

    for(int i = 0; i < 10; i++){
        menor = a[0];

        if (a[i] < menor){
            menor = a[i];
        }
        else{
            if (a[i] > menor){
              menor = menor;
            }      
        }
    }
    System.out.println(Arrays.toString(a));
}
    
asked by ACAU 01.10.2017 в 05:22
source

4 answers

1

This is called bubble order.

int a[] = {5,3,2,7,10,1};
    for (int x = 0; x < a.length; x++) {
        for (int i = 0; i < a.length-x-1; i++) {
            if(a[i] < a[i+1]){
                int tmp = a[i+1];
                a[i+1] = a[i];
                a[i] = tmp;
            }
        }
    }
    
answered by 01.10.2017 / 06:20
source
2

The simplest and most appropriate way is using the method sort () :

  int [] a  = {7,2,4,8,3,9,1,5,10,6};
  Arrays.sort(a);

To check, print the contents of the array:

 System.out.println(Arrays.toString(a));

and you will get the ordered array from lowest to highest (ascending) as output:

 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I recommend this excellent article where it shows other ways to order an array in Java.

Ways to sort an array in java.

    
answered by 03.10.2017 в 03:50
1

You can use the method of order of bubbles (Bubble sort). This method reviews and arranges the order of each element of the array by comparing it to the next. The process is repeated several times until all the elements have been verified, no more changes are necessary and the arrangement is ordered.

public static void main(String[] args) {
    int [] a  = {7,2,4,8,3,9,1,5,10,6};
    int temporal = 0;

    for (int i = 0; i < a.length; i++) {
        for (int j = 1; j < (a.length - i); j++) {
            if (a[j - 1] > a[j]) {
                temporal = a[j - 1];
                a[j - 1] = a[j];
                a[j] = temporal;
            }
        }
    }
    System.out.println(Arrays.toString(a));
}

I have changed the number 10 by a.length . In this way the algorithm will be more flexible, working for any arrangement regardless of the amount of elements it contains.

    
answered by 01.10.2017 в 06:33
1

Complementing the response of @spuente , bubble can be easily optimized, reducing its execution time by 50% in the worst case detecting when the array is ordered in one of the iterations, avoiding a good amount of comparisons.

In each iteration it is assumed that the array is ordered, setting a flag sorted = true , then, in the loop where the disordered elements are exchanged, if two disordered elements are found,% is set sorted = false

In each iteration it is checked if sorted keeps true , if so it implies that it iterated over list.length -i -1 remaining elements without finding disordered elements and therefore the array is ordered and it does not make sense to continue iterating about it.

public static void main(String[] args) {
        optimizedBubbleSort(new int[]{1,2,4,5,6}); // Iteración 0, array ordenado
        optimizedBubbleSort(new int[]{11,2,44,5,16}); // Iteración 2, array ordenado
        optimizedBubbleSort(new int[]{0,8,74,5,1}); // Iteración 3, array ordenado
}

    private static void optimizedBubbleSort(int [] list){
        for(int i =0; i< list.length; i++){
            boolean sorted = true; // asumo que para la iteración i el listado es ordenado,
            for(int j =0; j< list.length - i - 1; j++){ // en cada iteración los elementos desde la posición (length-i) estan ordenados, por lo tanto solo recorro hasta esa posición
                if(list[j] > list[j+1]){
                    int temp = list[j];
                    list[j] = list[j+1];
                    list[j+1] = temp;
                    sorted = false; 
                }
            }
            if(sorted){ 
                System.out.println(String.format("Iteración %s, array ordenado", i));
                return;
            }
        }
    }
    
answered by 01.10.2017 в 06:58