-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountingSortNegativos.java
51 lines (39 loc) · 1.4 KB
/
CountingSortNegativos.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
package questoes;
import java.util.Arrays;
import java.util.Scanner;
class CountingSortNegativos {
public static int[] converteArrayStringEmInt (String[] array) {
int[] arrayInt = new int[array.length];
for (int i = 0; i < array.length; i++) {
arrayInt[i] = Integer.parseInt(array[i]);
}
return arrayInt;
}
public static void countingSort (int[] A, int k, int menorValor) {
int[] C = new int[k - menorValor + 1];
int[] B = new int[A.length];
// Frequência
for (int j : A) {
C[j - menorValor]++;
System.out.println(Arrays.toString(C));
}
// Cumulativa
for (int i = 1; i < C.length; i++) {
C[i] += C[i - 1];
}
System.out.println("Cumulativa do vetor de contagem - " + Arrays.toString(C));
// Preenchendo B
for (int i = A.length - 1; i >= 0; i--) {
B[C[A[i] - menorValor] - 1] = A[i];
C[A[i] - menorValor]--;
}
System.out.println(Arrays.toString(C) + "\n" + Arrays.toString(B));
}
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int[] array = converteArrayStringEmInt(scanner.nextLine().split(" "));
int maiorValor = scanner.nextInt();
int menorValor = scanner.nextInt();
countingSort(array, maiorValor, menorValor);
}
}