Oct 8, 2014

Shell sort

12:37 AM Posted by Unknown , No comments
//Tool: Dev C++ 

#include <iostream>

void ShellSort( int array[], int gap, int len );

int main( void )
{
int len;
//int array[]= { 8, 3, 7, 9, 1, 24, 2 };
int array[]= { 42, 20, 17, 13, 28, 14, 23, 15 };

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

for( int gap= len/ 2; gap> 0; gap/= 2 )
{
ShellSort( array, gap, len );
}

return 0;
}

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

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

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

}

The result:
28 20 17 13 42 14 23 15 
28 14 17 13 42 20 23 15 
28 14 17 13 42 20 23 15 
28 14 17 13 42 20 23 15 
17 14 28 13 42 20 23 15 
17 13 28 14 42 20 23 15 
17 13 28 14 42 20 23 15 
17 13 28 14 42 20 23 15 
17 13 23 14 28 20 42 15 
17 13 23 14 28 15 42 20 
13 17 23 14 28 15 42 20 
13 17 23 14 28 15 42 20 
13 14 17 23 28 15 42 20 
13 14 17 23 28 15 42 20 
13 14 15 17 23 28 42 20 
13 14 15 17 23 28 42 20 
13 14 15 17 20 23 28 42 

Oct 7, 2014

Select sort

8:10 PM Posted by Unknown , No comments
//Tool: Dev C++

#include <iostream>

void SelectSort( 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 ] );

SelectSort( array, len );

return 0;
}

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

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

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


The result:
42 20 17 13 28 14 23 15
-----------------------
13 42 20 17 28 14 23 15 
13 14 42 20 28 17 23 15 
13 14 15 42 28 20 23 17 
13 14 15 17 42 28 23 20 
13 14 15 17 20 42 28 23 
13 14 15 17 20 23 42 28 
13 14 15 17 20 23 28 42 
13 14 15 17 20 23 28 42 

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

Oct 6, 2014

Binary search

7:56 PM Posted by Unknown , No comments
//Functin: binary search
//Tool: Dev C++
//notes: first, bubble sort
//       second, binary search

#include <iostream>

void bin_search( int array[], int low, int high, int key );
void bubble_sort( int array[], int len );


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

key= 90;
high= sizeof( array )/ sizeof( array[ 0 ] );
low= 0;

bubble_sort( array, high );

bin_search( array, low, high- 1, key );
std::cout<< std::endl;

return 0;
}


void bin_search( int array[], int low, int high, int key )
{
int mid;

mid= ( low+ high )/ 2;

if( low> high )
{
return ;  //game over
}

if( array[ mid ]== key )
{
std::cout<< "I find: key= "<< array[ mid ];
}
else if( array[ mid ]> key )
{
bin_search( array, low, mid- 1, key );
}
else
{
bin_search( array, mid+ 1, high, key );
}

return ;
}

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 

I find: key= 90