-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBucketSort.cpp
96 lines (93 loc) · 1.66 KB
/
BucketSort.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "linked_list.h"
#include "basic_array_function.h"
#include <iostream>
using namespace::std;
pNode bucket_sort(pNode head);
int main_bucket_sort()
{
cout << "Bucket sort:\n" << "n:\n";
int n;
cin >> n;
double nums[100];
for (; n != 0;)
{
for (int i(0); i < n; i++)
cin >> nums[i];
pNode head = create_from_array(nums, n);
head = bucket_sort(head);
cout << "ans:\n";
linked_list_print(head);
cout << "n:\n";
cin >> n;
}
return 0;
}
pNode bucket_sort(pNode head)
{
if (head == NULL)
return NULL;
// find the max number
double max = head->val;
pNode temp = head;
pNode now = NULL;
int i = 0;
for (; temp->next != NULL; temp = temp->next)
{
if (temp->val > max)
max = temp->val;
}
int n = (int)(max + 1);
// init a array to store pNode
pNode* bucket_head = new pNode[n];
if (bucket_head != NULL)
{
for (i = 0; i < n; i++)
{
bucket_head[i] = NULL;
}
}
// put nums into bucket
for (temp = head; temp != NULL; )
{
now = temp;
temp = temp->next;
i = (int)(now->val);
now->next = bucket_head[i];
bucket_head[i] = now;
}
// sort every bucket
for (i = 0; i < n; i++)
{
bucket_head[i] = linked_list_sort(bucket_head[i]);
}
// put out the bucket
bool is_head = false;
for (i = 0; i < n - 1; i++)
{
if (bucket_head[i] == NULL)
{
continue;
}
else
{
if (is_head == false)
{
is_head = true;
head = bucket_head[i];
}
}
for (temp = bucket_head[i]; temp->next != NULL; temp = temp->next)
;
for (int j(i + 1); j < n; j++)
{
if (bucket_head[j] != NULL)
{
temp->next = bucket_head[j];
break;
}
}
}
if (bucket_head != NULL)
delete[] pNode(bucket_head);
return head;
}