0% found this document useful (0 votes)
2 views5 pages

Bubble Sort Algorithm Details

Bubble Sort is a simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order, making smaller elements 'bubble' to the top. While easy to implement and understand, it has a time complexity of O(n^2), making it inefficient for large datasets. It is primarily used for educational purposes and is suitable for small or nearly sorted arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Bubble Sort Algorithm Details

Bubble Sort is a simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order, making smaller elements 'bubble' to the top. While easy to implement and understand, it has a time complexity of O(n^2), making it inefficient for large datasets. It is primarily used for educational purposes and is suitable for small or nearly sorted arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BUBBLE SORT - Analysis and Design of Algorithms

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

complexity is quite high.

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:

- Compare adjacent elements.

- Swap them if they are in the wrong order.

- Continue this process for the entire array.

- Repeat the process for all elements until the array is sorted.

5. Algorithm:

BUBBLE_SORT(arr, n)
for i from 0 to n-1

for j from 0 to n-i-2

if arr[j] > arr[j+1]

swap arr[j] and arr[j+1]

6. Implementation in C:

#include <stdio.h>

void swap(int *xp, int *yp) {

int temp = *xp;

*xp = *yp;

*yp = temp;

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n-1; i++) {

for (int j = 0; j < n-i-1; j++) {

if (arr[j] > arr[j+1]) {

swap(&arr[j], &arr[j+1]);

void printArray(int arr[], int size) {

for (int i=0; i < size; i++)


printf("%d ", arr[i]);

printf("\n");

int main() {

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

int n = sizeof(arr)/sizeof(arr[0]);

bubbleSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

7. Implementation in Python:

def bubble_sort(arr):

n = len(arr)

for i in range(n):

for j in range(0, n-i-1):

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

arr = [64, 34, 25, 12, 22, 11, 90]

bubble_sort(arr)

print("Sorted array is:", arr)

8. Implementation in Java:
public class BubbleSort {

void bubbleSort(int arr[]) {

int n = arr.length;

for (int i = 0; i < n-1; i++)

for (int j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1]) {

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

public static void main(String args[]) {

BubbleSort ob = new BubbleSort();

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

ob.bubbleSort(arr);

System.out.println("Sorted array:");

for (int i = 0; i < arr.length; i++)

System.out.print(arr[i] + " ");

9. Expected Output:

Sorted array: 11 12 22 25 34 64 90

10. Advantages:
- Simple to implement.

- Does not require additional memory (in-place sort).

- Easy to understand for beginners.

11. Disadvantages:

- Very inefficient for large datasets.

- Time complexity is O(n^2) in the average and worst cases.

- Not suitable for applications where performance is critical.

12. Real-World Applications:

- Used in educational purposes and as a benchmark for learning sorting algorithms.

- Rarely used in production environments due to inefficiency.

- 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy