forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGnomeSort.java
More file actions
28 lines (23 loc) · 706 Bytes
/
GnomeSort.java
File metadata and controls
28 lines (23 loc) · 706 Bytes
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
import java.util.Arrays;
public class GnomeSort {
public static void main(String[] args) {
int vetor[] = {10, -10, -9, 9, 8, -8, 7, -7, 6, -6, 5, -5, 4, -4, 3, -3, 2, -2, 1, -1, 0};
System.out.println("Vetor antes da ordenação:" + Arrays.toString(vetor));
gnomeSort(vetor);
System.out.println(
"Vetor após ordenação com o algoritmo Gnome Sort: " + Arrays.toString(vetor));
}
static void gnomeSort(int[] vetor) {
int i = 0;
while (i < vetor.length) {
if (i == 0 || vetor[i] >= vetor[i - 1]) {
i++;
} else {
int auxiliar = vetor[i];
vetor[i] = vetor[i - 1];
vetor[i - 1] = auxiliar;
i--;
}
}
}
}