9
We usually learn in college that, to sort integer vectors, we use a technique called Bubble Sort:
int[] vetorOrdenado = new int[8];
vetorOrdenado[0] = 2;
vetorOrdenado[1] = 41;
vetorOrdenado[2] = 12;
vetorOrdenado[3] = 6;
vetorOrdenado[4] = 5;
vetorOrdenado[5] = 29;
vetorOrdenado[6] = 17;
vetorOrdenado[7] = 3;
for(int i = 0; i < vetorOrdenado.length;i++){
for(int j = 0; j < vetorOrdenado.length - 1; j++){
if(vetorOrdenado[i] < vetorOrdenado[j]){
int aux = vetorOrdenado[i];
vetorOrdenado[i] = vetorOrdenado[j];
vetorOrdenado[j] = aux;
}
}
}
This technique sorts an integer vector, as can be seen in ideone.
There is another way to sort numerical vectors without using loop within loop, as in Bubble Sort? Collections
has no way to sort an array like this?
It has several https://en.wikipedia.org/wiki/Sorting_algorithm http://www.sorting-algorithms.com/ In most cases, well optimized Quick Sort works best. Bubble is very bad. Java has a very suitable algorithm ready: http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#Sort-byte:A-
– Maniero
@bigown these methods there are only version 8? I realized that has no return, so he orders taking advantage of the vector itself past?
– user28595
They are very old. They ordain in-place.
– Maniero
Book tip to learn more about sorting algorithms: Algorithms. Theory and Practice - Thomas Cormen
– Paulo Elias