-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortableVectorStub.h
49 lines (41 loc) · 1.11 KB
/
SortableVectorStub.h
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
// Specification file for the SortableVector class
#ifndef SORTABLEVECTOR_H
#define SORTABLEVECTOR_H
#include "SimpleVector.h"
template <class T>
class SortableVector : public SimpleVector<T>
{
public:
// Constructor
SortableVector(int size) : SimpleVector<T>(size) {}
// Copy constructor
SortableVector(const SortableVector &obj) : SimpleVector<T>(obj) {}
// Sort the vector using selection sort
void sort();
};
template <class T>
void SortableVector<T>::sort()
{
int minIndex;
T minValue;
for (int i = 0; i < this->size() - 1; i++)
{
minIndex = i;
minValue = this->operator[](i);
for (int j = i + 1; j < this->size(); j++)
{
if (this->operator[](j) < minValue)
{
minIndex = j;
minValue = this->operator[](j);
}
}
if (minIndex != i)
{
T temp = this->operator[](i);
this->operator[](i) = this->operator[](minIndex);
this->operator[](minIndex) = temp;
}
}
}
#endif