selection sort

This is a selection sort algorithm!
IT doesn't work! any suggestions?


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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void selectionSort (int size [], int array);
void swap (int *min_pos, int *max);
int findMax (int size [],int array, int i);
void fillArray (int size[], int);
void printArray (int size [], int array);

int main ()
{

  int size [25];
  int array = 25;

  fillArray (size , array);
  selectionSort (size , array);
  printArray (size, array);

return 0;
}


/*******************************************************************/
void fillArray (int size[], int)
{
  int x;
  srand  (( unsigned int) time (NULL));
  for (x=0; x < 25; x++)
  {
  size [x] = rand () % 100;
  cout << size [x] << " ";

  }
  cout << endl << endl;
}
/*******************************************************************/
void selectionSort (int size [], int array)
{
  int min_pos;
  int max;
  int n=0;
  for (int i=0; i < 24; i++)
        {
  cout << size [i] << "  you ";
  max = findMax (size, array, n);
        if (max !=i)
        swap (&i, &max);
  n++;
  cout << " " << n << " " << i;
        }
  cout << endl << endl << size [2] << endl << endl;
}
/*******************************************************************/
int findMax (int size [], int array, int i)
{
  int min_pos;
  min_pos = 0;
  for (int j=i + 1; j < array; j++)
        {
  if (size [j] < size[min_pos])
        min_pos = j;
        }
  return (min_pos);
}
/*******************************************************************/
void swap (int *min_pos, int *max)
{
  int temp;
   temp = *min_pos;
  *min_pos = *max;
  *max = temp;
}
/*******************************************************************/
/*******************************************************************/
void printArray (int size[], int array)
{
  for (int i = 0; i < array; i++)
  cout << size [i] << " ";
  cout << endl << endl << size [1] << endl;
}

Last edited on
What do you mean by "it doesn't work"? Does it erase top secret government files? Does it cause your next-door neighbor's blender to explode? Does it cause you to fail a sobriety test? You need to be specific.
right now it j is in an endless loop!
but if i got the endless loop to stop then it would not sort the numbers.
You've switched the names of "array" and "size", which is probably confusing you to the point of being incapable of explaining what you mean. Maybe if you renamed them back to how they should be you wouldn't be so confused anymore ;)
You've also named your function that returns the minimum value findMax.

7
8
for (int i=0; i < 24; i++)
{

I think you meant i < array here rather than i < 24.
(Of course, array is a really weird name for the size and size is a really weird name for an array....)

You also don't really need your n variable, since the values of n follow right along with the values of i, so you can just use i instead of n.

11
12
if (max !=i)
    swap (&i, &max);

Here, you're just swapping the values of i and max -- what you really want is to swap the values in the array (or should I say "in the 'size' "? :-) ) at positions i and max.
So it should be
swap(&size[i], &size[max]); (for readability)
or
swap(size + i, size + max); (same thing).

19
20
21
22
int findMax (int size [], int array, int i)
{
    int min_pos;
    min_pos = 0;

Since the assumption at this point is that all of the array elements from position 0 to position i-1 are in their final (sorted) position, this should be
min_pos = i;
instead, because we're trying to find the minimum of the rest of the array (which goes from i to array-1).

Fix all of those and your code should work.
Thank you! YOU ARE AWESOME!
Topic archived. No new replies allowed.