SortViz — learn sorting algorithms by visualization
year
2021
SortViz is a sorting visualization application that showcases eight sorting algorithms, including lesser-known ones such as Radix and Gnome, in a visually intuitive manner. The application allows users to modify the sorting speed, the number of values being sorted, and the option to enable step-by-step sorting. One of its convenient features is the ability to adjust the sorting speed during the process itself.
SortViz is built using Java GUI libraries such as JavaFX and Swing, providing a robust graphical interface for users. The user interface was created using Scene Builder, a popular visual layout tool that simplifies the process of designing high-quality user interfaces.
SortViz utilizes a multi-threaded architecture, executing each sorting algorithm on a separate thread, which enables simultaneous execution of two algorithms and enhances the application's responsiveness. This feature also allows users to modify the state of the sorting process, making it easier to visualize how the algorithm works. To ensure the application's security, SortViz utilizes SQLite to store user credentials, ensuring user data is kept safe and secure.
During my final year of high school, I took an introductory course in computer science where we learned about different sorting algorithms and how to use them. As I progressed through the course, I noticed that some students were struggling to understand the concepts due to the abstract nature of the algorithms and the lack of any interactive visual aid. It was then that I had an idea: what if there was a tool that could help students learn about sorting algorithms in a more intuitive and engaging way?
This inspired me to develop a sorting visualization project as my final project for the course. I wanted to give students a hands-on way to explore the algorithms and understand their mechanics in a more tangible way. I named the project SortViz and began working on it in my spare time. The feedback from my teacher and classmates was overwhelmingly positive, and I felt proud of what I had accomplished. But the real validation came when my computer science teacher asked if he could use SortViz as a teaching aid in his classes. Knowing that my creation was helping others learn was an incredibly rewarding feeling.
Challenge
Responsiveness and and thread management
One of the main challenges I faced when developing SortViz was managing the order of execution for the threads. When using multithreading, it is difficult to ensure that the threads are executing in the correct order and that the user interface remains responsive with the sorting algorithm's progress.
To solve this challenge, I implemented a message-passing architecture between the threads. Rather than relying on shared memory or synchronization primitives, I used a message queue to communicate between threads. For example, when the user interacted with the SortViz application to change the sorting speed or to pause or resume sorting, the user interface thread would send a message to the sorting thread to update its behavior. Similarly, when the sorting thread completed a step in the sorting algorithm, it would send a message to the user interface thread to update the visualization.
In SortViz, I used a LinkedBlockingQueue to pass messages between the user interface thread and the sorting threads. The LinkedBlockingQueue class is a FIFO (first-in, first-out) data structure that allows for efficient adding and removing of elements. When one thread adds an element to the queue, another thread can safely remove it from the queue without the risk of concurrency issues like race conditions or deadlocks. This allows for safe and efficient communication between threads.
Using a message-passing architecture helped me to mitigate the complexity of managing multiple threads and made it easier to develop a high-quality and efficient sorting visualization application like SortViz. By overcoming this challenge, I was able to create a program that accurately and efficiently demonstrated different sorting algorithms visually and allowed users to modify the speed of sorting and the number of values being sorted.
Future ideas & what’s next
One future idea for SortViz is to add more sorting algorithms to the tool. While the current 8 algorithms are incredibly helpful, there are many more algorithms that could be added to make the tool even more comprehensive and educational. By introducing more complex and less well-known algorithms, SortViz could help students and developers better understand the different ways that data can be sorted.
One other possibility for SortViz is the introduction of real-time sorting. Currently, the tool allows users to modify the speed of sorting and the number of values being sorted. However, by introducing an option for real-time sorting, users could input values on-the-fly and watch as the sorting algorithm continuously sorts them. This would make the tool even more interactive and engaging.
Also, one more potential improvement for SortViz is the addition of more data structures. In addition to sorting algorithms, there are many other interesting data structures that could be implemented visually using SortViz. For example, trees, stacks, and queues could be added to the tool to make it even more versatile and educational.