This project provides a pretty simple and detailed explanation of Bubble sort algorithm.
So, if you were looking for information on this topic, but other sources did not give you a complete understanding, then I hope this project will help you.
- An implementation of the algorithm is given in the file BubbleArray.java
- Мethod "bubbleSorter" performs sorting of the array by the Bubble sort algorithm. The detailed explanation of this method realization is given below.
- A demonstration of the algorithm work is given in the file BubbleSortDemo.java
Here are the key ideas of the method bubbleSorter realization according to the example below. Look at the illustrations and read the explanation.
-
In the example on the pictures we have array with 6 elements {15, 8, 23, 16, 42, 4}.
-
Algorithm makes iterations in outer loop and inner loop. For each iteration in outer loop the algorithm iterates from the first element to the last in inner loop. The last element is bounded by the “array out border”, that moved by outer loop after its each iteration.
-
At the beginning, in inner loop algorithm iterates for 5 times comparing each element with the next element and swapping them if the next element is less.
-
After each iteration in outer loop, at least one element moves at the end, where it should be.
-
Therefore, step by step each element comes on its place.