Bubble Sort Algorithm

Yasir
2 min readMay 8, 2020

Line by Line Bubble Sort Algorithm Explained in C++

Bubble sort is the simplest sorting algorithm. It works by iterating the input array from the first element to the last, comparing each pair of elements and swapping them if needed. Bubble sort continues its iterations until no more swaps are needed.

Line 16 :- Here is the main function , when we execute the program , program starts from main function .

Now we are declaring an array named as myarray which has sized “5" . And then we are asking to user to enter the 5 elements for the array through for loop 5 times . Then we print the array as it is through for loop . [When can ask to user for the size of array also (dynamic memory allocation)]

Line 27 :- Here we are calling a function named as bubbleSort where the main sorting algorithm is performed , and also passing the array myarray .

Line 4 :- Here we will perform sorting algorithm . In parameter we are taking array from main function .

Line 5 :-Here we are iterate through arrays element and in second for loop we are comparing one by one elements .

Line 7 :- Now we are performing swapping by comparing elements .

After compiling the bubbleSort function , program will go back to main() function to Line 29 .

Line 29 :- Here we are printing the elements of array after sorting .

This was whole line by line explanation for Bubble Sort in C++ .

--

--

Yasir

Exploring the Swift, SwiftUI, and Apple universe.