Simple Selection Sort Structured Code

Sorting RAndom Numbers through a Simple Selection Sort

Page 1 of 1

1 Replies - 5484 Views - Last Post: 26 April 2007 - 11:54 AM Rate Topic: -----

#1 lovelyc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-April 07

Simple Selection Sort Structured Code

Posted 26 April 2007 - 08:43 AM

I have currently attempted to write a structured program which includes the header, test, and implementation files for a simple selection sort of 10 random numbers. It is not working. I have deleted alot of the problems and started over. Please tell me what I am missing.


implementation file:

#include <vcl.h>

using std::cin;
using std::cout;
using std::endl;
using std::exit;


#include "Sort.h"

int randomnumber;

#include "Header.h"

//---------------------------------------------------------------------------

Sort::Sort()
 {
	 for (int i = 0; i < 10; i++)
	 {
		myArray[i] = EMPTY;
	 }
	 cout<<"This is the Chandria Poole Simple Selection Sort Constructor."<<endl;
 }

//----------------------------------------------------------------------------
Sort::~Sort()
 {
		cout<<"This is the Chandria Poole Simple Selection Sort Destructor."<<endl;
		cin.get();
 }

//---------------------------------------------------------------------------

void Sort::Swap(int numbers[], int array_size)

{
	int i;
	int j;
	int min;
	int temp;

	for (i = randomNumber; i < array_size-1; i++)
	  {
			min = i;

	for (j = i+1; j < array_size; j++)
	  {
			if (numbers[j] < numbers[min])
				{
					min = j;
	  }		 }

	temp = numbers[i];

	numbers[i] = numbers[min];

	numbers[min] = temp;
  }

}
//---------------------------------------------------------------------------

void Sort::Random()
{
srand(time(0));
int randomNumber = rand() % 50 + 1;

}


header file:


//---------------------------------------------------------------------------

#ifndef SortH
#define SortH

	const int ARRAY_CAPACITY = 10;
	const int EMPTY = -1;
	typedef int ArrayElement;

class Sort
{
	public:

		Sort(void);
		~Sort(void);
		void Sort::Swap(int numbers[], int array_size;
		

	private:
	 /***** Data Members *****/
		ArrayElement  myArray[ARRAY_CAPACITY];
};
//---------------------------------------------------------------------------

#endif
//---------------------------------------------------------------------------



Is This A Good Question/Topic? 0
  • +

Replies To: Simple Selection Sort Structured Code

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Simple Selection Sort Structured Code

Posted 26 April 2007 - 11:54 AM

in your headder:

void Sort::Swap(int numbers[], int array_size);

the line for (i = randomNumber; i < array_size-1; i++)
uses and uninitialized randomNumber however even if you did run void Sort::Random() that chooses a number that may be outside of the array's range (remeber that ARRAY_CAPACITY = 10).

all in all I can't see HOW this sorts anything. This is the selection sort.

I think what you want to do is make a function that fills the array with random numbers. (just a loop from 0 to ARRAY_CAPACITY-1 that sets myArray[i] to a random value).

Then you want to impliment a sort function as discribed in the wikipedia article. You may want to impliment you own "swap" routine and there are LOTS of examples of that on this site (look in the snippets or search the forum). There is also a swap function included with the STL.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1