Help

Write your question here.

Using pseudocode, write a program which employs the Selection Sort
technique to sort an array called ‘elements’ of size 20.
What have you gotten done on it so far?
Good eveing, am doing a course in programming and am not grasping anything. i was just checking out this site because i have an exam tomorrow. i have gotten nothing done thus far
Well, here is a sample selection sort algorithm. This is a complete program, not pseudocode. You will need to generate your own pseudocode from this, owever since I have provided you with a complete program, writing pseudocode should be easy as apple pie.

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
#include <iostream>

using namespace std;

const int arraySize = 10;

int main (int argc, char const *argv[])
{
	// array to be sorted
	int array[arraySize] = {5, 3, 7, 2, 6, 9, 0, 8, 1, 4};
	int minIndex;
	int minValue;
	
	//selection sort for array
	for (size_t i = 0; i < (arraySize - 1); ++i)
	{
		 //initialize both to zero, then increment them using i
		minIndex = i;
		minValue = array[i];
		
		//finds smallest value in remaining section of array
		for (int index = i + 1; index < (arraySize); index++)
		{
			// if index is less than the minValue variable, 
			if (array[index] < minValue)
			{
				//change minValue to whatever array element the loop is on
				minValue = array[index];
				
				// increment minValue with index
				minIndex = index;
			}
		}
		//replace the minIndex variable's companion in the array with the current starter value
		array[minIndex] = array[i];
		
		//replace the current element of array with the minValue
		array[i] = minValue;
	}
	
	for (size_t i = 0; i < arraySize; ++i)
	{
		cout << array[i] << " ";
	}
	return 0;
}
Output from /users/max/tmp/c++/testprs/untitled.cc
0 1 2 3 4 5 6 7 8 9


Best,
max
Topic archived. No new replies allowed.