Oct 7, 2014

Insert sort( 2 )

7:57 PM Posted by Unknown , No comments
//Tool: Dev C++

#include <iostream>

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

int main( void )
{
 int array[] = { 42, 20, 17, 13, 28, 14, 23, 15 };
 int len= 0;

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

  InsertSort( len, array );

  return 0;
}


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

  for( int i= 0; i< len; i++ )
 {
  int j= i;

   while( j> 0 && array[ j-1 ]> array[ j ] )
  {
   temp= array[ j- 1 ];
   array[ j- 1 ]= array[ j ];
   array[ j ]= temp;

    j--;  //j= j- 1
  }

   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

0 Comment:

Post a Comment