My code:
//Tool: Dev C++
#include <iostream>
void InsertSort( int array[], int len );
int main( void )
{
int array[] = { 42, 20, 17, 13, 28, 14, 23, 15 };
int len;
len= sizeof( array )/ sizeof( array[ 0 ] );
InsertSort( array, len );
return 0;
}
void InsertSort( int array[], int len )
{
int temp;
for( int i= 1; i< len; i++ )
{
for( int j= 0; j< i; j++ )
{
if( array[ j ]> array[ i ] )
{
temp= array[ j ];
array[ j ]= array[ i ];
array[ i ]= temp;
}
}
for( int k= 0; k< len; k++ )
{
std::cout<< array[ k ]<< " ";
}
std::cout<< "\n";
}
}
The result:
20 42 17 13 28 14 23 15
17 20 42 13 28 14 23 15
13 17 20 42 28 14 23 15
13 17 20 28 42 14 23 15
13 14 17 20 28 42 23 15
13 14 17 20 23 28 42 15
13 14 15 17 20 23 28 42
17 20 42 13 28 14 23 15
13 17 20 42 28 14 23 15
13 17 20 28 42 14 23 15
13 14 17 20 28 42 23 15
13 14 17 20 23 28 42 15
13 14 15 17 20 23 28 42
0 Comment:
Post a Comment