c program bubble sort

>> Friday, November 18, 2011


c program to implement bubble sort. Sorting refers to the operation of rearranging the element. Bubble sort algorithm requires n-1 passes, where n is the number of input items. Here is the c program for bubble sort. The bubble sort gets its name because elements tend to move up into the correct order like bubbles rising to the surface.


#include<stdio.h> #include<conio.h> int main( ) { int a[100]; int i, j, temp, n ; printf("how many numbers you want to sort : \n"); scanf("%d",&n); printf("Enter %d number values you want to sort\n", n); for(j=0; j<n; j++) scanf("%d",&a[j]); for(j=1;j<n;j++) {             for(i=0; i<n; i++)             {                       if(a[i]>a[i+1])                       {                                 temp=a[i];                                 a[i]=a[i+1];                                 a[i+1]=temp;                       }             }      }      printf ( "\n\nArray after sorting:\n") ;      for ( i = 0 ; i <n ; i++ )      printf ( "%d\t", a[i] ) ;      getch(); }
output:

c program bubble sort output
c program to implement bubble sort



Use of Bubble sort :Bubble sort is simple and so it is often used to introduce the concept of sorting algorithm, to introductory computer science students.

0 comments:

Post a Comment

About This Blog

This blog will contain c programs related to interview preparation, basic programs, operating system, graphics, data structure, algorithms implementation, compiler and porjects. C is one of the most widely used programming languages of all time. This blog is intended for engineer students and for the people who are preparing for their interview in IT sector.

Share and Save