How to Write a Bubble Sort C++

From many of the diverse concepts in C++, Sorting is a well-known one. Sorting has come up with many types. One of its well-known types is a Bubble Sort. The bubble sort algorithm is quite simple and well-known for doing comparison-based sorting within the elements of an array or a data structure. The swapping method will be applied to the head-to-head indexes of an array after comparing both. The bubble sort is quite easy to do but not reliable for a large set of data as it takes a lot of time. Therefore, we will be implementing the Bubble sort in C++ via the Ubuntu 20.04 system. So, let’s get started.

Open the console application of the Ubuntu 20.04 system first with Ctrl+Alt+T. After opening it, we have to generate a new “c++” file named “bubble.cc” using the simple “touch” command of the shell terminal. It would create your C++ file in your Linux home file folder. To implement bubble sort, open the generated file from the file explorer in some editor, i.e., text editor. It can also be opened in the terminal within the nano editor. Both the commands are already shown in the given image.

Example 01:

Let’s have a first example to demonstrate the working of bubble sort in C++. We have started this code of C++ with the “iostream” header file. It has been included with the keyword “#include”. After that, a namespace, i.e., “standard,” must be used in the code before any function. We have defined a main() function of the integer return type. Within the main() function, We have defined an array “A” of size 50 and a variable “temp” to do swapping. The cout statement is used here to tell a user that we have to add some elements in an array. The “for” loop has been initialized to iterate the array “A” from index 0 to 9 to input the values in the array with the “cin” statement. One outer and one inner loop has been used.

The outer “for” loop has been initialized from 1 to 9 to iterate the inner loop fully. The inner loop has been used to iterate until the comparison has been performed with swapping. The “if” statement has been used to compare the first index value with the value next to the first index of an array “A”. When the first index value is larger than the second index value, it will perform the swapping within the “if” statement. The second index value will be swapped with the first index value. This process will continue to do so until the end of a loop and the last index of an array. When the value at the first index is smaller than the value at the next index, it will not perform swapping, and the next iteration will be performed. The new variable “temp” will be replaced with the value at the first index. While the first index will be replaced with the next consecutive index value of the array. The value of the “temp” variable will be saved to the second index of an array.

The cout statement is again used to show that the array has been sorted. The already sorted array with bubble sort will be iterated using the “for” loop up to the last index of an array. The next cout statement has been utilized to display the array values in a sorted way. The main() function closes here, and the program ends. Now, it’s time to save your bubble sort code with the “Ctrl+S” shortcut. After this, we have to close this bubble.cc file and return to the shell terminal with the “Ctrl+X” shortcut.

As we have returned to the terminal shell, it’s time to compile the bubble sort file with the c++ compiler. We have to utilize the “g++” built-in compiler that has been installed with the “apt” package. The filename has been used with the “g++” compiler to compile the bubble sort code quickly. As the compilation result returns nothing, it means the bubble sort code is syntactically correct and got no errors. Now, we have to run this compiled file with the “./a.out” command followed by the key “Enter”. The input has been asked from the user, i.e., add numbers in an integer array “A” up to 10 words in a random unsorted way. As a result, the program and sorted the array with bubble sort and returned the sorted array as shown below.

Example 02:

After opening the file, we have included an “input-output” stream header file at the top. The standard namespace must be used hereafter the stream file. The user-defined function “Swap” has been defined with two integer pointer type variables, “x” and “y”. The integer type variable “temp” has been defined to get the values from the other function variable “x”. The variable pointer “y” values have been saved to the variable “x”, and “y” has been replaced with the variable “temp” value. The swapping of values has been done.

After the “swap” function, the user-defined “show” function has been implemented to display the array before or after the sort, having two integer type parameters. The first one is the pointer Array, and the other is the size of an array. Within this function, we have initialized a “for” loop to iterate the array “A” up to the size “s” passed by the main() function. The cout statement displays each value at a unique index of an array “A”. Now the function has been ended.

Here comes the original function “Sort” to perform the bubble sort technique on the array “A”. The function takes pointer integer Array and size “s” as an argument. Within this function, we have utilized the inner and outer “for” loops. Within the outer “for” loop, the “swaps” variable has been initialized to 0. Within the inner “for” loop, we have been comparing the current variable to the next consecutive value of an array. If the condition succeeds, we will call the “Swap” function to perform swapping of two consecutive values of an array, and the integer “swaps” will be set to 1. If the “swaps” are not found here, it means the array is sorted.

The main() function is started with the declaration of array “A” of size 12. The “for” loop has been initialized to input the values in an array with the help of a “cin” statement. The sort() function has been called to sort the array with bubble sort, then a show() function is called to display the sorted array on a shell.

The execution shows that the user entered random values into the array, and the sorted array has been displayed below.


So, we have discussed the C++ bubble sort with some examples to sort an array data structure that is randomly defined or initialized. This has been done by swapping and comparing values. The inner and outer “for” loops have also been utilized here for swapping and comparison purposes. All the above C++ examples are quite understandable and easy to implement.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.