-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuket_sort.cpp
More file actions
84 lines (66 loc) · 1.36 KB
/
buket_sort.cpp
File metadata and controls
84 lines (66 loc) · 1.36 KB
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
/*
* Name :sharad Dixit
* Institute :IIITA
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct node {
float data;
struct node *next;
};
void find_bucket( float arr[], int n, node *buck[])
{
for ( int i = 0; i < n; i++) {
int tmp = arr[i];
node *temp = new node;
temp->data = arr[i];
temp->next = buck[tmp];
buck [tmp] = temp;
}
}
void sort_link( int n, node *buck[]) {
for ( int i = 0; i < n; i++ ) {
node *temp = buck[i];
while ( temp != NULL) {
node *check = temp ->next;
while ( check != NULL) {
if ( (temp -> data) > (check -> data) ){
float count = temp ->data;
temp -> data = check -> data;
check ->data = count;
}
check = check->next;
}
temp = temp ->next;
}
}
}
void read_sorted(float arr[],int n , node *buckets[]){
for (int i = 0; i<n ;i++){
struct node * temp = new node;
temp = buckets[i];
while ( temp != NULL ) {
cout << temp->data << endl;
temp = temp->next;
}
}
}
int main( int argc, char *argv[] )
{
int n = atoi ( argv[1] );
float arr[ n ];
node *buckets[n];
cout << "BEFORE\n";
for ( int i = 0; i < n; i++){
buckets[i] = NULL;
float tmp = rand()%100;
arr[i] = tmp /100;
//cout << arr[i] << "\n";
}
find_bucket(arr,n,buckets);
sort_link( n,buckets);
cout << "AFTER " << endl;
read_sorted( arr,n , buckets);
}