Bubble Sorting in Array in C Programming

CWC
2 Min Read

Bubble sorting in Array in C Programming is one of the slowest, if not the slowest, sorts. It is for this reason that its use is very rare and this algorithm remains much criticized. But why is it so slow? We evaluate the speed of a sorting algorithm by observing its number of comparisons / exchanges and thus establish a scale that we call complexity. Bubble sorting makes a lot of comparisons / exchanges for few values ​​to sort. It is mathematically estimated that it averages n (n-1)/ 4 operations for n values ​​to sort.

In the worst case, the complexity of bubble sorting in Array in C Programming is also in O (n²). The worst case is, of this sort, a list sorted in the opposite direction. The best case is a sorted list. If the algorithm is confronted with this case, its complexity improves in O (n), which means that it makes only n comparisons form values ​​to be sorted (in reality, for reasons of intervals, it is n ‘in fact only n-1).

Bubble sorting in Array in C Programming is one of the worst sorts because of its high complexity which results in too long execution time. And yet, this method of sorting continues to be used and this, surely, for its originality. But you still need to know that it is relatively sufficient for small lists on a computer, I say relatively. From twenty elements, it is already better to use another more suitable sorting. You have to choose your algorithm according to your needs.

#include<stdio.h>
#include<conio.h>
void main()
{
   int a[100],i,j,n,t;
   clrscr();
   printf("enter how many value in array\n");
   scanf("%d",&n);
   printf("Enter %d value \n",n);
   for(i=0;i<n;i++)
scanf("%d",&a[i]);
   printf("Original List :\n");
   for(i=0;i<n;i++)
printf("%5d",a[i]);
 
  /* bubble sort logic */
       for(i=1;i<n;i++)
 for(j=0;j<n-i;j++)
    if(a[j]>=a[j+1])
    {
     t=a[j];
     a[j]=a[j+1];
     a[j+1]=t;
    }
   printf("\n\nSorted list :\n");
   for(i=0;i<n;i++)
printf("%5d",a[i]);
 
   getch();
}
Share This Article
1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version