# Simple Selection Sort Structured Code

Page 1 of 1

## 1 Replies - 7088 Views - Last Post: 26 April 2007 - 11:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=27270&amp;s=900670dfe9ecf3a0075477cef5be2c40&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lovelyc

Reputation: 0
• 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;

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

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;

}

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

#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

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Simple Selection Sort Structured Code

Posted 26 April 2007 - 11:54 AM

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.