-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcountingsort.c
40 lines (35 loc) · 1.05 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
/*
* Copyright(C) 2020, Thalisson Alves <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#include <stdlib.h>
#include "countingsort.h"
#include "ordenacaomacros.h"
void countingsort(Item *v, int l, int r)
{
Item min = v[l], max = v[l];
for (int i = l + 1; i <= r; i++)
{
if (less(v[i], min))
min = v[i];
if (less(max, v[i]))
max = v[i];
}
int range = max - min + 1;
int *count = (int *)calloc(range, sizeof(int));
for (int i = l; i <= r; i++)
count[v[i] - min]++;
int idx = 0;
for (int i = 0; i < range; i++)
for (int j = 0; j < count[i]; j++)
v[idx++] = i + min;
free(count);
}