### Introduction

Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms are better.

### Pseudo Code

The pseudo code of the bubble sort is given bellow.

procedure bubbleSort( A : list of sortable items )

repeat

swapped = false

for i = 1 to length(A) - 1 inclusive do:

if A[i-1] > A[i] then

swap( A[i-1], A[i] )

swapped = true

end if

end for

until not swapped

end procedure

### Complexity

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.

The only significant advantage that bubble sort has over most other implementations, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently built into the algorithm. Performance of bubble sort over an already-sorted list (best-case) is O(n). By contrast, most other algorithms, even those with better average-case complexity, perform their entire sorting process on the set and thus are more complex. However, not only does insertion sort have this mechanism too, but it also performs better on a list that is substantially sorted (having a small number of inversions).

### Simulation

**Simulation #1:**

**Video**

More online simulation

- http://www.cs.oswego.edu/~mohammad/classes/csc241/samples/sort/Sort2-E.html
- http://www.sorting-algorithms.com/bubble-sort

### Implementation

Implementations of the bubble sort is given bellow. For now we have only three supported language, in future we would try to incorporate more implementations.

#### Code Sample in “c”

Real implementation in c is given bellow.

#include<stdio.h>

#include<conio.h>

void bubble(int a[],int n)

{

int i,j,t;

for(i=n-2;i>=0;i--)

{

for(j=0;j<=i;j++)

{

if(a[j]>a[j+1])

{

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}

}

}//end for 1.

}//end function.

void main()

{

int a[100],n,i;

clrscr();

printf("\n\n Enter integer value for total no.s of elements to be sorted: ");

scanf("%d",&n);

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

{ printf("\n\n Enter integer value for element no.%d : ",i+1);

scanf("%d",&a[i]);

}

bubble(a,n);

printf("\n\n Finally sorted array is: ");

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

printf("%3d",a[i]);

} //end program.

#### Code Sample in “c++”

Bellow a simple implementation of bubble sort in c++ is given.

#include <stdio.h>

#include <iostream.h>

void bubbleSort(int *array,int length)//Bubble sort function

{

int i,j;

for(i=0;i<10;i++)

{

for(j=0;j<i;j++)

{

if(array[i]>array[j])

{

int temp=array[i]; //swap

array[i]=array[j];

array[j]=temp;

}

}

}

}

void printElements(int *array,int length) //print array elements

{

int i=0;

for(i=0;i<10;i++)

cout<<array[i]<<endl;

}

void main()

{

int a[]={9,6,5,23,2,6,2,7,1,8}; // array to sort

bubbleSort(a,10); //call to bubble sort

printElements(a,10); // print elements

}

#### Code Sample in “c#”

The c# implementation is given bellow.

namespace AllAlgorithm

{

public class BubbleSort

{

private readonly int[] _samples = new int[100];

private int x;

public void SortArray()

{

int i;

int j;

for (i = (x - 1); i >= 0; i--)

{

for (j = 1; j <= i; j++)

{

if (_samples[j - 1] > _samples[j])

{

int temp = _samples[j - 1];

_samples[j - 1] = _samples[j];

_samples[j] = temp;

}

}

}

}

}

}

## No comments:

## Post a Comment