forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSort.java
37 lines (30 loc) · 850 Bytes
/
InsertionSort.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
public class InsertionSort {
public static void main(String[] args) {
int vetor[] = {9, 0, 4, 2, 3, 8, 7, 1, 6, 5};
System.out.println("Insertion Sort:");
System.out.println("Vetor não ordenado:");
mostraVetor(vetor);
vetor = insertionSort(vetor);
System.out.println("Vetor ordenado:");
mostraVetor(vetor);
}
public static int[] insertionSort(int vetor[]) {
int chave, aux;
for (int i = 0; i < vetor.length; i++) {
chave = vetor[i];
aux = i - 1;
while (aux >= 0 && vetor[aux] > chave) {
vetor[aux + 1] = vetor[aux];
aux -= 1;
}
vetor[aux + 1] = chave;
}
return vetor;
}
public static void mostraVetor(int vetor[]) {
for (int i = 0; i < vetor.length; i++) {
System.out.print(vetor[i] + ", ");
}
System.out.println("");
}
}