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);
}
}
|