-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
247 lines (168 loc) · 5.69 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
// #include <string.h>
#include <math.h>
typedef u_int8_t byte;
void swap_int(int *a, int *b);
void allocateArray_int(int **arrPtr, byte arrLen);
void getArrElemVals(int **arrPtr, int len);
void copyValue_int(int **dest, int **org, byte from, byte to);
// void appendValue_int(int **dest, int *org, byte orgLen);
void bubbleSort(int **arrPtr, byte arrLen);
void mergeSort(int **arrPtr, byte arrLen);
void printArrElems(int *arr, byte arrLen);
/*/ The Main Program /*/
int main() {
// Variables
auto int *arr;
byte arrLen;
// Get Array with It's Length
printf( "Please enter a length for array (even): " );
scanf("%i", &arrLen);
// Exit If The Length is Greater than 255
if(arrLen > 255) exit(0);
// if(arrLen % 2 != 0) exit(0);
// Allocated Array and Point to It
allocateArray_int(&arr, arrLen);
// Get Value of Elements from User
getArrElemVals(&arr, arrLen);
// Sort The Array Using Merge Sort Algorithm
mergeSort(&arr, arrLen);
// Print Array Elements
printArrElems(arr, arrLen);
free(arr);
return 0;
}
/*/ Swap Values /*/
void swap_int(int *a, int *b){
auto int tmp = *a;
*a = *b;
*b = tmp;
}
/*/ Point to Allocated Array /*/
void allocateArray_int(int **arrPtr, byte arrLen){
*arrPtr = calloc( (size_t) arrLen, sizeof(int) );
if (*arrPtr == NULL) {
printf("\nArray Allocation FAILED !!!\n");
abort();
}
}
/*/ Get Value of Array Elements from User Input /*/
void getArrElemVals(int **arrPtr, int len){
for( register byte i = 0; i < len; i++ ){
printf("Value of Index %i: ", i);
scanf( "%i", (*arrPtr + i) );
}
}
/*/ Copy Arr /*/
void copyValue_int(int **dest, int **org, byte from, byte to){
for (register byte i = 0, j = from; j < to; i++, j++)
*(*dest + i) = *(*org + j);
}
/*/ Append Arr /*/
/*void appendValue_int(int **dest, int *org, byte orgLen){
memcpy(*dest, org, orgLen * sizeof(int));
return;
}*/
/*/ Sort Array Using Bubble Sort Algorithm /*///e.g. {12, 6, 9, 11, 7, 10, 2, 5, 3, 1, 4, 8}
void bubbleSort(int **arrPtr, byte arrLen) {
// Main-Gauging
for (register byte i = arrLen - 1; i > 0; i--) {
// Sub-Gauging
for (register byte j = 0; j < i; j++) {
// Swap
// if :: This Element is Greater Than The Next One
if (*(*arrPtr + j) > *(*arrPtr + j + 1)) {
swap_int(*arrPtr + j, *arrPtr + j + 1);
}
}
}
}
/*/ Sort Array Using Merge Sort Algorithm /*/
//e.g. {12, 6, 9, 11, 7, 10, 2, 5, 3, 1, 4, 8}
//e.g. {4, 3, 2, 1}
void mergeSort(int **arrPtr, byte arrLen){
if (arrLen < 2) return;
// Define Left & Right Arrays
byte leftLen = (byte) ceil(arrLen / 2.0);
byte rightLen = (byte) floor(arrLen / 2.0);
auto int *leftArr ; allocateArray_int(&leftArr, leftLen);
auto int *rightArr; allocateArray_int(&rightArr, rightLen);
// Copy Left & Right Values in Them
copyValue_int(&leftArr, arrPtr, 0, leftLen);
copyValue_int(&rightArr, arrPtr, leftLen, arrLen);
// Sort The Left And Right
mergeSort(&leftArr, leftLen);
mergeSort(&rightArr, rightLen);
/////////////////////////
// Merge Them Together //
/////////////////////////
register byte k = 0; // Main Array Index
register byte i = 0; // Left Array Index
register byte j = 0; // Right Array Index
// After Using Them We Need a SubArray
auto int *subArr;
register byte subArrLen = 0;
// Loop
// if :: left indexes are not finished
// and if :: right indexes are not finished
while (i < leftLen && j < rightLen) {
// if :: right element is greater than the left one
if(*(leftArr + i) < *(rightArr + j)) {
*(*arrPtr + k) = *(leftArr + i);
i++;
// else if :: left element is greater than the right one or they are equivalent
} else {
*(*arrPtr + k) = *(rightArr + j);
j++;
}
k++;
}
////////////////////////////////////////////////
// Let's use the sub array (( if necessary )) //
////////////////////////////////////////////////
// if :: left indexes are finished
if (i == leftLen && j != rightLen) {
// Make Left Array Free (( we don't need it more ))
free(leftArr);
// Calculate The Length of Sub Array
subArrLen = rightLen - j;
// Keep Right Array Remained Elements
allocateArray_int(&subArr, subArrLen);
// Copy Values on Sub Array
copyValue_int(&subArr, &rightArr, j, rightLen);
// Bubble Sort on Them
bubbleSort(&subArr, subArrLen);
// Skip The Next If Statement
goto subArrayIsReady;
}
// if :: right indexes are finished
if (j == rightLen && i != leftLen) {
// Make Right Array Free (( we don't need it more ))
free(rightArr);
// Calculate The Length of Sub Array
subArrLen = leftLen - i;
// Keep Right Array Remained Elements
allocateArray_int(&subArr, subArrLen);
// Copy Values on Sub Array
copyValue_int(&subArr, &leftArr, i, leftLen);
// Merge Sort on Them
bubbleSort(&subArr, subArrLen);
}
subArrayIsReady:
// Insert The Sorted Sub Array to The Main Array (( if it has any element ))
if (subArrLen > 0) {
register byte l = 0; // The Sub Array Index
while (k < arrLen) {
*(*arrPtr + k) = *(subArr + l);
l++;
k++;
}
}
}
/*/ Show Array Elements /*/
void printArrElems(int *arr, byte arrLen){
for (register byte i = 0; i < arrLen; i++)
printf("%i, ", *(arr + i));
printf("\b\b\n");
}