Wednesday, 28 November 2012

Bubble sort algorithm with time complexity calculation


//Bubble sort algorithm implementation. Also calculate time complexity.
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[100],temp,i,j,n,count;
 count=0;count++;
 printf("Bubble Sort\nEnter total no. of values: ");
 {
  scanf("%d",&n);
  count++;
 }
 printf("Enter the values:-\n");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
  count++;
 }
 for(i=0;i<n;i++)
 {
  for(j=i;j<n;j++)
  {
   if(a[j]<a[i])
   {
    temp=a[j];count++;
    a[j]=a[i];count++;
    a[i]=temp;count++;
   }
   count++;
  }
  count++;
 }
 printf("Bubble-Sort result:-\n");
 for(i=0;i<n;i++)
 {
  printf("%d\t",a[i]);
  count++;
 }
 printf("\nTime Complexity is: %d",count);
 getch();
}
//program written by Mars.

4 comments:

You are welcome, your comment helps me to make this blog more better.