Bubble sort is a simple and intuitive sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It derives its name from the way smaller elements “bubble” to the top of the list with each iteration. Despite its simplicity, bubble sort is generally inefficient on large lists and is mainly used for educational purposes or for sorting small datasets.
In this introduction, we’ll explore the implementation of bubble sort using pointers in C++. Pointers are variables that store memory addresses, allowing direct manipulation of memory locations. Utilizing pointers in sorting algorithms can sometimes lead to more efficient code and can deepen understanding of memory management.
Example Program: Bubble Sort Using Pointers in C++
#include <iostream>
using namespace std;
// Function to perform bubble sort using pointers
void bubbleSort(int *arr, int size) {
bool swapped;
for (int i = 0; i < size - 1; i++) {
swapped = false;
for (int j = 0; j < size - i - 1; j++) {
if (*(arr + j) > *(arr + j + 1)) {
// Swapping using pointers
int temp = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = temp;
swapped = true;
}
}
// If no two elements were swapped in the inner loop, the array is sorted
if (!swapped)
break;
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
bubbleSort(arr, size);
cout << "\nSorted array: ";
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
return 0;
}
Explanation of Example Program:
- The program begins by including the necessary header files and defining the namespace.
- The
bubbleSort
function implements the bubble sort algorithm using pointers. It takes an integer pointerarr
and the size of the arraysize
as parameters. - Inside the
bubbleSort
function, a boolean variableswapped
is initialized to keep track of whether any elements were swapped during an iteration. - Two nested loops iterate over the array elements. The outer loop runs from
0
tosize - 1
, and the inner loop runs from0
tosize - i - 1
. This ensures that in each iteration, the largest element bubbles to its correct position at the end of the array. - Within the inner loop, elements are compared using pointers (
*(arr + j)
and*(arr + j + 1)
). If the current element is greater than the next element, they are swapped using pointer arithmetic. - If no elements were swapped in the inner loop, it means the array is already sorted, and the outer loop can be terminated early using the
break
statement. - In the
main
function, an arrayarr
is defined, and its size is calculated. - The original array is displayed using a loop.
- The
bubbleSort
function is called to sort the array. - Finally, the sorted array is displayed using another loop.
Conclusion:
Bubble sort is a fundamental sorting algorithm that provides a clear illustration of basic sorting concepts. Implementing it using pointers in C++ offers insight into memory management and pointer manipulation. While bubble sort is not suitable for large datasets due to its inefficiency, understanding its principles can pave the way for grasping more complex sorting algorithms and data structures.