DATA STRUCTURES AND ALGORITHM – Best Sorting Algorithms – Bubble sort Algorithm Bubble sort in C

DATA STRUCTURES AND ALGORITHM – Best Sorting Algorithms – Bubble sort Algorithm Bubble sort in C

Bubble Sort Algorithm in C – Explanation & Code

Bubble Sort is one of the simplest sorting algorithms. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until the list is sorted.

 How Bubble Sort Works?

  1. Compare adjacent elements.
  2. Swap them if they are in the wrong order.
  3. Move to the next pair and repeat.
  4. Continue this process for every element.
  5. Repeat the entire process for multiple passes until the array is sorted.

Best Case (Already Sorted): O(n)
Worst Case (Reverse Sorted): O(n²)
Average Case: O(n²)
Stable Sort: Yes
In-Place Algorithm: Yes

 Bubble Sort Algorithm in C

#include <stdio.h>

// Function to perform Bubble Sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
int swapped;

for (i = 0; i < n - 1; i++) {
swapped = 0; // Flag to check if swapping happened

for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) { // Swap if the current element is greater than the next
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1;
}
}

// If no two elements were swapped, break the loop (Optimization)
if (swapped == 0)
break;
}
}

// Function to print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Main function
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");
printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array: ");
printArray(arr, n);

return 0;
}

Explanation of Code

  1. Outer Loop: Runs for (n-1) passes to move the largest element to the end.
  2. Inner Loop: Compares adjacent elements and swaps them if they are out of order.
  3. Optimization: If no swaps occur in a pass, the array is already sorted, and the loop breaks.
  4. Function Usage:
    • bubbleSort() performs sorting.
    • printArray() prints the array before and after sorting.

 Example Execution

Input:

int arr[] = {64, 25, 12, 22, 11};

Sorting Process:

Pass 1: [25, 12, 22, 11, 64]
Pass 2: [12, 22, 11, 25, 64]
Pass 3: [12, 11, 22, 25, 64]
Pass 4: [11, 12, 22, 25, 64]

Output:

Original array: 64 25 12 22 11
Sorted array: 11 12 22 25 64

 Advantages & Disadvantages

Advantages:

  • Simple and easy to implement.
  • Works well for small datasets.
  • Stable sorting algorithm.

Disadvantages:

  • Inefficient for large datasets.
  • Time complexity O(n²) makes it slow compared to other sorting algorithms.

 Alternative Sorting Algorithms

  • Selection Sort – More efficient but still O(n²).
  • Insertion Sort – Efficient for nearly sorted arrays.
  • Merge Sort & Quick Sort – More efficient, O(n log n).

Would you like an optimized sorting algorithm like Quick Sort or Merge Sort in C?

DATA STRUCTURES AND ALGORITHM – Best Sorting Algorithms – Bubble sort Algorithm Bubble sort in C

V: Sorting: Bubble sort, Merge sort, Insertion Sort, Selection …

Data Structures & Algorithms Bubble Sort

Leave a Reply

Your email address will not be published. Required fields are marked *

error: