DS – Bubble Sort Algorithm

Bubble Sort:

  • It is a simple sorting technique
  • In this algorithms, index element compare with next element and swapping them if required.
  • For each pass, highest element bubbled to last location.
  • This process continues until all elements get sorted.
#include<stdio.h>
void BubbleSort(int[], int);
int main()
{
            int arr[10],i;
            for(i=0 ; i<10 ; i++)
            {
                        arr[i] = rand()%50;
            }
           
            printf(“Before sort : \n”);
            for(i=0 ; i<10 ; i++)
                        printf(“%d  “, arr[i]);
           
            BubbleSort(arr,10);
           
            printf(“\nAfter sort : \n”);
            for(i=0 ; i<10 ; i++)
                        printf(“%d  “, arr[i]);
                       
            return 0;
}
void BubbleSort(int sort[],int n)
{
            int i,j,t;
            for(i=0 ; i<n-1 ; i++)
            {
                        for(j=0 ; j<n-1-i ; j++)
                        {
                                    if(sort[j]>sort[j+1])
                                    {
                                                t = sort[j];
                                                sort[j] = sort[j+1];
                                                sort[j+1] = t;
                                    }
                        }
            }
}
Scroll to Top