-
Notifications
You must be signed in to change notification settings - Fork 1
/
listFrequence.c
61 lines (50 loc) · 1.54 KB
/
listFrequence.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
60
61
#include <stdlib.h>
#include "listFrequence.h"
struct node{
unsigned char byte;
long long int frequence;
};
struct list{
struct node array[256];
};
listFrequence* listFrequence_create(){
listFrequence *newList = malloc(sizeof(listFrequence));
int i;
for(i = 0; i < 256; i++){
newList->array[i].byte = (unsigned char) i;
newList->array[i].frequence = 0;
}
return newList;
}
void listFrequence_increaseFrequence(listFrequence *list, unsigned char byte){
if(list->array[byte].byte == byte)
list->array[byte].frequence = list->array[byte].frequence + 1;
}
long long int listFrequence_getFrequenceAtIdx(listFrequence *list, unsigned char idx){
return list->array[idx].frequence;
}
unsigned char listFrequence_getByteAtIndex(listFrequence *list, unsigned char idx){
return list->array[idx].byte;
}
void swap(struct node *a, struct node *b){
struct node aux = *a; *a = *b; *b = aux;
}
void quicksort(struct node array[], unsigned char idxi, unsigned char idxf){
if(idxf - idxi < 1)
return;
long long int pivot = array[(idxf + idxi)/2].frequence;
unsigned char left = idxi;
unsigned char right = idxf;
while(left <= right){
while(array[left].frequence < pivot && left < idxf)
++left;
while(array[right].frequence > pivot && right > idxi)
--right;
if(left <= right) {
swap(&array[left], &array[right]);
++left; --right;
}
}
quicksort(array, idxi, right);
quicksort(array, left, idxf);
}