A comparison-based sorting algorithm called Bubble Sort iteratively traverses the list to be sorted, compares nearby members, and swaps them if they are out of order. Up till the complete list is sorted, this procedure is repeated. Smaller elements "bubble" to the top of the list with each iteration, giving the algorithm its name.
- We start by defining the bubble Sort function, which has two input parameters: an integer array arr and its size n .
- The array is iterated by using nested for loops . The inner loop compares and switches nearby elements while the outer loop regulates the number of passes.
- We utilize an if statement inside the inner loop to determine whether the current element is greater than the following element. If so, a temporary variable called temp is used to swap them.
- We declare an array named arr and its size n in the main function. Unsorted integers are contained in the array.
- Using a for loop , we print the initial array.
- After that, the array is sorted by calling the bubble Sort method .
Example:
Let's examine the step-by-step process of implementing Bubble Sort in the C programming language. Presented below is the code:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i< n - 1; i++) {
for (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]);
printf("Original array: ");
for (int i = 0; i< n; i++) {
printf("%d ", arr[i]);
}
bubbleSort(arr, n);
printf("\nSorted array: ");
for (int i = 0; i< n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
Conclusion:
In summary, Bubble Sort is a simple and easy-to-understand sorting technique that can serve as a foundational step in exploring more advanced sorting algorithms. While it may not be the most efficient choice for sorting large datasets, its straightforward nature and logical process have made it a commonly chosen approach in educational settings.
This article delves into a comprehensive examination of how Bubble Sort operates, its implementation in the C programming language, syntax details, and illustrated output instances. By studying this blog post, you will grasp the fundamental concepts of Bubble Sort and gain hands-on familiarity with it through a systematic implementation guide and code analysis.
It is crucial to keep in mind that highly efficient sorting methods such as Quick Sort, Merge Sort, or Heap Sort are commonly preferred in practical scenarios, particularly when dealing with extensive datasets. These sorting algorithms are specifically designed to manage significant volumes of data more adeptly and provide better time complexities.
Comprehending Bubble Sort offers essential perspectives on the mechanics of comparisons and swapping elements in the sorting procedure, laying the foundation for grasping additional sorting algorithms. This knowledge forms the basis for enhancing your comprehension of computer science and various sorting techniques.