Oct 6, 2014

Bubble sort

7:53 PM Posted by Unknown , No comments
//Function: bubble sort
//Tool: Dev C++

#include <iostream>

void bubble_sort( int array[], int len );


int main( void )
{
int len;
int array[]= { 4, 6, 22, 66, 43, 23, 90, 65, 42 };

len= sizeof( array )/ sizeof( array[ 0 ] );

bubble_sort( array, len );

return 0;
}

void bubble_sort( int array[], int len )
{
int temp;

for( int i= 0; i< len- 1; i++ )
{
for( int j= 0; j< len- 1- i ; j++ )
{
if( array[ j ]> array[ j+ 1 ] )
{
temp= array[ j ];
array[ j ]= array[ j+1];
array[ j+ 1 ]= temp;
}
}

for( int k= 0; k< len; k++ )
{
std::cout<< array[ k ]<< " ";
}
std::cout<< "\n";
}
}


The result:

4 6 22 43 23 66 65 42 90 
4 6 22 23 43 65 42 66 90 
4 6 22 23 43 42 65 66 90 
4 6 22 23 42 43 65 66 90 
4 6 22 23 42 43 65 66 90 
4 6 22 23 42 43 65 66 90 
4 6 22 23 42 43 65 66 90 
4 6 22 23 42 43 65 66 90 

0 Comment:

Post a Comment