-
Notifications
You must be signed in to change notification settings - Fork 4k
/
CountingSort.c
59 lines (44 loc) · 1.22 KB
/
CountingSort.c
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
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
void countingSort(int *vet,int *org, int tam){
int i, j, k;
int baldes[MAX];
for(i = 0 ; i < MAX ; i++)
baldes[i] = 0;
for(i = 0 ; i< tam ; i++)
baldes[vet[i]]++;
/*for(i = 0, j = 0; j < MAX ; j++)
for(k = baldes[j] ; k > 0 ; k--)
vet[i++] = j;*/
for(i = 1 ; i < MAX ; i++){
baldes[i] = baldes[i] + baldes[i-1];
}
for(i = (tam-1) ; i >=0 ; i--){
org[baldes[vet[i]]] = vet[i];
baldes[vet[i]]--;
}
}
int main(){
int *vet, *org;
int i, tam;
printf("Entre com o tamanho do vetor: ");
scanf("%d", &tam);
vet = (int*)malloc(tam*sizeof(int));
org = (int*)malloc(tam*sizeof(int));
printf("Entre com os valores dos elementos\n");
for(i = 0 ; i < tam ; i++){
printf("Elemento %d: ",i+1);
scanf("%d", &vet[i]);
}
printf("Seu vetor desordenado\n");
for(i = 0 ; i < tam ; i++){
printf("%d\t", vet[i]);
}
countingSort(vet, org,tam);
printf("\nSeu vetor Ordenado\n");
for(i = 1 ; i < tam+1 ; i++){
printf("%d\t", org[i]);
}
return 0;
}