-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
193 lines (118 loc) · 3.96 KB
/
main.cpp
File metadata and controls
193 lines (118 loc) · 3.96 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
ifstream in_data;
ofstream out_data1;
ofstream out_data2;
class HeapSort {
private:
int* heapAry;
int size_array;
public:
HeapSort(int size) {
heapAry = new int[size]();
size_array = size;
}
~HeapSort() {
delete[] heapAry;
}
bool isHeapFull() {
if(heapAry[0]==size_array-1) return true;
else return false;
}
void insertOneDataItem(int newnum) {
if(isHeapFull()==true) { //check if array is full
cout<<"\nArray full on attempt to insert: "<<newnum<<"\n\n";
return;
}
heapAry[0]++; //increase counter of elements with each insertion call
heapAry[heapAry[0]] = newnum;
bubbleUp();
}
void bubbleUp() {
int current_Index = heapAry[0];
while( heapAry[current_Index] < heapAry[current_Index/2] && current_Index!=1) {
int temp = heapAry[current_Index]; //save current value of index to temp
heapAry[current_Index] = heapAry[current_Index/2]; // tranfser value of larger parent to child slot (move down)
heapAry[current_Index/2] = temp; //transfer value of temp (saved child value) to parent;
current_Index = current_Index/2; //set new index to next parent
}
}
bool isHeapEmpty() {
if(heapAry[0]==0) return true;
else return false;
}
int deleteRoot() {
int root = heapAry[1];
if(isHeapEmpty()==true) {
cout<<"\nArray Empty!\n";
return 0;
}
heapAry[1] = heapAry[heapAry[0]]; //'set' last leaf on the heap to root
heapAry[heapAry[0]]=0; //'delete' last leaf on the heap
heapAry[0]--;
//if (heapAry[0] == 1) return root;
if (heapAry[0] > 0) bubbleDown();
return root;
}
void buildHeap(ifstream& in_file, ofstream& out_file) {
int num;
while (in_file >> num) {
out_file << "inserting: " << setw(3) << setfill(' ') << num << "\t| ";
insertOneDataItem(num);
print(out_file, 10);
}
}
void print(ofstream& out_file, int limit) {
for (int i = 1; i <= heapAry[0] && i <= limit; i++)
out_file << setw(3) << setfill(' ') << heapAry[i] << " ";
out_file << endl;
}
void deleteHeap(ofstream& out_file) {
while (!isHeapEmpty()) {
out_file << "deleting: " << setw(3) << setfill(' ') << deleteRoot() << "\t| ";
print(out_file, 10);
}
out_file << endl;
}
void bubbleDown() {
int currentIndex, minChild;
currentIndex = 1;//begin at 1 (root)
while(currentIndex < heapAry[0]) {
if(currentIndex*2+1 <= heapAry[0]) {
if(heapAry[currentIndex*2] < heapAry[currentIndex*2+1]) minChild = currentIndex*2;
else minChild = currentIndex*2+1;
}
else if(currentIndex*2 <= heapAry[0]) minChild = currentIndex*2;
else return;
if(heapAry[currentIndex] > heapAry[minChild]) {
int temp = heapAry[currentIndex];
heapAry[currentIndex] = heapAry[minChild];
heapAry[minChild] = temp;
}
else return;
currentIndex = minChild;
}
}
};
int main(int argc, char * argv[]) {
in_data.open(argv[1]);
out_data1.open(argv[2]);
out_data2.open(argv[3]);
int count,num;
if (in_data.is_open()) {
while (in_data>>num) {
count++; //count num of elements in txt file
out_data1<<num<<" ";
}
out_data1<<"\n"<<"Total number of elements found in file: "<<count<<"\n\n";
in_data.close();
}
HeapSort myHeap (count+1);
in_data.open(argv[1]);
myHeap.buildHeap(in_data, out_data1);
myHeap.deleteHeap(out_data2);
return 0;
}