Double-Ended Selection Sort Help

I'm sorting a vector of random integers using a double-ended selection sort function. I've never done this before and wrote this code based on an algorithm. I've got it working for the most part but it always either adds extra numbers or leaves a number unsorted.

Sample output:
3 14 13 2 13 0 6 15 14 12 1 7 15 1 11 10
0 14 13 2 13 3 6 15 14 12 1 7 10 1 11 15
0 1 13 2 13 3 6 11 14 12 14 7 10 1 15 15
0 1 1 2 13 3 6 11 14 12 13 7 10 14 15 15
0 1 1 2 13 3 6 11 10 12 13 7 14 14 15 15
0 1 1 2 3 13 6 11 10 12 7 13 14 14 15 15
0 1 1 2 3 7 13 11 10 12 13 13 14 14 15 15
0 1 1 2 3 7 12 11 13 13 13 13 14 14 15 15
0 1 1 2 3 7 12 11 13 13 13 13 14 14 15 15

8 10 15 1 7 8 1 10 13 8 11 1 5 13 4 12
1 10 12 8 7 8 1 10 13 8 11 1 5 13 4 15
1 1 12 8 7 8 10 10 4 8 11 1 5 13 13 15
1 1 1 8 7 8 10 10 4 8 11 12 5 13 13 15
1 1 1 4 7 8 10 10 8 8 11 5 12 13 13 15
1 1 1 4 5 8 10 10 8 8 7 11 12 13 13 15
1 1 1 4 5 7 10 8 8 8 10 11 12 13 13 15
1 1 1 4 5 7 8 10 8 10 10 11 12 13 13 15
1 1 1 4 5 7 8 10 10 10 10 11 12 13 13 15

1 14 12 4 6 5 10 11 15 15 1 4 3 2 12 15
1 14 12 4 6 5 10 11 15 15 1 4 3 2 12 15
1 1 12 4 6 5 10 11 15 12 14 4 3 2 15 15
1 1 2 4 6 5 10 11 12 12 14 4 3 15 15 15
1 1 2 3 6 5 10 11 12 12 4 4 14 15 15 15
1 1 2 3 4 5 10 11 12 4 6 12 14 15 15 15
1 1 2 3 4 4 10 11 6 5 12 12 14 15 15 15
1 1 2 3 4 4 10 11 6 10 12 12 14 15 15 15
1 1 2 3 4 4 10 11 11 10 12 12 14 15 15 15

Sorting Function:
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
template <typename T>
void doubleSelectionSort(vector<T> &v, int vecSize)
{
  T min;
  T max;
  int minCount;
  int maxCount;
  int seek;
  int maxSeek = vecSize-1;
  int j;

  for(seek = 0; seek < maxSeek; seek++)
    {
      minCount = seek;
      min = v[seek];
      maxCount = maxSeek;
      max = v[maxSeek];
      j = maxSeek;

      for(int i = seek + 1; i < vecSize; i++)
	{
	  if(v[i] < min)
	    {
	      min = v[i];
	      minCount = i;
	    }

	  else if(v[j] > max)
	    {
	      max = v[j];
	      maxCount = j;
	    }
	  j--;
	}

      v[minCount] = v[seek];
      v[seek] = min;
      v[maxCount] = v[maxSeek];
      v[maxSeek] = max;

      maxSeek--;

      displayVector(v, vecSize);
    }
}
Last edited on
Why else in line 28?
Thanks. I wasn't thinking when I put that. I changed it and it works more often now but still adds extra numbers sometimes.

6 11 12 8 2 10 12 6 13 4 15 3 9 12 15 8
2 11 12 8 6 10 12 6 13 4 15 3 9 12 8 15
2 3 12 8 6 10 12 6 13 4 8 11 9 12 15 15
2 3 4 8 6 10 12 6 12 12 8 11 9 13 15 15
2 3 4 6 8 10 12 6 12 9 8 11 12 13 15 15
2 3 4 6 6 10 12 8 11 9 8 12 12 13 15 15
2 3 4 6 6 8 8 10 11 9 12 12 12 13 15 15
2 3 4 6 6 8 8 10 9 11 12 12 12 13 15 15
2 3 4 6 6 8 8 10 10 11 12 12 12 13 15 15 <- My function

2 3 4 6 6 8 8 9 10 11 12 12 12 13 15 15 <- correct sort

4 3 0 9 12 14 14 0 3 2 2 8 10 3 4 12
0 3 4 9 12 14 12 0 3 2 2 8 10 3 4 14
0 0 4 9 12 4 12 3 3 2 2 8 10 3 14 14
0 0 2 9 12 4 3 3 3 4 2 8 10 12 14 14
0 0 2 2 10 4 3 3 3 4 9 8 12 12 14 14
0 0 2 2 8 4 10 3 3 4 9 10 12 12 14 14
0 0 2 2 8 3 9 4 3 4 10 10 12 12 14 14
0 0 2 2 8 3 4 4 9 9 10 10 12 12 14 14
0 0 2 2 8 3 4 4 9 9 10 10 12 12 14 14 <- My Function

0 0 2 2 3 3 3 4 4 8 9 10 12 12 14 14 <- correct sort

1 9 3 4 14 12 12 2 15 15 8 14 10 4 13 7
1 9 3 4 14 12 12 2 15 7 8 14 10 4 13 15
1 2 3 4 14 12 12 9 13 7 8 14 10 4 15 15
1 2 3 4 14 12 12 9 13 7 8 4 10 14 15 15
1 2 3 4 10 12 12 9 13 7 8 4 14 14 15 15
1 2 3 4 4 12 12 9 10 7 8 13 14 14 15 15
1 2 3 4 4 7 8 9 10 12 12 13 14 14 15 15
1 2 3 4 4 7 8 9 10 12 12 13 14 14 15 15
1 2 3 4 4 7 8 9 10 12 12 13 14 14 15 15 <- My Function

1 2 3 4 4 7 8 9 10 12 12 13 14 14 15 15 <- correct sort. Function worked this time
Last edited on
Did you tried a look at google for your algorithm?
Ok yea I just did. Found something that worked. Thanks. Probably should have done that first.
Topic archived. No new replies allowed.