Bubble Sort algorithm -Data Structure developerIndian.com

Updated:13/08/2023 by Computer Hope

Go Back


Bubble Sort Technique In C++. Bubble Sort is the simplest of the sorting techniques
The average and worst case complexity are of O(n2) where n is the number of items in list.Bubble Sort

//Bubble Sorting
#include<stdio.h>
#include<conio.h>
void main()
{
  int x[10],i,j,t;
  printf("\nEnter 10 number:");
  for(i=0;i<10;i++)
  scanf("%d",&x[i]);
  printf("\n before sorting");
  for(i=0;i<10;i++)
	 printf("  %d",x[i]);
  for(i=0;i<10;i++)
  {
	 for(j=0;j<9-i;j++)
	 {
		if(x[j]>x[j+1])
		{
		  t=x[j];
		  x[j]=x[j+1];
		  x[j+1]=t;
		}
	 }//end of j loop
  }//end of i loop
  printf("\n after sorting");
  for(i=0;i<10;i++)
	 printf("  %d",x[i]);
  getch();
}

Conclusion:

In this Article ,we learn one of the basic types of sorting used in programming is the bubble sort. Bubble sort algorithms go through a series of data (usually numbers) and arrange each number individually into ascending or descending order. The algorithm does this by contrasting number X with number Y that is nearby. The two are switched and the process recurs if X is greater than Y.