Bubble Sort C++ Code Examples
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
Here are some C++ code examples for implementing Bubble Sort:
1. Basic Bubble Sort Implementation
#include
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array: \n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Explanation:
-
Function
bubbleSort(int arr[], int n)
:- Takes an integer array
arr
and its sizen
as input. - Uses nested loops to iterate through the array:
- Outer loop runs from
i = 0
ton - 1
, controlling the number of passes. - Inner loop runs from
j = 0
ton - i - 1
, comparing adjacent elements.
- Outer loop runs from
- If
arr[j]
is greater thanarr[j + 1]
, they are swapped using a temporary variabletemp
.
- Takes an integer array
-
Main function:
- Creates an unsorted integer array
arr
. - Calls the
bubbleSort
function to sort the array. - Prints the sorted array.
- Creates an unsorted integer array
2. Bubble Sort with Optimized Swaps
This version optimizes the basic implementation by keeping track of whether any swaps were made during a pass. If no swaps are made, the array is already sorted, and we can stop the sorting process.
#include
using namespace std;
void bubbleSort(int arr[], int n) {
bool swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array: \n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Explanation:
swapped
variable: Introduced to track whether any swaps occurred during a pass.- Optimization: If
swapped
remainsfalse
after a pass, it means the array is already sorted, and the loop breaks.
3. Bubble Sort using Function Objects
This example demonstrates the use of function objects (functors) to define the comparison logic for sorting.
#include
#include
using namespace std;
template
void bubbleSort(T arr[], int n, function compare) {
bool swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (compare(arr[j], arr[j + 1])) {
// Swap arr[j] and arr[j+1]
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
// Sort in ascending order
bubbleSort(arr, n, less());
cout << "Sorted array (ascending): \n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Sort in descending order
bubbleSort(arr, n, greater());
cout << "Sorted array (descending): \n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Explanation:
function<bool(T, T)> compare
: This parameter takes a function object that defines the comparison logic.less<int>()
: A standard function object for ascending order comparison.greater<int>()
: A standard function object for descending order comparison.
These examples demonstrate different ways to implement Bubble Sort in C++. The choice of implementation depends on your specific needs and the level of flexibility required. While Bubble Sort is simple to understand, it is generally inefficient for larger datasets and is not the most commonly used sorting algorithm in practice.