C++ || “One Size Fits All” – BubbleSort Which Works For Integer, Float, & Char Arrays

Print Friendly, PDF & Email

Here is another sorting algorithm, which sorts data that is contained in an array. The difference between this method, and the previous methods of sorting data is that this method works with multiple data types, all in one function. So for example, if you wanted to sort an integer and a character array within the same program, the code demonstrated on this page has the ability to sort both data types, eliminating the need to make two separate sorting functions for the two different data types.


Integer Arrays
Character Arrays
Function Pointers - What Are They?

The highlighted lines are sections of interest to look out for.

Notice, the same function declaration is being used for all 3 different data types, with the only difference between each function call are the parameters which are being sent out.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: The function works for all three data types)

Original values in the int array:
44 91 43 22 20 100 77 80 84 60 47 91 51 81

The sorted values in the int array:
20 22 43 44 47 51 60 77 80 81 84 91 91 100
Original values in the float array:
49.5 30.5 67.5 50.5 29.5 89.5 78.5 80.5 54.5 7.5 54.5 38.5 56.5 70.5

The sorted values in the float array:
7.5 29.5 30.5 38.5 49.5 50.5 54.5 54.5 56.5 67.5 70.5 78.5 80.5 89.5
Original values in the char array:
This Is Random Text Brought To You By My Programming Notes

The sorted values in the char array:
Brought By Is My Notes Programming Random Text This To You

Was this article helpful?
👍 YesNo

Leave a Reply