What is Bubble Sort ? | Program for Bubble Sort in C

bubble sort

Bubble sort, sometimes referred to as sinking sort, Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. 

Example: First Pass: ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.

/*
Write a program in C for Bubble sort
*/ 


#include<stdio.h>

int main()
{
 int arr[10],i,j,t;
 printf("Please enter 10 values:\n");
 for(i=0;i<10;i++)
  scanf("%d",&arr[i]);

 for(i=0;i<9;i++)
 {
  for(j=0;j<9-i;j++)
  {
   if (arr[j]>arr[j+1])
   {
    t=arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=t;
   }
  }
 }

 printf("Sorted Array is:\n");
 for(i=0;i<10;i++)
  printf("%d\n",arr[i]);

 return 0;
}


Post a Comment

0 Comments