This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortableArray.h
192 lines (168 loc) · 3.6 KB
/
SortableArray.h
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
// Lab 14a, Write A Sortable Array Class Template
// Programmer: Minos Park
// Editor(s) used: Sublime Text 2
// Compiler(s) used: G++
#include <algorithm>
#include <stack>
using namespace std;
#ifndef SortableArray_h
#define SortableArray_h
template <class DataType, int CAPACITY>
class SortableArray
{
DataType data[CAPACITY], dummy;
bool inUse[CAPACITY];
public:
SortableArray(){for(int i = 0; i < CAPACITY; i++) inUse[i] = false;}
DataType operator[](int) const;
DataType& operator[](int);
int capacity()const{return CAPACITY;}
int size()const;
int lsearch(const DataType&) const;
bool containskey(int i) const{return (i < 0 || i >= CAPACITY)?false:inUse[i];}
void deletekey(int);
void sort(int);
int bsearch(int,const DataType&) const;
};
template <class DataType, int CAPACITY>
DataType& SortableArray<DataType, CAPACITY>::operator[](int i)
{
if(i < 0 || i >= CAPACITY)
return dummy;
inUse[i] = true;
return data[i];
}
template <class DataType, int CAPACITY>
DataType SortableArray<DataType, CAPACITY>::operator[](int i) const
{
if(i < 0 || i >= CAPACITY || inUse[i] == false)
return dummy;
return data[i];
}
template <class DataType, int CAPACITY>
int SortableArray<DataType, CAPACITY>::size() const
{
int count=0;
for(int i = 0; i < CAPACITY; i++)
{
if(inUse[i] == true)
count++;
}
return count;
}
template <class DataType, int CAPACITY>
int SortableArray<DataType, CAPACITY>::lsearch(const DataType& value) const
{
for(int i = 0; i < CAPACITY; i++)
{
if(inUse[i] == true && data[i] == value)
return i;
}
return -1;
}
template <class DataType, int CAPACITY>
void SortableArray<DataType, CAPACITY>::deletekey(int i)
{
if(i < 0 || i >= CAPACITY)
return;
inUse[i] = false;
}
template <class DataType, int CAPACITY>
void SortableArray<DataType, CAPACITY>::sort(int range)
{
stack<pair<int, int> > s;
pair<int, int> p;
p.first = 0;
p.second = range;
s.push(p);
while(!s.empty())
{
int head = s.top().first;
int back = s.top().second;
s.pop();
if(head >= back - 1)
continue;
int pivot = (head + back - 1) / 2;
int left = head;
int right = back - 1;
while(1)
{
while(left < pivot)
{
if(data[pivot] < data[left])
break;
left+=1;
}
while(pivot < right)
{
if(data[right] < data[pivot])
break;
right-=1;
}
if(left == right)
{
pair<int, int> p;
p.first = head;
p.second = pivot;
s.push(p);
p.first = pivot + 1;
p.second = back;
s.push(p);
break;
}
else if (left == pivot)
{
swap(data[pivot], data[right]);
pivot = right;
left+=1;
}
else if (pivot == right)
{
swap(data[pivot], data[left]);
pivot = left;
right-=1;
}
else
{
swap(data[left], data[right]);
left+=1;
right-=1;
}
}
}
}
template <class DataType, int CAPACITY>
int SortableArray<DataType, CAPACITY>::bsearch(int range, const DataType& target) const
{
int start = 0;
int end;
if(range > size())
range = size();
end = range - 1;
while(start != end)
{
int middle = (start + end) / 2;
if(data[middle] == target)
{
return middle;
}
else if(data[middle] > target)
{
if(middle == start)
break;
else
{
end = middle - 1;
}
}
else
{
if(middle == end)
break;
else
start = middle + 1;
}
}
return -1;
}
#endif