-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (85 loc) · 2.45 KB
/
main.cpp
File metadata and controls
129 lines (85 loc) · 2.45 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
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
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
ifstream in_data1;
ofstream out_data1;
ofstream out_data2;
class bucketSort {
private:
int minData =0 , maxData=0, bucketSize, offset;
int* bucketAry;
public:
bucketSort(int min, int max) {
minData = min;
maxData = max;
offset=min;
bucketSize = max-min+1;
bucketAry = new int[bucketSize]();
}
~bucketSort() {
delete[] bucketAry;
}
void buildAry(ifstream& in_data) {
int num = 0;
if (in_data.is_open()) {
while (in_data>>num) {
bucketAry[num-offset]++;
}
}
}
void printBucketAry() {
out_data2<<"Index: ";
for(int i=0; i<19; i++) {
out_data2<<left << setw(3)<<i<<" ";
}
out_data2<<endl<<"Value: ";
for(int i=0; i<19; i++) {
out_data2<<left << setw(3)<<bucketAry[i]<<" ";
}
}
void printSortedData() {
out_data1<<"Sorted List: ";
for(int i=0; i<bucketSize; i++) {
while(bucketAry[i]!=0) {
if(bucketAry[i]!=0) out_data1<<i+offset<<" ";
bucketAry[i]--;
}
}
}
void getInfo() {
cout<<"min: "<<minData<<endl;
cout<<"max: "<<maxData<<endl;
cout<<"size: "<<bucketSize<<endl;
cout<<"offset: "<<offset<<endl;
cout<<bucketAry[197]<<endl;
}
};
void findMinMax(ifstream& in_data, int &mn, int &mx) {
int num;
if (in_data.is_open()) {
while (in_data>>num) {
if(mn==0) mn=num;
if (num < mn) mn = num;
if(num > mx) mx = num;
}
}
}
int main(int argc, char * argv[]) {
int min=0, max =0;
in_data1.open(argv[1]);
out_data1.open(argv[2]);
out_data2.open(argv[3]);
findMinMax(in_data1,min,max);
bucketSort myBucket (min,max);
in_data1.close();
in_data1.open(argv[1]);
myBucket.buildAry(in_data1);
myBucket.printBucketAry();
myBucket.printSortedData();
in_data1.close();
out_data1.close();
out_data2.close();
return 0;
}