Because of the goto reset instruction, I believe your sort function has entered an infinite loop.
If you wish to construct a basic bubble-sort algorithm, follow these steps:
#include <iostream>
#include <utility>
#include <vector>
void bubble_sort(std::vector<int>& v) {
if(v.size() == 0) return;
for(int max = v.size(); max > 0; max--) {
for(int i = 1; i < max; i++) {
int& current = v[i - 1];
int& next = v[i];
if(current < next)
std::swap(current, next);
}
}
}
This function accepts a vector and swaps any consecutive pair of items in the vector that are out of order.
The smallest element "bubbles" to the top of the vector as a consequence.
The process is continued until all of the pieces are in their proper place.
When we run it, we observe that it outputs the correct answer:
int main() {
std::vector<int> test = {5, 9, 3, 6, 2};
bubble_sort(test);
for(int i : test) {
std::cout << i << ' ';
}
std::cout << '\n';
}
To make this process faster, I'm going to use std::sort.
The standard library includes a sort function that can sort almost anything.
std::sort is extremely well implemented, more efficient than bubble sort, and quite simple to use.
By default, std::sort sorts objects in ascending order, but it's simple to alter it to work in descending order.
There are two approaches to this.
The first method sorts the vector using reverse iterators (which enable you to pretend the vector is in reverse order), while the second method uses std::greater, which instructs std::sort to sort items in reverse order.
// Way 1:
std::sort(test.rbegin(), test.rend());
// Way 2:
auto compare_func = std::greater<>();
std::sort(test.begin(), test.end(), compare_func);
We can re-write the program using std::sort:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> test = {5, 9, 3, 6, 2};
auto compare_function = std::greater<>();
std::sort(test.begin(), test.end(), compare_function);
for(int i : test) {
std::cout << i << ' ';
}
std::cout << '\n';
}