forked from theutpal01/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucketsort.cpp
116 lines (103 loc) · 2.03 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
using namespace std;
struct node
{
float info;
struct node *link;
};
void Printarray(float *A, int n)
{
for (int i = 0; i < n; i++)
{
cout << A[i] << " ";
}
}
struct node *insort(struct node *Nnode, struct node *sor)
{
if (sor == NULL || sor->info >= Nnode->info)
{
Nnode->link = sor;
sor = Nnode;
}
else
{
struct node *curr = sor;
while (curr->link != NULL && curr->link->info < Nnode->info)
{
curr = curr->link;
}
Nnode->link = curr->link;
curr->link = Nnode;
}
return sor;
}
struct node *InsertionSort(struct node *head)
{
struct node *ptr = head, *sor = NULL;
while (ptr != NULL)
{
struct node *next = ptr->link;
sor = insort(ptr, sor);
ptr = next;
}
return sor;
}
void Bucketsort(float *A, int n)
{
struct node *B[20];
int j;
for (int i = 0; i < n; i++)
{
B[i] = NULL;
}
for (int i = 0; i < n; i++)
{
j = n * A[i];
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->info = A[i];
ptr->link = B[j];
B[j] = ptr;
}
for (int i = 0; i < n; i++)
{
B[i] = InsertionSort(B[i]);
}
int i = 0;
int t = 0;
while (i < n)
{
for (int j = 0; j < n; j++)
{
if (B[j] == NULL)
{
continue;
}
else
{
struct node *ptr = B[j];
while (ptr != NULL)
{
A[i] = ptr->info;
i++;
ptr = ptr->link;
}
}
}
}
}
int main()
{
int i, n;
float A[20];
printf("Enter the value of n :");
scanf("%d", &n);
printf("Enter the %d array elements :", n);
for (i = 0; i < n; i++)
{
printf("A[%d]= ", i);
scanf("%f", A + i);
}
Bucketsort(A, n);
Printarray(A, n);
return 0;
}