#include <iostream.h>
const int MAX = 10 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int item ) ;
void sort( ) ;
void display( ) ;
} ;
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
void array :: add ( int item )
{
if ( count < MAX )
{
arr[count] = item ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
void array :: sort( )
{
int temp ;
for ( int i = 1 ; i <= count - 1 ; i++ )
{
for ( int j = 0 ; j < i ; j++ )
{
if ( arr[j] > arr[i] )
{
temp = arr[j] ;
arr[j] = arr[i] ;
for ( int k = i ; k > j ; k-- )
arr[k] = arr[k - 1] ;
arr[k + 1] = temp ;
}
}
}
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "\t" ;
cout << endl ;
}
void main( )
{
array a ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 31 ) ;
a.add ( 13 ) ;
a.add ( 2 ) ;
cout << "\nInsertion sort.\n" ;
cout << "\nArray before sorting:" << endl ;
a.display( ) ;
a.sort( ) ;
cout << "\nArray after insertion sorting:" << endl ;
a.display( ) ;
} |
#include <iostream.h>
const int MAX = 10 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int item ) ;
void sort( ) ;
void display( ) ;
} ;
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
void array :: add ( int item )
{
if ( count < MAX )
{
arr[count] = item ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
void array :: sort( )
{
int temp ;
for ( int i = 1 ; i <= count - 1 ; i++ )
{
for ( int j = 0 ; j < i ; j++ )
{
if ( arr[j] > arr[i] )
{
temp = arr[j] ;
arr[j] = arr[i] ;
for ( int k = i ; k > j ; k-- )
arr[k] = arr[k - 1] ;
arr[k + 1] = temp ;
}
}
}
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "\t" ;
cout << endl ;
}
void main( )
{
array a ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 31 ) ;
a.add ( 13 ) ;
a.add ( 2 ) ;
cout << "\nInsertion sort.\n" ;
cout << "\nArray before sorting:" << endl ;
a.display( ) ;
a.sort( ) ;
cout << "\nArray after insertion sorting:" << endl ;
a.display( ) ;
}
Editorial Team
We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.
The last (k + 1) variable in the function void sort() is undefined. Can you fix it..??
nice
that’s nice but i want it in c and java both.
Better way to implement Insertion Sort