-
Notifications
You must be signed in to change notification settings - Fork 1
/
Selection.java
62 lines (52 loc) · 2.11 KB
/
Selection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//==============//
//SELECTION SORT//
//==============//
package Sorting;
import java.util.Random;
public class Selection {
public static void main(String[] args) {
System.out.println("\n#================#");
System.out.println("| SELECTION SORT |");
System.out.println("#================#\n");
// criacao do array
int[] array = new int[10];
// preenchimento do array com numeros aleatorios de 10 a 99
for (int i = 0; i < array.length; i++){
array[i] = (int) (new Random().nextInt(90) + 10);
}
// imprime o vetor desordenado
System.out.println("Vetor desordenado:");
System.out.println("*-------------------------------------------------*");
for (int i = 0; i < array.length; i++){
System.out.print("| " + array[i] + " ");
}
System.out.println("|");
System.out.println("*-------------------------------------------------*\n");
//=====SELECTION SORT=====//
// variaveis auxiliares
int min_idx;
// controlador
for (int i = 0; i < array.length-1; i++){
// define a esquerda do vetor como o menor elemento
min_idx = i;
// comparador
for (int j = i+1; j < array.length; j++){
// se o elemento j for menor do que o menor elemento, realiza a troca
if (array[j] < array[min_idx]) min_idx = j;
}
// funcao swap
int aux = array[i];
array[i] = array[min_idx];
array[min_idx] = aux;
}
//=====SELECTION SORT=====//
// imprime o vetor ordenado
System.out.println("Vetor ordenado apos o sort:");
System.out.println("*-------------------------------------------------*");
for (int i = 0; i < array.length; i++){
System.out.print("| " + array[i] + " ");
}
System.out.println("|");
System.out.println("*-------------------------------------------------*\n");
}
}