-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvt.c
executable file
·1877 lines (1583 loc) · 43.5 KB
/
lvt.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "lvt.h"
#ifndef _PRINT_FUNCTIONS_
void print_pint(int *pint, size_t size)
{
for (size_t i = 0ul; i < size; i++)
printf("%d ", pint[i]);
printf("\n");
}
void print_pchar(char *pchar, size_t size)
{
for (size_t i = 0ul; i < size; i++)
printf("%c ", pchar[i]);
printf("\n");
}
void print_ppint(int **ppint, size_t rows, size_t cols)
{
// Simple iterating over the matrix by indeces of row and column
for (size_t row = 0ul; row < rows; row++)
{
for (size_t col = 0ul; col < cols; col++)
{
printf("%d\t", ppint[row][col]);
}
printf("\n");
}
}
void print_ppchar(int **ppchar, size_t rows, size_t cols)
{
for (size_t row = 0ul; row < rows; row++)
{
for (size_t col = 0ul; col < cols; col++)
{
printf("%c\t", ppchar[row][col]);
}
printf("\n");
}
}
#endif // !_PRINT_FUNCTIONS_
#ifndef _MEMORY_
#ifndef _CHECK_ALLOCATING_
bool is_alloc_pint(int *pint)
{
return (!pint) ? false : true;
}
bool is_alloc_ppint(int **ppint, size_t rows)
{
for (size_t row = 0ul; row < rows; row++)
{
if (!ppint[row])
return false;
}
if (!ppint)
return false;
return true;
}
bool is_alloc_pchar(char *pchar)
{
return (!pchar) ? false : true;
}
bool is_alloc_ppchar(int **ppchar, size_t rows)
{
for (size_t row = 0ul; row < rows; row++)
{
if (!ppchar[row])
return false;
}
if (!ppchar)
return false;
return true;
}
bool is_alloc_pdouble(double *pd)
{
return (!pd) ? false : true;
}
bool is_alloc_ppdouble(double **ppd, size_t rows)
{
for (size_t row = 0ul; row < rows; row++)
{
if (!ppd[row])
return false;
}
if (!ppd)
return false;
return true;
}
#endif // !_CHECK_ALLOCATING_
#ifndef _COMPARATORS_
int cmp_int_asc(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
int cmp_int_desc(const void *a, const void *b) { return (*(int *)b - *(int *)a); }
#endif // !_COMPARATORS_
#ifndef _ALLOCATING_
int *alloc_mem_pint(size_t size)
{
int *p = (int *)calloc(size, sizeof(int));
if (!is_alloc_pint(p))
{
printf("Can't allocate memory for array of integers. Exiting with \'-1\' status\n");
exit(-1);
}
return p;
}
int **alloc_mem_ppint(size_t rows, size_t cols)
{
int **pp = (int **)calloc(cols, sizeof(int *));
if (!pp)
{
printf("Can't allocate memory for each row of matrix. Exiting with \'-1\' status\n");
exit(-1);
}
for (size_t row = 0ul; row < rows; row++)
{
pp[row] = (int *)calloc(rows, sizeof(int));
if (!pp[row])
{
printf("Can't allocate memory for each column of matrix. Exiting with \'-1\' status\n");
exit(-1);
}
}
return pp;
}
char *alloc_mem_pchar(size_t size)
{
char *p = (char *)calloc(size, sizeof(char));
if (!is_alloc_pchar(p))
{
printf("Can't allocate memory for array of characters. Exiting with \'-1\' status\n");
exit(-1);
}
return p;
}
char **alloc_mem_ppchar(size_t rows, size_t cols)
{
char **pp = (char **)calloc(cols, sizeof(char *));
if (!pp)
{
printf("Can't allocate memory for each row of matrix. Exiting with \'-1\' status\n");
exit(-1);
}
for (size_t row = 0ul; row < rows; row++)
{
pp[row] = (char *)calloc(rows, sizeof(char));
if (!pp[row])
{
printf("Can't allocate memory for each column of matrix. Exiting with \'-1\' status\n");
exit(-1);
}
}
return pp;
}
double *alloc_mem_pdouble(size_t size)
{
double *p = (double *)calloc(size, sizeof(double));
if (!is_alloc_pdouble(p))
{
printf("Can't allocate memory for array of doubles. Exiting with \'-1\' status\n");
exit(-1);
}
return p;
}
double **alloc_mem_ppdouble(size_t rows, size_t cols)
{
double **pp = (double **)calloc(cols, sizeof(double *));
if (!pp)
{
printf("Can't allocate memory for each row of matrix. Exiting with \'-1\' status\n");
exit(-1);
}
for (size_t row = 0ul; row < rows; row++)
{
pp[row] = (double *)calloc(rows, sizeof(double));
if (!pp[row])
{
printf("Can't allocate memory for each column of matrix. Exiting with \'-1\' status\n");
exit(-1);
}
}
return pp;
}
#endif // !_ALLOCATING_
#ifndef _DEALLOCATING_
void dealloc_mem_pint(int *pint)
{
free(pint);
pint = NULL;
}
void dealloc_mem_ppint(int **ppint, size_t rows)
{
for (size_t row = 0ul; row < rows; row++)
{
free(ppint[row]);
ppint[row] = NULL;
}
free(ppint);
ppint = NULL;
}
void dealloc_mem_pchar(char *pchar)
{
free(pchar);
pchar = NULL;
}
void dealloc_mem_ppchar(char **ppchar, size_t rows)
{
for (size_t row = 0ul; row < rows; row++)
{
free(ppchar[row]);
ppchar[row] = NULL;
}
free(ppchar);
ppchar = NULL;
}
void dealloc_mem_pdouble(double *pd)
{
free(pd);
pd = NULL;
}
void dealloc_mem_ppdouble(double **ppd, size_t rows)
{
for (size_t row = 0ul; row < rows; row++)
{
free(ppd[row]);
ppd[row] = NULL;
}
free(ppd);
ppd = NULL;
}
#endif // !_DEALLOCATING_
#endif // !_MEMORY_
#ifndef _INPUT_
int input_int()
{
char buf[256] = {0};
scanf("%s", buf);
while ((atoi(buf) == 0) && (buf[0] != '0'))
{
printf("Input error. Try again: ");
scanf("%s", buf);
}
return atoi(buf);
}
double input_double()
{
char buf[256] = {0};
scanf("%s", buf);
while ((atof(buf) == 0.0) && (buf[0] != '0'))
{
printf("Input error. Try again: ");
scanf("%s", buf);
}
return atof(buf);
}
int *input_manual_pint(size_t size)
{
int *parr = alloc_mem_pint(size);
for (size_t i = 0ul; i < size; i++)
{
printf("parr[%lu] = ", i);
parr[i] = input_int();
}
return parr;
}
int *input_random_pint(size_t size, int low, int high)
{
srand(time(NULL));
int *parr = alloc_mem_pint(size);
// Filling array with random integer values
for (size_t i = 0ul; i < size; i++)
parr[i] = rand() % low + high;
return parr;
}
int **input_manual_ppint(size_t rows, size_t cols)
{
int **matrix = alloc_mem_ppint(rows, cols);
for (size_t row = 0ul; row < rows; row++)
{
for (size_t col = 0ul; col < cols; col++)
{
printf("matrix[%ld][%ld] = ", row, col);
matrix[row][col] = input_int();
}
}
return matrix;
}
int **input_random_ppint(size_t rows, size_t cols, int low, int high)
{
srand(time(NULL));
int **matrix = alloc_mem_ppint(rows, cols);
for (size_t row = 0ul; row < rows; row++)
{
for (size_t col = 0ul; col < cols; col++)
{
matrix[row][col] = rand() % low + high;
}
}
return matrix;
}
double *input_random_pdouble(size_t size, double low, double high)
{
srand(time(NULL));
double *parr = alloc_mem_pdouble(size);
// Filling array with random integer values
for (size_t i = 0ul; i < size; i++)
{
double f = (double)rand() / RAND_MAX;
parr[i] = low + f * (high - low);
}
return parr;
}
double **input_random_ppdouble(size_t rows, size_t cols, double low, double high)
{
srand(time(NULL));
double **matrix = alloc_mem_ppdouble(rows, cols);
for (size_t row = 0ul; row < rows; row++)
{
for (size_t col = 0ul; col < cols; col++)
{
double f = (double)rand() / RAND_MAX;
matrix[row][col] = low + f * (high - low);
}
}
return matrix;
}
#endif // !_INPUT_
#ifndef _ALGORITHMS_
int get_count_of_digits_in_number(int n)
{
int size = 0;
while (n > 0)
{
n /= 10;
size++;
}
return size;
}
int get_count_of_digits_in_number_v2(int n) { return floor(log10(n)) + 1; }
int *split_number_on_digits(int n)
{
int size = get_count_of_digits_in_number(n), i = 0;
int *digits = alloc_mem_pint(size);
while (n > 0)
{
digits[i] = n % 10;
n /= 10;
i++;
}
return digits;
}
int *split_number_on_digits_ver2(int n, bool rev)
{
int size = get_count_of_digits_in_number(n), i = 0;
// Allocating memory for integer array
int *digits = (int *)calloc(size, sizeof(int));
// If memory not allocated properly -> exiting from the programm
if (!digits)
{
fprintf(stderr, "Error: split_number_on_digits(%d, ...): Can't allocate memory. Exiting with failure status: %s\n",
n, strerror(errno));
exit(EXIT_FAILURE);
}
while (n > 0)
{
digits[i] = n % 10;
n /= 10;
i++;
}
if (rev == false)
reverse_pint(digits, size);
return digits;
}
void reverse_pint(int *pint, int size)
{
int temp = 0;
for (int i = 0; i < size / 2; i++)
{
temp = pint[i];
pint[i] = pint[size - i - 1];
pint[size - i - 1] = temp;
}
}
char *int_to_pchar(int n)
{
int size = log10(n) + 1;
char *pChar = alloc_mem_pchar((size_t)size);
for (int i = size - 1; i >= 0; i--, n /= 10)
pChar[i] = (n % 10) + '0';
return pChar;
}
void swap_ints(int *a, int *b)
{
// 5 and 3
*a += *b; // 8 3
*b -= *a; // 8 -5
*b *= -1; // 8 5
*a -= *b; // 3 5
}
bool is_prime(int n)
{
if (n <= 0)
return false;
for (int i = 2; i <= (int)sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
void bubbleSortAscending(int *arr, size_t size)
{
for (size_t i = 0; i < size; i++)
for (size_t j = 0; j < size; j++)
if (arr[i] < arr[j])
SWAP(arr[i], arr[j]);
}
void bubbleSortMatrixAscending(int **matrix, size_t rows, size_t cols)
{
// Perform bubble sort on the diagonal elements
for (size_t i = 0; i < rows; i++)
for (size_t j = 0; j < rows; j++)
for (size_t k = 0; k < cols; k++)
for (size_t m = 0; m < cols; m++)
if (matrix[i][k] < matrix[j][m])
SWAP(matrix[i][k], matrix[j][m]);
}
void insertionSortAscending(int *arr, size_t size)
{
// Iterating by vector from 2nd element to end: [begin + 1; end]
for (size_t i = 1ul; i < size; i++)
{
// Initializing position of previous element from 'i'
size_t j = i - 1ul;
// Initializing currenint value = of vector
int value = arr[i];
// While position of prev element is lower than size of vector
// and element in this position is bigger than currenint value = ->
// assigning it to next element (j + 1) of vector
while (j < size && arr[j] > value)
{
arr[j + 1] = arr[j];
j--;
}
// Assigning current element to next from previous
arr[j + 1] = value;
}
}
void insertionSortMatrixAscending(int **matrix, size_t rows, size_t cols)
{
int *pint = alloc_mem_pint(rows * cols);
matrixToArr(matrix, pint, rows, cols);
for (size_t i = 1; i < rows * cols; i++)
{
int val = pint[i];
size_t pos = i - 1;
while (pos < rows * cols && pint[pos] > val)
{
pint[pos + 1] = pint[pos];
pos--;
}
pint[pos + 1] = val;
}
arrayToMatrix(pint, matrix, rows, cols);
}
void selectionSortAscending(int *arr, size_t size)
{
// Iterating over the range
for (size_t i = 0; i < size; i++)
{
// For example, minimal element is begin element of the vector
size_t minPos = i;
// Iterating over the unsorted range
for (size_t j = i + 1ul; j < size; j++)
// If element from the unsorted range is lower than the current ->
// assigning new position to 'minPos' variable
if (arr[j] < arr[minPos])
minPos = j;
// Swap minimal element with current
SWAP(arr[i], arr[minPos]);
}
}
void selectionSortMatrixAscending(int **matrix, size_t rows, size_t cols)
{
for (size_t row = 0; row < rows; row++)
{
for (size_t col = 0; col < cols; col++)
{
size_t minRow = row, minCol = col;
int min = matrix[row][col];
for (size_t j = col + 1; j < cols; j++)
{
if (matrix[row][j] < min)
{
minRow = row;
minCol = j;
min = matrix[row][j];
}
}
for (size_t i = row + 1; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
if (matrix[i][j] < min)
{
minRow = i;
minCol = j;
min = matrix[i][j];
}
}
}
matrix[minRow][minCol] = matrix[row][col];
SWAP(matrix[row][col], min);
}
}
}
void ShellSortAscending(int *arr, size_t size)
{
for (size_t interval = size / 2; interval > 0; interval /= 2)
{
for (size_t i = 0; i < size; i++)
{
int val = arr[i];
size_t j = 0;
for (j = i; (j >= interval) && (arr[j - interval] > val); j -= interval)
{
arr[j] = arr[j - interval];
}
arr[j] = val;
}
}
}
void ShellSortMatrixAscending(int **matrix, size_t rows, size_t cols)
{
int *pint = alloc_mem_pint(rows * cols);
matrixToArr(matrix, pint, rows, cols);
for (size_t gap = rows * cols / 2; gap > 0; gap /= 2)
{
for (size_t i = gap; i < rows * cols; i++)
{
int temp = pint[i];
size_t j = i;
while (j >= gap && pint[j - gap] > temp)
{
pint[j] = pint[j - gap];
j -= gap;
}
pint[j] = temp;
}
}
arrayToMatrix(pint, matrix, rows, cols);
}
void qSortAscending(int *arr, size_t size, size_t low, size_t high)
{
size_t i = low, j = high;
// Select pivot value
int pivot = arr[(i + j) / 2], tmp = 0;
while (i <= j && i < size && j < size)
{
while (arr[i] < pivot && i < size)
i++;
while (arr[j] > pivot && j < size)
j--;
if (i <= j && i < size && j < size)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
// Recursive call sorting to left side from pivot
if (j > low && j < size)
qSortAscending(arr, size, low, j);
// Recursive call sorting to right side from pivot
if (i < high && i < size)
qSortAscending(arr, size, i, high);
}
void quickSortAscending(int *arr, size_t size) { qSortAscending(arr, size, 0, size - 1); }
void quickSortMatrixAscending(int **matrix, size_t rows, size_t cols)
{
int *pint = alloc_mem_pint(rows * cols);
matrixToArr(matrix, pint, rows, cols);
quickSortAscending(pint, rows * cols);
arrayToMatrix(pint, matrix, rows, cols);
}
void bubbleSortDescending(int *arr, size_t size)
{
for (size_t i = 0; i < size; i++)
for (size_t j = 0; j < size; j++)
if (arr[i] > arr[j])
SWAP(arr[i], arr[j]);
}
void bubbleSortMatrixDescending(int **matrix, size_t rows, size_t cols)
{
for (size_t i = 0; i < rows; i++)
for (size_t j = 0; j < rows; j++)
for (size_t k = 0; k < cols; k++)
for (size_t m = 0; m < cols; m++)
if (matrix[i][k] > matrix[j][m])
SWAP(matrix[i][k], matrix[j][m]);
}
void insertionSortDescending(int *arr, size_t size)
{
// Iterating by vector from 2nd element to end: [begin + 1; end]
for (size_t i = 1ul; i < size; i++)
{
// Initializing currenint value = of vector
int val = arr[i];
// Initializing position of previous element from 'i'
size_t pos = i - 1;
// While position of prev element is lower than size of vector
// and element in this position is bigger than currenint value = ->
// assigning it to next element (j + 1) of vector
while (pos < size && arr[pos] < val)
{
arr[pos + 1] = arr[pos];
pos--;
}
// Assigning current element to next from previous
arr[pos + 1] = val;
}
}
void selectionSortDescending(int *arr, size_t size)
{
// Iterating over the range
for (size_t i = 0; i < size; i++)
{
// For example, minimal element is begin element of the vector
size_t maxPos = i;
// Iterating over the unsorted range
for (size_t j = i + 1ul; j < size; j++)
{
// If element from the unsorted range is lower than the current ->
// assigning new position to 'maxPos' variable
if (arr[j] > arr[maxPos])
maxPos = j;
}
// Swap minimal element with current
SWAP(arr[i], arr[maxPos]);
}
}
void ShellSortDescending(int *arr, size_t size)
{
for (size_t interval = size / 2; interval > 0; interval /= 2)
{
for (size_t i = 0; i < size; i++)
{
int val = arr[i];
size_t j = 0;
for (j = i; (j >= interval) && (arr[j - interval] < val); j -= interval)
arr[j] = arr[j - interval];
arr[j] = val;
}
}
}
void qSortDescending(int *arr, size_t size, size_t low, size_t high)
{
size_t i = low, j = high;
// Select pivot value
int pivot = arr[(i + j) / 2], tmp = 0;
while (i <= j && i < size && j < size)
{
while (arr[i] > pivot && i < size)
i++;
while (arr[j] < pivot && j < size)
j--;
if (i <= j && i < size && j < size)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
// Recursive call sorting to left side from pivot
if (j > low && j < size)
qSortDescending(arr, size, low, j);
// Recursive call sorting to right side from pivot
if (i < high && i < size)
qSortDescending(arr, size, i, high);
}
void quickSortDescending(int *arr, size_t size) { qSortDescending(arr, size, 0, size - 1); }
// Calculates next "run" (interval of values)
int next_run(int run) { return (run <= 1) ? 0 : (int)ceil(run / 2.0); }
void in_place_merge(int arr[], int start, int end)
{
int run = end - start + 1;
for (run = next_run(run); run > 0; run = next_run(run))
{
for (int i = start; i + run <= end; i++)
{
int j = i + run;
if (arr[i] > arr[j])
{
swap_ints(&arr[i], &arr[j]);
}
}
}
}
void inPlaceMergeSort(int arr[], int s, int e)
{
if (s == e)
return;
// Calculating mid to slice the
// array in two halves
int mid = (s + e) / 2;
// Recursive calls to sort left
// and right subarrays
inPlaceMergeSort(arr, s, mid);
inPlaceMergeSort(arr, mid + 1, e);
in_place_merge(arr, s, e);
}
void mergeSortDirect(int arr[], int size) { inPlaceMergeSort(arr, 0, size - 1); }
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1,
n2 = r - m;
// Create temporary arrays
int L[n1], R[n2];
// Copy data to temporary arrays L[] and R[]
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temporary arrays back into arr[l..r]
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
// Copy the remaining elements of L[], if any
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of R[], if any
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void merge_sort_natural(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow
int m = l + (r - l) / 2;
// Sort first and second halves
merge_sort_natural(arr, l, m);
merge_sort_natural(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
void mergeSortNatural(int arr[], int size) { merge_sort_natural(arr, 0, size - 1); }
void insertionSortForTimsort(int arr[], int left, int right)
{
for (int i = left + 1; i <= right; i++)
{
int key = arr[i], j = i - 1;
while (j >= left && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void timsort(int arr[], int n)
{
for (int i = 0; i < n; i += RUN)
insertionSortForTimsort(arr, i, MIN((i + RUN - 1), (n - 1)));
for (int size = RUN; size < n; size = 2 * size)
{
for (int left = 0; left < n; left += 2 * size)
{
int mid = MIN((left + size - 1), (n - 1));
int right = MIN((left + 2 * size - 1), (n - 1));
merge(arr, left, mid, right);
}
}
}
void matrixToArr(int **src, int *dest, size_t rows, size_t cols)
{
size_t counter = 0;
for (size_t i = 0; i < rows; i++)
for (size_t j = 0; j < cols; j++)
{
dest[counter] = src[i][j];
++counter;
}
}
void arrayToMatrix(int *src, int **dest, size_t rows, size_t cols)
{
size_t counter = 0;
for (size_t i = 0; i < rows && counter < rows * cols; i++)
for (size_t j = 0; j < cols; j++)
{
dest[i][j] = src[counter];
counter++;
}
}
unsigned long hex_to_ulong(const char *str) { return strtoul(str, NULL, 0x10); }
char *str_to_upper(char *str)
{
// If string is empty no need to perform any actions with it
if (strlen(str) == 0)
return "";
char *copy = alloc_mem_pchar(strlen(str));
strcpy(copy, str);
// Modifying each character of string 'str' to uppercase
for (size_t i = 0; i < strlen(str); i++)
copy[i] = toupper(copy[i]);
return copy;
}
char *str_to_lower(char *str)
{
if (strlen(str) == 0)
return "";
char *copy = alloc_mem_pchar(strlen(str));
strcpy(copy, str);
for (size_t i = 0; i < strlen(str); i++)
copy[i] = tolower(copy[i]);
return copy;
}
char *int_to_hex(int value, size_t maxlen, char const *format)
{
char *hex = (char *)calloc(maxlen, sizeof(char));
if (!hex)
{
fprintf(stderr, "Can't allocate memory: %s\n", strerror(errno));
exit(1);
}
snprintf(hex, maxlen, format, value);
hex[maxlen + 1] = 0x00;
return hex;
}
int hex_to_int(const char *hex, const char *format)
{
int res = 0;
sscanf(hex, format, &res);
return res;
}
unsigned hex_to_uint(const char *hex, const char *format)