-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBucket.cpp
50 lines (46 loc) · 1.21 KB
/
Bucket.cpp
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
#include <iostream>
struct node {
node* next;
int val;
};
void bucket_T(int*T,int size){
int * Buckets = (int*) malloc (sizeof(int)*10*size);
for(int i=0; i<10*size; i++){Buckets[i]=0;}
for(int i=0; i<size; i++) {
Buckets[T[i] * size]++;
}
int j=0;
for(int i=0; i<10*size; i++){
while(Buckets[i] !=0) {
T[j++] =i/size;
Buckets[i]--;
}
}
}
void bucket_L(node**L){
int size=0; node*k=*L;
while(k!=NULL){size++; k=k->next;}
node ** Buckets = (node**) malloc (sizeof(node*)*10*size);
for(int i=0; i<10*size; i++){Buckets[i]=new node;Buckets[i]->val=-1;Buckets[i]->next=NULL;}
k=*L;
while(k!=NULL){
node* i=Buckets[k->val * size];
node* tmp =Buckets[k->val * size]->next;
i->next = k;
k=k->next;
i=i->next;
i->next = tmp;
}
for(int i=0; i<10*size; i++){
insertion_L(&(Buckets[i]->next)); //use any sorting algorithm
}
node* Out = new node;
node*n =Out;
for(int i=0; i<10*size; i++){
if(Buckets[i]->next!=NULL) {
n->next = Buckets[i]->next;
while (n->next != NULL) { n = n->next; }
}
}
*L=Out->next;
}