Friday, November 12, 2010

/* Bubble sort. */

/* Bubble sort. */
#include <stdio.h>
#include <conio.h>

void main( )
{
 int arr[5] ;

 for(k=0;k<5;k++)
     {
     printf("Enter element :-");
     scanf("%d",&arr[k]);
    }
int i, j, temp ;
 clrscr( ) ;
 printf ( "Bubble sort.\n" ) ;
 printf ( "\nArray before sorting:\n") ;

 for ( i = 0 ; i <= 4 ; i++ )
  printf ( "%d\t", arr[i] ) ;

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

 printf ( "\n\nArray after sorting:\n") ;
 for ( i = 0 ; i <= 4 ; i++ )
  printf ( "%d\t", arr[i] ) ;

 getch( ) ;
}

No comments: