Your first Algorithm ;(
The Bubble Sort algorithm sorts an array of items in increasing order. It steps through the array and compares the adjacent elements. If they are not sorted, it swaps them. It then keeps repeating the procedure until the array is fully sorted. Let’s jump into an example.
The following elements are compared during the first iteration: 0 and 1, 1 and 2, 2 and 3, 3 and 4, and 4 and 5. In the first comparison of the first iteration, elements at indices 0 and 1 are compared. Since 3 is less than 7, the elements remain in their current positions.
Next, elements at indices 1 and 2 are compared. Since 7 is larger than 1, the elements are swapped.
Next, elements at indices 2 and 3 are compared. Since 7 is larger than 4, the elements are swapped.
Moving to elements 3 and 4, the algorithm compares the values 7 and 6. Since 7 is larger than 6, the values are swapped.
Lastly, elements at indices 4 and 5 are compared. Since 7 is less than 5, the elements are swapped.
This completes the first iteration. We know that the last element is fully sorted. In the next iteration the last element will not be compared. So, in iteration two the following comparisons will occur: 0 and 1, 1 and 2, 2 and 3, and 3 and 4.
The Bubble Sort algorithm begins the second iteration. Values at indices 0 and 1 are compared. Since 3 is greater than 1, the elements are swapped.
Next, the algorithm compares the values 3 and 4. Since 3 is less than 4, the values remain in their current positions.
Values at indices 2 and 3 are compared. Since 4 is less than 6, the values are not swapped.
The Bubble Sort algorithm compares the values at indices 3 and 4. Since 6 is greater than 5, the elements are swapped.
By inspecting the array visually, we can quickly see that the array is sorted, but the Bubble Sort algorithm keeps going since it doesn’t know that it’s finished. It starts the third iteration. Since 1 is less than 3, the values are not swapped.
Next, 3 and 4 are compared. Since 3 is less than 4, the values are not swapped.
Finally, values 4 and 5 are compared. Since 4 is less than 5, the values remain in their current positions. This finishes the third iteration. The Bubble Sort algorithm guarantees that it has found the last 3 numbers in the sequence.
Bubble Sort starts the fourth iteration. Since 1 is less than 3, the values are not swapped.
Next, the algorithm compares values at indices 1 and 2. Since 3 is less than 4, the elements remain in their current positions. This ends the fourth iteration. The algorithm guarantees that the last 4 digits have been sorted. One more iteration to go.
The Bubble Sort algorithm starts working on the fifth iteration. It has only one comparison: the comparison between values 1 and 3. Since 1 is less than 3, the values remain in their current positions and the algorithm finishes the fifth iteration.
There are no further comparisons to be performed. The Bubble Sort algorithm has sorted the array in n-1 iterations, which in this case equals 5.
The following example was deliberately chosen to show that even though the Bubble Sort algorithm completed the sort in 2 iterations, n-1 iterations were still needed to finish the execution of the algorithm. It can be deduced that the Bubble Sort algorithm is not efficient for large data sets; it always runs in O(n2).
Algorithm Tutorial Series
Continue your Algorithm Learning.
Your first Algorithm ;(
BUBBLE SORT ALGORITHM VISUALLY EXPLAINED
The Bubble Sort algorithm sorts an array of items in increasing order. It steps through the array and compares the adjacent elements. Let’s look at it visually.
Coming Soon
Insertion Sort Algorithm Visually Explained
The insertion sort starts with the value at index 1 and compares it to the value at index 0. If the value at index 1 is less than the value at index 0, the values are swapped. The index is incremented, and the procedure is repeated.