Tuesday, March 10, 2009

C Program Bubble Sort

// Program for Bubble Sort

#include <iostream.h>
#include <conio.h>

//----------------------------------------------------------------------

void Bubblesort ( int array[] , int UB )
{
for ( int i = 0 ; i <= UB ; i++ )
for ( int j=0 ; j<=UB-1 ; j++ )
{
if ( array [j] > array [j+1] )
{
int temp = array [j] ;
array [j] = array[j+1] ;
array [j+1] = temp ;

}
}
}


void Showarray ( int array[] , int UB )
{
gotoxy ( 1,2 ) ;
cout << "\n" ;

for ( int i = 0 ; i <= UB ; i++ )
{
cout << array [i] << endl ;
}

}

//----------------------------------------------------------------------

void main ()
{
clrscr ();

int array [10] = { 42 , 23 , 74 , 11 , 65 , 58 , 94 , 36 , 99 , 87 };

Bubblesort ( array , 9 ) ;

Showarray ( array , 9 ) ;

getch ();

}

No comments:

Post a Comment