C Program:Bubble Sort



Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items.

PROGRAM:

//Bubble Sort by Vikas Konaparthi
#include <stdio.h>
void bubble(int a[10],int size);
void ReadArray(int a[10],int size);
void WriteArray(int a[10],int size);

void ReadArray(int a[10],int size)
{
    int i;
    printf("\n Enter %d elements",size);
    for(i=0;i<size;i++)
    scanf("%d",&a[i]);
}
void WriteArray(int a[10],int size)
{
    int i;
    printf("\n Sorted Array\n");
    for(i=0;i<size;i++)
    {
        printf("%d\t",a[i]);
    } 
}
void bubble(int a[10],int size)
{
  int temp,i,j;
  for(i=0;i<size;i++)
  {
   for(j=0;j<size-1;j++)
   {
    if(a[j]>a[j+1])
    {
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
    }
  }
 }
}   
void main()
{
    int a[10],size;
    printf("\nEnter the size of array");
    scanf("%d",&size);
    ReadArray(a,size);
    bubble(a,size);
    WriteArray(a,size);


Previous Post Next Post