Bubble Sort Algorithm Details
Bubble Sort Algorithm Details
1. Introduction:
Bubble Sort is one of the simplest sorting algorithms used in computer science. It works by
repeatedly swapping the adjacent elements if they are in the wrong order. The algorithm gets its
name because smaller elements "bubble" to the top of the list with each iteration. Despite its
simplicity, Bubble Sort is not suitable for large data sets as its average and worst-case time
2. Problem Statement:
Given an array of n integers, the objective is to sort the array in ascending order using the Bubble
Sort technique.
3. Technique:
Bubble Sort compares each pair of adjacent items and swaps them if they are in the wrong order.
This process is repeated for every element in the array until no swaps are needed, indicating the
array is sorted.
4. Steps:
- Repeat the process for all elements until the array is sorted.
5. Algorithm:
BUBBLE_SORT(arr, n)
for i from 0 to n-1
6. Implementation in C:
#include <stdio.h>
*xp = *yp;
*yp = temp;
swap(&arr[j], &arr[j+1]);
printf("\n");
int main() {
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
7. Implementation in Python:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
bubble_sort(arr)
8. Implementation in Java:
public class BubbleSort {
int n = arr.length;
arr[j] = arr[j+1];
arr[j+1] = temp;
ob.bubbleSort(arr);
System.out.println("Sorted array:");
9. Expected Output:
Sorted array: 11 12 22 25 34 64 90
10. Advantages:
- Simple to implement.
11. Disadvantages:
- Can be used when the dataset is nearly sorted, and the cost of writing another algorithm is high.
13. Conclusion:
Bubble Sort is a straightforward algorithm that is ideal for educational purposes. Though it is rarely
used in practical applications due to its quadratic time complexity, it helps in understanding the basic
concept of sorting and algorithm design. For small or nearly sorted datasets, Bubble Sort can be
acceptable. However, for large datasets, more efficient algorithms like Quick Sort or Merge Sort
should be used.