I am trying to create a selection sort algorithm that takes in an array of random strings and sorts it. I have looked online and in my book to try and model my code off of it and this is what I came up with. I am not sure where I went wrong, any help would be appreciated.
Here is how you load the array with the random strings:
string Sorter::randomString() {
string s = "";
for (int i = 0; i < MAX_CHARS; i++) {
char randomChar = char(rand() % 26 + 97);
s += randomChar;
}
return s;
}
void Sorter::load() {
for (int i = 0; i < MAX_STRINGS; i++)
workspace[i] = randomString();
Here is my selection sort:
void Sorter::selectionSort() {
for (int pass = 0; pass < MAX_STRINGS - 1; pass++) {
string smallest = workspace[pass];
for (int pos = pass + 1; pos < MAX_STRINGS - pass - 1; pos++) {
if (workspace[pos] > smallest) {
smallest = workspace[pos];
}
swap(workspace[pos], workspace[pass]);
}
}
}
I expected the array workspace to be sorted, but it is not :(