Bubble sort in C C++

Bubble sort is one of the comparison based sorting algorithm.

Important Points

  • Traversing through the array and swapping adjacent elements.
  • Time complexity : – O(n^2).
  • At each iteration largest/greatest element is positioned at the end of the array.
  • Why name? Heavier elements settle at the end/bottom leading to light elements bubble up to the top at each iteration.
  • Adaptive
  • Stable

C Program for Bubble Sort

#include<stdio.h>
#include<stdlib.h>

void swap(int *p, int *q)
{
    int temp;
    temp = *p;
    *p = *q;
    *q = temp;
}

void BubbleSort(int A[],int n)
{
    int i,j,flag;
    for (i=0;i<n-1;i++)
    {
        flag = 0;
        for(j=0;j<n-i-1;j++)
        {
            if(A[j]>A[j+1])
            {
                swap(&A[j],&A[j+1]);
                flag = 1;
            }
        }
        if(flag==0) break;
    }
}

int main()
{
    int A[10] = {1,5,2,1,6,3,6,2,5,10};
    int i;
    BubbleSort(A,10);
    for(i=0;i<10;i++)
    {
        printf("%d ",A[i]);
    }
    return 0;
}

Other sorting algorithms

Count sort in C/C++

Merge sort in C/C++

Quick Sort in C/C++

Selection sort in C/C++

Insertion Sort in C/C++

Bubble Sort in C/C++

Criteria for sorting analysis

LEAVE A REPLY