Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed code formatting, errors, and compilation warnings. Introduce new array input types, and additional sorting algorithms. Larger array sizes are now properly displayed, among several other changes. #31

Open
wants to merge 289 commits into
base: master
Choose a base branch
from

Conversation

Unbreakable-Syntax
Copy link

@Unbreakable-Syntax Unbreakable-Syntax commented Oct 4, 2024

I have a huge appreciation for this project, and now I want to introduce a lot of changes and improvements! I really hope this will be accepted.
 
New array input types:

  • Wave
  • Nearly Sorted
  • Scrambled Head/Tail
  • Pipe Organ
  • Bitonic Array (I named it Mirrored Organ in the file).
  • Sawtooth/Reverse Sawtooth
  • Many Similar (dynamic)
  • Quicksort Killer
    Credits to @MusicTheorist
  • Spike
  • Ribbon
  • Max/Flipped Min Heapified
  • Curve (Perlin Noise, Blancmange, Bell)
    Credits to the ArrayV project

These array input types could pose a challenge on some sorting algorithms such as Single Pivot Quick Sort.

I have fine-tuned the array generation for these new input types, these input types should work properly regardless of input size.

Additional sorting algorithms:

  • Pancake Sort
    This is a version of Pancake Sort that searches for the maximum element in n/2 time, allowing for slightly faster sort time
  • Dual Cocktail Shaker Sort
    Based on my current implementation of Cocktail Twister Sort
  • Optimized Gnome Sort
    This is a variant of Gnome Sort that uses a jump/leap optimization
  • Bead Sort
  • Grail Sort
  • Pattern-Defeating Quick Sort
  • Optimized Bubble Sort
  • Double Selection Sort
  • Circle Sort
  • Bad Sort
    Obtained from here
  • Sandpaper Sort
  • Double Sandpaper Sort
    Credits to @Taihennami
  • In-Place Radix Sort LSD (Base-10/Base-2)
    To alter between 2 bases, go to the InPlaceRadixSortLSD() method and modify the value of size_t bucket.
    Credits to @w0rthy, and "The Studio" Discord channel for teaching me how the major methods work!
  • Pairwise Sorting Network (Iterative and Recursive)
    Credits to @aphitorite
  • American Flag Sort
  • New Shuffle Merge Sort
    Credits to @EmeraldBlock
  • (Optimized) Weave Merge Sort
  • Andrey Astrelin's In-Place Merge Sort
  • Adjacency Pancake Sort
  • Gravity Sort
  • Proportion Extend Merge Sort
  • Buffer Partition Merge Sort
    Credits to @aphitorite for these 4 sorting algorithms starting from Weave Merge to Buffer Partition
  • Flan Sort (Quick Library Sort)
    Credits to @aphitorite and @Flanlaina

I recommend not removing the original variants (such as Gnome Sort and Bubble Sort) for demonstration and educational purposes.

New Quick Sort pivot rules:

  • Random Median of Three
  • Median of Five (random, pre-determined)
  • Median of Seven (random, pre-determined)
  • Median of Nine (random, pre-determined)
  • Ninther (random, pre-determined)
  • Median of Eleven (random, pre-determined)
  • Median of Fifteen
    Inspired on the idea of Median of Fifteen in Flux Sort by @scandum
  • Median of Twenty-One
  • Median of Three Ninther
    Credits to the developers of Flan Sort @aphitorite and @Flanlaina

Project enhancements:

  • Improved visual array rendering, this allows the project to properly display up to 16,384 elements maximum, even more if the user manually edits the array size slider. (1, 2, 3, 4)
  • Added a "swaps" counter, the project now counts the swaps performed by a sorting algorithm. (1, 2, 3, 4, 5, 6, 7, 8)

Bug fixes and warning reductions:

  • Fixed "cannot terminate thread: The handle is invalid" (incredibly hard to fix!) [1, 2]
  • Fixed "undefined reference to SDL_main" (hard to fix) [1, Reupdated Makefile due to accidental downgrade]
  • Fixed a couple of hidden code warnings, such as the SortAlgoThread() constructor not being detected (1, 2)
  • Fixed deprecated std::iterator (1, 2)
  • Fixed implicit operator= on ArrayItem (1)
  • Fixed deprecated random_shuffle (1)
  • Fixed warning-inducing access syntax on InsertionSort2, BogoCheckSorted, and CheckSorted (1, 2)
  • Fixes the issue in this pull request due to changes in watch()
  • Fixed leftover sound when a sorting algorithm has been reset under high delay (1, 2)

Miscellaneous changes:

  • The project now compiles with -O2 flag, improving performance on visual array rendering (1)
  • Increased framerate cap from 30 FPS to 45 FPS, allowing for smoother sorting animations. (1)
  • The project now immediately detects if a sorting algorithm has been cancelled, making the sorting cancellation to be incredibly fast even on high delays (1000 ms above) (1, 2, 3)
  • The project compiles under C++11 minimum, despite all the syntax changes (1)
  • The options panel will never disappear now (1)
  • The watch() method for live index tracking is now truly thread-safe with std::atomic<ssize_t> (1, 2)
  • Fixed one-off Binary Insertion Sort issue (<= to <)
  • Improved Heap Sort performance [credits to @chromi] (1)
  • Improved set() inversions calculation (1, 2)
  • Removed Yield() and TestDestroy() call (1)

The reason why this PR looked massive is because I made the pull request early on, and then I made several other changes as time went by. If it is desired to check out individual changes, then please see the commit history of my fork (200+ commits) to see all the changes I made.

If this is going to be done, then please pay attention only on the enhancements, bug fixes, warning reductions, and miscellaneous changes. All commits related to additional sorting algorithms, array input types, and pivot rules, as well as changes made to these additions, can be safely ignored.

I hope all these changes will be considered please! I spent a lot of time making sure that all my additions are not broken by testing it as much as possible. I also spent a lot of time debugging and fixing all the errors and warnings I could find too.

It has been an honor! I am happy to make this project evolve and improve so much.

@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 2 new array input types Introduce 6 new array input types, and 1 new quadratic sorting algorithm Oct 4, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 6 new array input types, and 1 new quadratic sorting algorithm Introduce 6 new array input types, and 2 new quadratic sorting algorithms Oct 5, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 6 new array input types, and 2 new quadratic sorting algorithms Introduce 6 new array input types, and 6 additional quadratic sorting algorithms Oct 5, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 6 new array input types, and 6 additional quadratic sorting algorithms Introduce 6 new array input types, and 7 additional quadratic sorting algorithms Oct 5, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 6 new array input types, and 7 additional quadratic sorting algorithms Introduce 7 new array input types, and 7 additional quadratic sorting algorithms Oct 5, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 7 new array input types, and 7 additional quadratic sorting algorithms Introduce 7 new array input types, and 8 additional quadratic sorting algorithms Oct 7, 2024
Bead Sort, before this fix, makes the last 2 elements after the sorting process is done to be equal, when they shouldn't be, this also results in a small chance of crashing the program. The issue is now resolved.
My declaration of int max is wrong, leading to incorrect results on reverse sorted array, this should FINALLY fix it now, I have also tested it out.
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 7 new array input types, and 8 additional quadratic sorting algorithms Introduce 9 new array input types, and 8 additional quadratic sorting algorithms Oct 7, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 9 new array input types, and 8 additional quadratic sorting algorithms Introduce 9 new array input types, and 9 additional sorting algorithms Oct 8, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 9 new array input types, and 9 additional sorting algorithms Introduce 9 new array input types, and 10 additional sorting algorithms Oct 9, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Introduce 9 new array input types, and 10 additional sorting algorithms Removed compilation warnings. Introduce 9 new array input types, and 10 additional sorting algorithms. Oct 9, 2024
Credits to @MusicTheorist for the Quicksort Killer input
@Unbreakable-Syntax Unbreakable-Syntax changed the title Removed compilation warnings. Introduce 9 new array input types, and 10 additional sorting algorithms. Removed compilation warnings. Introduce 10 new array input types, and 11 additional sorting algorithms. Oct 10, 2024
Credits to @w0rthy, and "The Studio" Discord channel for teaching me how the major methods work!
@Unbreakable-Syntax Unbreakable-Syntax changed the title Removed compilation warnings. Introduce 10 new array input types, and 11 additional sorting algorithms. Removed compilation warnings. Introduce 10 new array input types, and 12 additional sorting algorithms. Oct 10, 2024
This change allows the application to display as much as 8192 elements without the visualization going past the screen borders. It is possible to go past 8192 elements, but the application will lag at this point. 0.2 downscaling will not work for displaying large array sizes, 0.1 downscaling will allow displaying up to 12000 array elements, but again, this will cause huge lag.
This change complements the aggressive bar width downscaling change.
Reverted the normal calculation back to the original formula, this gives the best of both worlds. Array sizes within the 1000 range will not upscale past the application window border, and array sizes of 2000 and above will also never go past the window border. This should stabilize the aggressive downscale solution even more.
This introduces some corrections:
1. The SoundCallback() method is not apparently being detected, even if it is declared in SortSound.cpp, the reason for this is because the middle argument on the declaration of SoundCallback() is different compared to how it is defined in SortSound.cpp. Even if unsigned char is the same as Uint8, C++ still treats the 2 types as different, hence it says that SoundCallback() cannot be found. Therefore, the SoundCallback() declaration should match its definition, which is what this change introduces.

2. The constructor for SortAlgoThread is apparently not being detected properly by WSortView.h, even if it is properly declared in WSortView.cpp, the apparent cause of this is because there are 2 missing semicolons, one from the class declaration, and one from the END_EVENT_TABLE() above the constructor declaration. This change adds this 2 missing semicolons, making the constructor declaration to be finally detected.

3. DECLARE_EVENT_TABLE() apparently should not have a semicolon. Before, DECLARE_EVENT_TABLE() is not being detected because there is a semicolon after it, which is wrong. This change removes those semicolons so that DECLARE_EVENT_TABLE() is now finally detected.
@Unbreakable-Syntax Unbreakable-Syntax changed the title Removed compilation warnings. Introduce 10 new array input types, and 12 additional sorting algorithms. Removed code formatting and compilation warnings. Introduce 10 new array input types, and 12 additional sorting algorithms. Oct 11, 2024
Since decimal places are not considered for speed delays anymore in 1 ms and above, it is best not to show them anymore, as they could just lead to confusion.
Wrong placement
This should allow the visualizer to be more compatible now. With this change, the project should now compile under at least C++11
This should allow all higher Median of N implementations to perform at least a little better due to higher threshold (previous * 2), since clustering has even lesser chances of happening, especially the randomized variants.
Credits to @Taihennami on "The Studio" Discord for introducing me to this great minimum threshold formula, as well as introducing me to using Shell Sort for sorting n = 11. The minimum threshold for all higher Median of N is now (n * n), such as 5 * 5 = 25, or n^2, this should allow all higher Median of N pivot rules to perform faster on small array sizes due to having less overhead, as well as providing better pivot selection when the boundary is high enough.
This should be the last pivot rule I will ever implement. Assuming that I will implement Median of Medians properly, then I will add it too, but this is not going to be my main focus now. I suppose I will now aim towards adding more sorting algorithms.
Since watch() in this fork never expects a volatile variable anymore, and all Quick Sorts here use a separate watched std::atomic<ssize_t> variable, the typecasting fix provided earlier is of no use now.
I almost forgot to convert this sorting algorithm, my bad.
This should allow for more predictable behavior.
After some research, I am able to conclude that delay * 1000 isn't going to cause any overflow here in most cases, especially considering that a double can hold an even larger value than an int. But, in case microDelay somehow holds an infinite value, then the additional condition will ensure that microDelay will be set to the max value that a double can hold to ensure that proper comparison and functionality is still there.
After some research, I am able to conclude that delay * 1000 isn't going to cause any overflow here in most cases, especially considering that a double can hold an even larger value than an int. But, in case microDelay somehow holds an infinite value, then the additional condition will ensure that microDelay will be set to the max value that a double can hold to ensure that proper comparison and functionality is still there.
I am very much bothered with just the microsecond loop interval only. 
The reason why it's bothering me so much is that, say, someone allowed 60000 ms in the visualizer, 60000 * 1000 = 60000000, which means the microsecond loop would have to perform a total of 60 million evaluation and incrementation which isn't really good. A previous solution I had was to simply ignore the decimal part and use millisecond interval, so instead of 60 million, only 60000 checks would be performed, but this isn't very good as well since the visualizer fully interprets the decimal part of the ms value.

This struggle finally ends today, finally! The DoDelay() method now has 2 loops, one for the whole number value, and the other one for the fractional value. If the delay for example is set to 1.89 ms, if only the microsecond loop is used, this would result in 1890 iterations in total, with this hybrid approach, only 891 iterations (1 for millisecond, .89 * 1000 = 890 for microsecond) are done which is a good improvement! This slash in iterations becomes more noticeable the larger the ms value is. For example, with 14.89 ms, the resulting iterations with microsecond loop only would be 14890 iterations, with this hybrid approach, it only becomes 904 iterations (.89 * 1000 = 890 + 14) only!

Now I can finally rest easy.
The wxMilliSleep() converts a decimal value such as 0.1 to just 0, so in the else block, since wxMilliSleep receives a truncated delay, it effectively does the same thing.
Since the fractional ms value will never go past 1.0, the check condition is not needed now,
@Unbreakable-Syntax Unbreakable-Syntax changed the title Fixed code formatting, errors, and compilation warnings. Introduce 17 new array input types, and 23 additional sorting algorithms. Allowed larger array sizes to not go past the window borders of the program, among several other changes. Fixed code formatting, errors, and compilation warnings. Introduce new array input types, and additional sorting algorithms. Allowed larger array sizes to not go past the window borders of the program, among several other changes. Nov 12, 2024
@Unbreakable-Syntax Unbreakable-Syntax changed the title Fixed code formatting, errors, and compilation warnings. Introduce new array input types, and additional sorting algorithms. Allowed larger array sizes to not go past the window borders of the program, among several other changes. Fixed code formatting, errors, and compilation warnings. Introduce new array input types, and additional sorting algorithms. Larger array sizes are now properly displayed, among several other changes. Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant