Skip to content

Commit c900382

Browse files
committed
Ajusts
Ajusted TimSort, README and changed number lengths in commented mains of all sorts to sizeof(array) / sizeof(long int). Changed the execution time variables in All_Sorts to unsigned long.
1 parent 3ccdbd8 commit c900382

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+180
-185
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Length of array | 10
104104
1. [Counting Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Non-Comparison_Distribution/Counting_Sort.c)
105105
1. [Gravity (Bead) Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Non-Comparison_Distribution/Gravity_(Bead)_Sort.c)
106106
1. [Pigeonhole Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Non-Comparison_Distribution/Pigeonhole_Sort.c)
107+
1. [Radix LSD Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Non-Comparison_Distribution/Radix_LSD.c)
107108

108109
### [Selection](https://github.com/h-ssiqueira/Sort_Algorithms/tree/master/Source/Selection)
109110
1. [Double Selection Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Selection/Double_Selection_Sort.c)
@@ -118,12 +119,12 @@ Length of array | 10
118119
Algorithm | Worst case | Best case | Average | Space complexity | In-place | Stable | Notes
119120
:---: | :---: | :---: | :---: | :---: | :---: | :---: | :---:
120121
[Bad Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Bad_Sort.c) | O(N³) | O(N³) | O(N³) | O(1) | **** | **** |
121-
[Bogo Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Bogo_Bogo_Sort.c) | | | | O(1) | **** | **** |
122-
[Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Bogo_Sort.c) | O((N+1)!) | O(N) | O((N+1)!) | O(1) | **** | **** | The worst case can be unbounded
123-
[Bubble Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Bubble_Bogo_Sort.c) | | | | O(1) | **** | **** |
124-
[Cocktail Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Cocktail_Bogo_Sort.c) | | | | O(1) | **** | **** |
125-
[Exchange Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Exchange_Bogo_Sort.c) | | | | O(1) | **** | **** |
126-
[Less Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Less_Bogo_Sort.c) | | | | O(1) | **** | **** |
122+
[Bogo Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Bogo_Bogo_Sort.c) | | | | O(1) | **** | **** | The worst case can ve unbounded due to random manipulation
123+
[Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Bogo_Sort.c) | O((N+1)!) | O(N) | O((N+1)!) | O(1) | **** | **** | The worst case can be unbounded due to random manipulation
124+
[Bubble Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Bubble_Bogo_Sort.c) | | | | O(1) | **** | **** | The worst case can be unbounded due to random manipulation
125+
[Cocktail Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Cocktail_Bogo_Sort.c) | | | | O(1) | **** | **** | The worst case can be unbounded due to random manipulation
126+
[Exchange Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Exchange_Bogo_Sort.c) | | | | O(1) | **** | **** | The worst case can be unbounded due to random manipulation
127+
[Less Bogo Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Less_Bogo_Sort.c) | | | | O(1) | **** | **** | The worst case can be unbounded due to random manipulation
127128
[Pancake Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Pancake_Sort.c) | O(N²) | O(N²) | O(N²) | O(1) | **** | **** |
128129
[Silly Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Silly_Sort.c) | | | | O(1) | **** | **** |
129130
[Slow Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Esoteric_Fun_Miscellaneous/Slow_Sort.c) | | | | O(1) | **** | **** |
@@ -148,7 +149,7 @@ Algorithm | Worst case | Best case | Average | Space complexity | In-place | Sta
148149
[Insertion Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Insertion/Insertion_Sort.c) | O(N²) | O(N) | O(N²) | O(1) | **** | **** |
149150
[Patience Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Insertion/Patience_Sort.c) | O(N log N) | O(N) | O(N log N) | O(N) | **** | **** |
150151
[Shell Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Insertion/Shell_Sort.c) | O(N<sup>3/2</sup>) or O(N log² N) | O(N log N) | --- | O(1) | **** | **** |
151-
[Tree Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Insertion/Tree_Sort.c) | O(N²) or O(N log N) | O(N log N) | O(N log N) | O(N) | **** | **** | In worst case, O(N²) when using Binary Search Tree and O(N log N) when using Self-Balanced Binary Search Tree
152+
[Tree Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Insertion/Tree_Sort.c) | O(N²) | O(N log N) | O(N log N) | O(N) | **** | **** | In worst case, O(N²) when using Binary Search Tree and O(N log N) when using Self-Balanced Binary Search Tree
152153
[Bottom-up Merge Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Merge/Bottom-Up_Merge_Sort.c) | O(N log N) | O(N log N) | O(N log N) | O(N) | **** | **** |
153154
[In-Place Merge Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Source/Merge/In-Place_Merge_Sort.c) | O(N²) | O(N²) | O(N²) | O(log N) | **** | **** |
154155
[Merge Sort](https://github.com/h-ssiqueira/Sort_Algorithms/blob/master/Merge/Source/Merge_Sort.c) | O(N log N) | O(N log N) | O(N log N) | O(N) | **** | **** |

Source/All_Sorts.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ void print(long int *array, int length);
3232
void generate(long int *array, int length, short int choice, int randominterval);
3333

3434
// Calculate execution time
35-
void calculatetime(struct timeval start, struct timeval end, long *sec, long *micro);
35+
void calculatetime(struct timeval start, struct timeval end, unsigned long *sec, unsigned long *micro);
3636

3737
// Get time before executing the sorting algorithm
3838
void BeforeExec(long int *array, int length, bool display, char *sort);
3939

4040
// Get time after executing the sorting algorithm
41-
void AfterExec(long int *array, int length, bool display, bool time, long sec, long micro);
41+
void AfterExec(long int *array, int length, bool display, bool time, unsigned long sec, unsigned long micro);
4242

4343
// Sorted increasing
4444
bool sorted(long int *array, int length);
@@ -51,7 +51,7 @@ int main(){
5151
long int *array, *arrayPOF2;
5252
int length = 10, i, powerof2 = 16, randominterval = 1024;
5353
short int option_sort, option_category, choice = 2;
54-
long sec, micro;
54+
unsigned long sec, micro;
5555
bool txtfile = false, displayarray = true, exectime = true;
5656

5757
create(&array,length);
@@ -914,7 +914,7 @@ int main(){
914914
}
915915
if(randominterval < 3)
916916
randominterval = 3;
917-
else if (randominterval > INT_MAX)
917+
else if(randominterval > INT_MAX)
918918
randominterval = INT_MAX;
919919
break;
920920
case 3:
@@ -984,10 +984,10 @@ void generate(long int *array, int length, short int choice, int randominterval)
984984
}
985985
}
986986

