Bubble Sort
Bubble Sort
Bubble Sort is one of the simplest sorting algorithms that work by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
What is Bubble Sort?
Bubble Sort is a straightforward algorithm that compares each pair of adjacent elements and swaps them if they are in the wrong order. This process is repeated until the array is sorted.
Example:
Unsorted List: [5, 3, 8, 4, 2]
Sorted List: [2, 3, 4, 5, 8]
Solutions:
Now let’s see how to implement Bubble Sort in different programming languages: Java, Python, C, and C++.
1. Java
import java.util.Arrays;
public class BubbleSort {
// Function to implement Bubble Sort
public static void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
// Swap if the element is greater than the next element
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped, the array is sorted
if (!swapped) break;
}
}
public static void main(String[] args) {
int[] arr = {5, 3, 8, 4, 2};
System.out.println("Unsorted array: " + Arrays.toString(arr));
bubbleSort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}
2. Python
# Function to implement Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
swapped = False
for j in range(n - i - 1):
# Swap if the element is greater than the next element
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
# If no elements were swapped, the array is sorted
if not swapped:
break
# Example usage
arr = [5, 3, 8, 4, 2]
print("Unsorted array:", arr)
bubble_sort(arr)
print("Sorted array:", arr)
3. C++
#include <iostream>
using namespace std;
// Function to implement Bubble Sort
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++) {
// Swap if the element is greater than the next element
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped, the array is sorted
if (!swapped) break;
}
}
int main() {
int arr[] = {5, 3, 8, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Unsorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
bubbleSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
4. C
#include <stdio.h>
// Function to implement Bubble Sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
int swapped;
for (i = 0; i < n - 1; i++) {
swapped = 0;
for (j = 0; j < n - i - 1; j++) {
// Swap if the element is greater than the next element
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1;
}
}
// If no elements were swapped, the array is sorted
if (!swapped) break;
}
}
int main() {
int arr[] = {5, 3, 8, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int i;
printf("Unsorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
bubbleSort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Explanation of Code:
Each code version defines a bubbleSort
function to sort an array using the Bubble Sort algorithm.
Inner Loop: Compares each pair of adjacent elements and swaps them if they are in the wrong order.
Outer Loop: Repeats the process for each element in the array.
Optimization: If no elements are swapped in a pass, the array is already sorted, and the loop breaks early.
Sample Input & Output:
Input:
Unsorted array: [5, 3, 8, 4, 2]
Output:
Sorted array: [2, 3, 4, 5, 8]
Conclusion:
Bubble Sort is a simple and intuitive sorting algorithm. Although it is not the most efficient for large datasets, it is an excellent choice for educational purposes and small datasets.
** Please do subscribe my blog for future updates and share with your friends, if you find this informative **
Comments
Post a Comment