-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.h
More file actions
254 lines (215 loc) · 4.38 KB
/
sort.h
File metadata and controls
254 lines (215 loc) · 4.38 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
using namespace std;
#include <algorithm>
using namespace std;
int hswap = 0;
int mswap = 0;
int qswap = 0;
int sswap = 0;
int iswap = 0;
int hcount = 0;
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
{largest = l; hcount++;}
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
{largest = r; hcount++;}
// If largest is not root
if (largest != i)
{
swap(arr[i], arr[largest]);
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
// main function to do heap sort
void heapSort(int arr[], int n)
{
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
{
heapify(arr, n, i);
hcount++;
}
// One by one extract an element from heap
for (int i=n-1; i>0; i--)
{
hcount++;
// Move current root to end
swap(arr[0], arr[i]);
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
int mcount = 0;
void merge (int a[], int lf, int ll, int rf, int rl)
{
int temp[100];
int j = lf;
int save = lf;
while ( (lf <= ll) && (rf <= rl))
{
mcount++;
if (a[lf] < a[rf])
{
temp[j++] = a[lf++];
mcount++;
}
else
temp [j++] = a[rf++];
}
while (lf <= ll )
{
temp[j++] = a[lf++];
mcount++;
}
while ( rf <= rl )
{
temp[j++] = a[rf++];
mcount++;
}
for ( j = save; j<= rl; j++)
{
a[j] = temp[j];
mcount++;
}
}
void mergeSort (int a[], int first, int last)
{
if ( first < last )
{
mcount++;
int middle = (first+last) / 2;
mergeSort (a,first,middle);
mergeSort (a,middle+1,last);
merge (a,first,middle,middle+1,last);
}
}
int qcount = 0;
void swap ( int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void partition ( int a[], int first, int last, int &p)
{
int pivot = a[first];
int j;
p = first;
for ( j = first+1; j <= last; j++)
{
qcount++;
if ( a[j] < pivot )
{
qcount++;
p++;
swap (a[p], a[j]);
}
}
swap (a[first], a[p]);
}
void quickSort ( int a[], int first, int last)
{
if ( first < last )
{
qcount++;
int point;
partition (a,first,last,point);
quickSort (a,first,point-1 );
quickSort (a, point+1, last);
}
}
int scount = 0;
void selectionSort ( int a[], int n )
{
int i,j, min;
for ( i = 0; i < n-1; i++)
{
scount++;
min = i;
for ( j = i+1; j < n; j++ )
{
scount++;
if ( a[j] < a[min] )
{
scount++;
min = j;
}
}
swap (a[i], a[min]);
}
}
int icount = 0;
void insertionSort ( int a[], int n )
{
int i,j;
bool order;
for ( i = 1; i < n ; i++ )
{
icount++;
order = false;
j = i;
while ( j != 0 && !order)
{
icount++;
if ( a[j] < a[j-1] )
{
icount++;
swap (a[j], a[j-1]);
j--;
}
else order = true;
}
}
}
void bigMerge (int a[], int lf, int ll, int rf, int rl)
{
int temp[1000];
int j = lf;
int save = lf;
while ( (lf <= ll) && (rf <= rl))
{
mcount++;
if (a[lf] < a[rf])
{
temp[j++] = a[lf++];
mcount++;
}
else
temp [j++] = a[rf++];
}
while (lf <= ll )
{
temp[j++] = a[lf++];
mcount++;
}
while ( rf <= rl )
{
temp[j++] = a[rf++];
mcount++;
}
for ( j = save; j<= rl; j++)
{
a[j] = temp[j];
mcount++;
}
}
void bigMergeSort (int a[], int first, int last)
{
if ( first < last )
{
mcount++;
int middle = (first+last) / 2;
bigMergeSort (a,first,middle);
bigMergeSort (a,middle+1,last);
bigMerge (a,first,middle,middle+1,last);
}
}