987-
void calculatetime(struct timeval start, struct timeval end, long *sec, long *micro){
987+
void calculatetime(struct timeval start, struct timeval end, unsigned long *sec, unsigned long *micro){
988988
*sec = end.tv_sec - start.tv_sec;
989989
*micro = end.tv_usec - start.tv_usec;
990-
printf("\n\tExecution time: %ld seconds %ld microsseconds.", *sec, *micro);
990+
printf("\n\tExecution time: %lu seconds %lu microsseconds.", *sec, *micro);
991991
}
992992

993993
void BeforeExec(long int *array, int length, bool display, char *sort){
@@ -1001,14 +1001,13 @@ void BeforeExec(long int *array, int length, bool display, char *sort){
10011001
fprintf(txt,"\n");
10021002
}
10031003
}
1004-
else{
1004+
else
10051005
printf("\n\tError: Cannot open the file.");
1006-
}
10071006
if(txt != NULL)
10081007
fclose(txt);
10091008
}
10101009

1011-
void AfterExec(long int *array, int length, bool display, bool time, long sec, long micro){
1010+
void AfterExec(long int *array, int length, bool display, bool time, unsigned long sec, unsigned long micro){
10121011
FILE *txt = fopen("data.txt","a+");
10131012
if(txt != NULL){
10141013
if(display){
@@ -1018,11 +1017,10 @@ void AfterExec(long int *array, int length, bool display, bool time, long sec, l
10181017
fprintf(txt,"\n");
10191018
}
10201019
if(time)
1021-
fprintf(txt,"\n\tExecution time: %ld seconds %ld microsseconds.\n", sec, micro);
1020+
fprintf(txt,"\n\tExecution time: %lu seconds %lu microsseconds.\n", sec, micro);
10221021
}
1023-
else{
1022+
else
10241023
printf("\n\tError: Cannot open the file.");
1025-
}
10261024
if(txt != NULL)
10271025
fclose(txt);
10281026
}

Source/Esoteric_Fun_Miscellaneous/Bad_Sort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ void BadSort(long int *array, int length){
3333
int main(){
3434
long int array[] = {131,418,457,76,631,739,462,299,716,68},*i;
3535
36-
for(i = array; i < array + 10; i++)
36+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
3737
printf("%ld ",*i);
3838
3939
printf("\n\n");
40-
BadSort(array,10);
41-
for(i = array; i < array + 10; i++)
40+
BadSort(array,sizeof(array)/sizeof(long int));
41+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
4242
printf("%ld ",*i);
4343
4444
return 0;

Source/Esoteric_Fun_Miscellaneous/Bogo_Bogo_Sort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ int main(){
4141
srand(time(NULL));
4242
long int array[] = {299,581,698,6,451,285,366,253,531,686},*i;
4343
44-
for(i = array; i < array + 10; i++)
44+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
4545
printf("%ld ",*i);
4646
4747
printf("\n\n");
48-
BogoBogoSort(array,10);
48+
BogoBogoSort(array,sizeof(array)/sizeof(long int));
4949
50-
for(i = array; i < array + 10; i++)
50+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
5151
printf("%ld ",*i);
5252
return 0;
5353
}

Source/Esoteric_Fun_Miscellaneous/Bogo_Sort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ int main(){
3333
srand(time(NULL));
3434
long int array[] = {1,10,9,7,3,0,50,654,222,-2}, *i;
3535
36-
for(i = array; i < array + 10; i++)
36+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
3737
printf("%ld ", *i);
3838
3939
printf("\n\n");
40-
bogo_sort(array,10);
40+
bogo_sort(array,sizeof(array)/sizeof(long int));
4141
42-
for(i = array; i < array + 10; i++)
42+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
4343
printf("%ld ", *i);
4444
return 0;
4545
}

Source/Esoteric_Fun_Miscellaneous/Bubble_Bogo_Sort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ void BubbleBogoSort(long int *array, int length){
2828
int main(){
2929
srand(time(NULL));
3030
long int array[] = {517,418,663,859,992,119,508,887,672,407}, *i;
31-
for(i = array; i < array + 10; i++)
31+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
3232
printf("%ld ",*i);
3333
3434
printf("\n\n");
3535
36-
BubbleBogoSort(array,10);
36+
BubbleBogoSort(array,sizeof(array)/sizeof(long int));
3737
38-
for(i = array; i < array + 10; i++)
38+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
3939
printf("%ld ",*i);
4040
return 0;
4141
}

Source/Esoteric_Fun_Miscellaneous/Cocktail_Bogo_Sort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ void CocktailBogoSort(long int *array, int length){
6161
int main(){
6262
srand(time(NULL));
6363
long int array[] = {920,153,587,320,302,722,902,916,637,346},*i;
64-
for(i = array; i < array + 5; i++)
64+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
6565
printf("%ld ",*i);
6666
6767
printf("\n\n");
6868
69-
CocktailBogoSort(array,5);
69+
CocktailBogoSort(array,sizeof(array)/sizeof(long int));
7070
71-
for(i = array; i < array + 5; i++)
71+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
7272
printf("%ld ",*i);
7373
return 0;
7474
}

Source/Esoteric_Fun_Miscellaneous/Exchange_Bogo_Sort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ void ExchangeBogoSort(long int *array, int length){
3838
int main(){
3939
srand(time(NULL));
4040
long int array[] = {180,979,34,25,484,70,341,755,293,365}, *i;
41-
for(i = array; i < array + 10; i++)
41+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
4242
printf("%ld ",*i);
4343
4444
printf("\n\n");
4545
46-
ExchangeBogoSort(array,10);
46+
ExchangeBogoSort(array,sizeof(array)/sizeof(long int));
4747
48-
for(i = array; i < array + 10; i++)
48+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
4949
printf("%ld ",*i);
5050
return 0;
5151
}

Source/Esoteric_Fun_Miscellaneous/Less_Bogo_Sort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ void LessBogoSort(long int *array, int length){
2929
int main(){
3030
srand(time(NULL));
3131
long int array[] = {180,979,34,25,484,70,341,755,293,365}, *i;
32-
for(i = array; i < array + 10; i++)
32+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
3333
printf("%ld ",*i);
3434
3535
printf("\n\n");
3636
37-
LessBogoSort(array,10);
37+
LessBogoSort(array,sizeof(array)/sizeof(long int));
3838
39-
for(i = array; i < array + 10; i++)
39+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
4040
printf("%ld ",*i);
4141
return 0;
4242
}

Source/Esoteric_Fun_Miscellaneous/Pancake_Sort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ void PancakeSort(long int *array, int length){
3333
int main(){
3434
long int array[] = {82,458,594,767,876,700,943,751,7,368},*i;
3535
36-
for(i = array; i < array + 10; i++)
36+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
3737
printf("%ld ",*i);
3838
3939
printf("\n\n");
40-
PancakeSort(array,10);
41-
for(i = array; i < array + 10; i++)
40+
PancakeSort(array,sizeof(array)/sizeof(long int));
41+
for(i = array; i < array + sizeof(array)/sizeof(long int); i++)
4242
printf("%ld ",*i);
4343
4444
return 0;

0 commit comments

Comments
 (0)