C++ selection sort

displaying each pass

Page 1 of 1

2 Replies - 2241 Views - Last Post: 21 February 2009 - 10:56 AM Rate Topic: -----

#1 RLRNY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-November 07

C++ selection sort

Post icon  Posted 16 February 2009 - 12:03 PM

I am doing a program with selection sort
i am able to so the final pass of the sort
but am un able to show the sorts before the
final

i show
8 23 32 45 56 78


and i need to show this
SAMPLE OUTPUT:

23 78 45 8 32 56 (original list)
8 78 45 23 32 56 (pass1)
8 23 45 78 32 56 (pass2)
8 23 32 78 45 56 (pass3)
8 23 32 45 78 56 (pass4)
8 23 32 45 56 78 (pass 5)

what am i forgetting in the following code
#include <iostream>
using namespace std;

void selectionSort(int *array,int length)//selection sort function 
{
	int i,j,min,minat;
	for(i=0;i<(length-1);i++)
	{
		minat=i;
		min=array[i];

	  for(j=i+1;j<(length);j++) //select the min of the rest of array
	  {
		  if(min>array[j])   //ascending order for descending reverse
		  {
			  minat=j;  //the position of the min element 
			  min=array[j];
		  }
	  }
	  int temp=array[i];
	  array[i]=array[minat];  //swap 
	  array[minat]=temp;

		
	}

}








void printElements(int *array,int length) //print array elements
{
	int i=0;
	for(i=0;i<6;i++)
	cout<<array[i]<<" ";
}


void main()
{

	int a[]={23,78,45,8,32,56};   // array to sort 
	selectionSort(a,6);				 //call to selection sort  
	printElements(a,6);			   // print elements 
}




Is This A Good Question/Topic? 0
  • +

Replies To: C++ selection sort

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: C++ selection sort

Posted 17 February 2009 - 12:33 AM

Add a call to printElements() in selectionSort():
...
array[i]=array[minat];  //swap 
array[minat]=temp;		

printElements(array,6);			   // print elements
...


This post has been edited by n8wxs: 17 February 2009 - 12:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 RLRNY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-November 07

Re: C++ selection sort

Posted 21 February 2009 - 10:56 AM

View Postn8wxs, on 16 Feb, 2009 - 11:33 PM, said:

Add a call to printElements() in selectionSort():
...
array[i]=array[minat];  //swap 
array[minat]=temp;		

printElements(array,6);			   // print elements
...




thank you very much this is what i have
#include <iostream>
using namespace std;

void printElements(int array[],int length);

void selectionSort(int *array,int length)//selection sort function
{
	int i,j,min,minat;
	for(i=0;i<(length-1);i++)
	{
		minat=i;
		min=array[i];

	  for(j=i+1;j<(length);j++) //select the min of the rest of array
	  {
		  if(min>array[j])   //ascending order for descending reverse
		  {
			  minat=j;  //the position of the min element
			  min=array[j];
		  }
	  }
	  int temp=array[i];
	  array[i]=array[minat];  //swap
	  array[minat]=temp;
	  printElements(array,6);			   // print elements
	  cout<<endl;

	   
	}

}








void printElements(int *array,int length) //print array elements
{
	int i=0;
	for(i=0;i<6;i++)
	cout<<array[i]<<" ";
}


void main()
{

	int a[]={23,78,45,8,32,56};   // array to sort
	selectionSort(a,6);				 //call to selection sort  
	printElements(a,6);			   // print elements
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1