Selection sort problem

I can not seem to get this selection sort program to work.



#include <iostream>

using std::cout;

using std::endl;

#include <cstdlib>

using std::rand;

using std::srand;

#include <ctime>

using std::time;

// Function Prototype

void selectionSort( int [], int );

int main()

{
// Given variable and constant declarations

const int SIZE = 10;

const int MAXRANGE = 1000;

int sortThisArray[ SIZE ] = {};

srand( time( 0 ) );

// Fill array with random numbers between 1-1000

cout <<"Unsorted array is:\n";


for(int i=0; i<SIZE; i++){

sortThisArray[ SIZE ] = (rand()%1000)+1;


// Display the unsorted array

cout << "Unsorted array is:\n";

for (int i =0; i< SIZE; i++)

cout << setw(4) << sortThisArray[i];

cout <<endl;


} // end main


}

// Display the sorted array


cout << "Sorted array is:\n";


// Call the sorting array function

selectionSort (sortThisArray, SIZE );


{



for (int j=0; j< SIZE; j++)


cout << setw(4) << sortThisArray[j];


cout << endl;

} // end main


return 0; // indicates successful termination

} // end main


// Function definition to sort the array

void selectionSort( int array[], int size )


{

int temp; // temporary variable used for swapping

int i;

int j;
// sort array until only one element is left

for (i=0; i< SIZE-1; i++)

{

j = i;


while (j > 0 && array [j-1] > array [j])



{


temp = array [j];
array[j] = array [j-1];
array [j-1] = temp;
j--;


}


}




} // end method selectionSort
Last edited on
@gneisler

Program fixed. In the future, put your code between the code tags.. [ CODE][/ CODE] ( without the extra space after the [. Makes it MUCH easier to read code. I explained in a few places what I changed to make it work. Compare this code to yours, to see the rest.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using std::setw;
using std::cout;
using std::endl;
using std::rand;
using std::srand;
using std::time;

// Function Prototype

void selectionSort( int [], int );

int main()
{
	// Given variable and constant declarations
	const int SIZE = 10;
	const int MAXRANGE = 1000;
	int sortThisArray[ SIZE ] = {0};
	time_t t;
	srand((unsigned) time(&t));
	// Fill array with random numbers between 1-1000
	for(int i=0; i<SIZE; i++)
	{
		sortThisArray[ i ] = (rand()%MAXRANGE)+1;// You had const SIZE here, so nothing was being
		                                     // inputted. sortThisArray would go from 0 to 9, no 10
	}
	// Display the unsorted array
	cout << "Unsorted array is:\n";
	for (int i =0; i< SIZE; i++)
	{
		cout << setw(4) << sortThisArray[i];
	}
	cout <<endl;

	// Display the sorted array
	cout << "Sorted array is:\n";
	// Call the sorting array function
	selectionSort(sortThisArray, SIZE );
	{
		for (int j=0; j< SIZE; j++)
			cout << setw(4) << sortThisArray[j];
		cout << endl;
	} // end main

	return 0; // indicates successful termination
} // end main

// Function definition to sort the array
void selectionSort( int array[], int size )
{
	int temp; // temporary variable used for swapping
	int i;
	int j;
	// sort array until only one element is left
	for (i=0; i< size; i++) // You used the const SIZE, which is unknown to the routine, plus 
		                    // you were sorting with size - 2 ( < size-1)
	{
		j = i;
		while (j > 0 && (array [j-1] > array [j]))
		{
			temp = array [j];
			array[j] = array [j-1];
			array [j-1] = temp;
			j--;
		}
	}
} // end method selectionSort  
Last edited on
With some modification

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cstdlib>
#include <ctime>

#define SIZE = 10;
#define MAXRANGE = 1000;
// Function Prototype

void selectionSort( int *, int );

int main()
{
			// Given variable and constant declarations

				int sortThisArray[ SIZE ] = {0};
				srand( time( 0 ) );

			// Fill array with random numbers between 1-1000
			cout <<"Unsorted array is:\n";

			for(int i=0; i<SIZE; i++)
				{
					sortThisArray[ SIZE ] = (rand()%1000)+1;
					// Display the unsorted array
					cout << "Unsorted array is:\n";

					for (int i =0; i< SIZE; i++)
						cout << setw(4) << sortThisArray[i];
					cout <<endl;


				}

		selectionSort (sortThisArray, SIZE );
		for (int j=0; j< SIZE; j++)
		cout << setw(4) << sortThisArray[j];
		cout << endl;
return 0; // indicates successful termination

} // end main


// Function definition to sort the array

void selectionSort( int *array, int size )
{

		int temp , i , j ; 
		for (i=0; i< SIZE-1; i++)
		{
				j = i;
				while (j > 0 && array [j-1] > array [j])
					{
						temp = array [j];
						array[j] = array [j-1];
						array [j-1] = temp;
						j--;
						}
		}
} // end method selectionSort 
Topic archived. No new replies allowed.