C++ program to implement Selection sort using class

by Nideesh C on April 12, 2011 · 1 comment

in C++




#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 = 0 ; i <= count - 2 ; i++ )
	{
		for ( int j = i + 1 ; j <= count - 1 ; j++ )
		{
			if ( arr[i] > arr[j] )
			{
				temp = arr[i] ;
				arr[i] = arr[j] ;
				arr[j] = temp ;
			}
		}
	}
}
void array :: display( )
{
	for ( int i = 0 ; i < count ; i++ )
		cout << arr[i] << "  " ;
	cout << endl ;
}
void main( )
{
	array a ;
	a.add ( 25 ) ;
	a.add ( 17 ) ;
	a.add ( 31 ) ;
	a.add ( 13 ) ;
	a.add ( 2 ) ;
	cout << "\nSelection sort.\n" ;
	cout << "\nArray before sorting:" << endl ;
	a.display( ) ;
	a.sort( ) ;
	cout << "\nArray after selection sorting:" << endl ;
	a.display( ) ;
}



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ program to implement Insertion sort using class
  2. C++ program to implement Quick sort Algorithm using class
  3. C++ program to implement Heap sort Algorithm
  4. C++ rogram to implement merge sort
  5. C program to sort given N elements using SELECTION sort method using functions a) To find maximum of elements b) To swap two elements

{ 1 comment… read it below or add one }

1 abdul April 29, 2011 at 2:38 pm

pls send to my mail id the following prorgram:

C++ program to find the different and total length of two various tubes

Reply

Leave a Comment

Previous post:

Next post: