void sorter1(vector<float> unsortedVector) {
bool Finished = false;
string debugMode;
// bool sameIndex2;
int index = 0;
int index2 = 0;
int tempMaxIndex{};
// places the first number
vector<float> sorted(unsortedVector.size()); // we sort to this
vector<bool> used(unsortedVector.size()); // notes used
// high numbers
cout << "debug mode?" << endl;
cin >> debugMode;
cout << endl;
//auto timeStart = chrono::steady_clock::now();;
while (!Finished) {
if (sorted[index2] < unsortedVector[index] && !used[index]) {
sorted[index2] = unsortedVector[index];
tempMaxIndex = index;
if (debugMode == "yes") {
cout << endl << "this is at index:" << index << endl;
cout << "this is at index2:" << index2 << endl;
}
}
if (index2 == unsortedVector.size() - 1) {
Finished = true;
for (int k = 0; k < sorted.size() - 1; k++) {
cout << sorted[k] << endl;
}
if (debugMode == "yes") {
cout << endl << "this is sorted size " << sorted.size();
cout << endl
<< "this is unsorted size " << unsortedVector.size() << endl;
for (int z = 0; z < used.size(); z++) {
cout << "this index is " << boolalpha;
cout << used[z] << endl;
}
}
}
if (index == unsortedVector.size() - 1) {
index = 0;
used[tempMaxIndex] = true;
index2++;
}
else {
index++;
}
}
//auto timeEnd = chrono::steady_clock::now();
//chrono::milliseconds time = timeEnd-timeStart;
//cout << "Time Elapsed:";
//get time here
}```
#I spent a while on this sorting algorithm i made for fun
5 messages · Page 1 of 1 (latest)
FWIW here is the version of your older version I made to fix it before trying to help you with it https://godbolt.org/z/hz5xGff1G
here were the three overlapping bugs I found and changes I made to correct for them:
- the second if branch set index to zero, but then proceeded to increment it to 1, this would always skip the first element, except on the first loop through. So I moved the index increment directly before it and removed the
-1on the index overflow check to compensate. - the used vector was initialized to zeros, which, since 0 is an index, would cause the sorter to erroneously think that zero index (the first element) was already sorted, so instead I initialized it to -1.
- the second if branch would increment the second index right before the third branch checked for overflow, because it had a
-1to the size it was checking for it would skip the last slot and leave it empty. I removed the-1.
If you are curious, you have implemented a non-inplace https://en.wikipedia.org/wiki/Selection_sort (I guess I might call it a hybrid variant with an insertion sort https://en.wikipedia.org/wiki/Insertion_sort )