Skip to content

shaanbanday/Bubble-Sort-Demonstration

Repository files navigation

Bubble-Sort-Demonstration

A graphical representation of how bubble sort works, coded in Java.

This program serves as a visual teaching aid to demonstrate how the Bubble Sort Algorithm works. The following class does not have a main method, but it constructs all the graphical elements that the SortingDemonstrationMain class calls to create the GUI. The constructor itself (this first method) is spilt into many constructors since this class is noticeably big and has many variables it needs to initialize. This class also implements an ActionListener method and a KeyListener method. To draw certain things, like the prompt to take the inputs and the state of the swapped variable, the class interacts with the paint method, which makes it easier to update often, compared to just using JLabels which are always the same text throughout the program's run. This class takes care of taking the input of 10 integers with a maximum of three digits (negative numbers are allowed) from the user to then put them in ascending order. To store the values for those numbers and keep track of their position on the screen, the Object-Oriented Programming principle of encapsulation is implemented by using a separate Element class. It becomes particularly useful during the sorting process to know when the numbers should stop moving. At the beginning of the program, music is played, instructions are given, and upon pressing the bubble sort button, users can then enter their desired integers. The order of the array before sorting is simply the order the numbers are entered in. During the sorting, the numbers are moved around, and the largest number is “bubbled” to the top, hence the name bubble sort. To help with understanding the algorithm, a message is displayed which says that the variable sorted must be false for 8 comparisons in a row for everything to be sorted.

About

A graphical representation of how bubble sort works, coded in Java.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages