-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_HW3.cpp
More file actions
159 lines (120 loc) · 2.93 KB
/
2_HW3.cpp
File metadata and controls
159 lines (120 loc) · 2.93 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
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <iostream>
constexpr size_t STACK_MAX_SIZE = 256;
constexpr size_t TOP = STACK_MAX_SIZE - 1;
/* Function Prototypes */
void PushBack(int stack[], int element);
void Pop(int stack[]);
size_t Find(int stack[], int element);
void QuickSort(int stack[], int low, int high);
int partition(int stack[], int low, int high);
void swap(int element1, int element2);
void PushBack(int stack[], int element) {
int top = stack[TOP];
if (top == STACK_MAX_SIZE - 1) return;
stack[top] = element;
stack[TOP]++;
}
void Pop(int stack[]) {
int top = stack[TOP];
if (top == 0) return;
stack[top] = 0;
stack[TOP]--;
}
size_t Find(int stack[], int element) {
for (int i = 0; i < STACK_MAX_SIZE; ++i)
{
if (stack[i] == element)
{
return i;
break;
}
}
return -1;
}
int partition(int stack[], int low, int high) {
int pivotIdx = (low + high) / 2;
int l = low;
int h = high;
while (l < h) {
while (stack[l] < stack[pivotIdx] && l < h){
l++; }
while (stack[h] >= stack[pivotIdx] && l < h){
h--;}
if (l < h) {
if (l == pivotIdx) {
pivotIdx = h;
}
swap(stack[l], stack[h]);
}
}
swap(stack[pivotIdx], stack[h]);
pivotIdx = h;
return pivotIdx;
}
void swap(int element1, int element2) {
int pivotIdx = element1;
element1 = element2;
element2 = pivotIdx;
}
void QuickSort(int stack[], int low, int high) {
if (low < high) {
int pivotIdx = partition(stack, low, high);
QuickSort(stack, low, pivotIdx - 1);
QuickSort(stack, pivotIdx + 1, high);
}
}
int main()
{
int stack[STACK_MAX_SIZE];
stack[STACK_MAX_SIZE - 1] = 0;
for (size_t i = 0; i < STACK_MAX_SIZE - 1; ++i)
{
PushBack(stack, i);
}
assert(stack[STACK_MAX_SIZE - 1] == STACK_MAX_SIZE - 1);
// bitshift 연산을 검색해볼 것. bitshift divided by 2라고 구글링
for (size_t i = 0; i < (STACK_MAX_SIZE >> 1) - 1; ++i)
{
Pop(stack);
}
assert(stack[STACK_MAX_SIZE - 1] == (STACK_MAX_SIZE >> 1));
for (size_t i = 0; i < STACK_MAX_SIZE - 1; ++i)
{
size_t index = Find(stack, i);
if (i < (STACK_MAX_SIZE >> 1))
{
assert(index == i);
std::cout << "Element " << i << " is at index " << index << std::endl;
}
else
{
assert(index == -1);
std::cout << "Element " << i << " is not in the stack" << std::endl;
}
}
srand(time(NULL));
for (size_t i = 0; i < (STACK_MAX_SIZE >> 1); ++i)
{
PushBack(stack, rand() % STACK_MAX_SIZE + STACK_MAX_SIZE);
}
assert(stack[STACK_MAX_SIZE - 1] == STACK_MAX_SIZE - 1);
QuickSort(stack, 0, static_cast<int>(STACK_MAX_SIZE - 2));
for (size_t i = 0; i < stack[STACK_MAX_SIZE - 1]; ++i)
{
for (size_t j = 0; j < stack[STACK_MAX_SIZE - 1]; ++j)
{
if (j < i)
{
assert(stack[j] <= stack[i]);
}
else
{
assert(stack[i] <= stack[j]);
}
}
}
return 0;
}