矢量元素按降序排序 [英] Sorting vector elements in descending order
问题描述
请告诉我我的方法出了什么问题.当我运行代码时,要花很长时间才能计算出结果.
Please tell me what is wrong in my approach. When I run the code, it is taking too long to compute to see the result.
#include <iostream>
#include <vector>
using namespace std;
vector<int> vec;
vector<int> sort(vector<int> x) {
vector<int> y;
int i = 1;
reset:for(i = 1; i <= x.size(); i++){
for (int j = 1; j <= x.size();) {
if (j == i) {
j++;
}
else {
if (x[i - 1] > x[j - 1]) {
j++;
}
else {
i++;
goto reset;
}
}
}
y.push_back(x[i - 1]);
x.erase(x.begin() + i - 1);
}
return y;
}
int main(){
vec.push_back(5);
vec.push_back(9);
vec.push_back(3);
vec.push_back(6);
vec.push_back(2);
for (int i = 1; i <= vec.size(); i++) {
cout << sort(vec)[i-1] << " ";
}
}
我正在将给定的5个整数序列按降序排序.请帮忙.
I am sorting this given sequence of 5 integers into descending order. Please help.
我的计划是在整个向量x中搜索最大的整数,然后将其移到向量y,然后重复该过程.
My plan was to search for the greatest integer in the whole vector x and move to it to the vector y and repeat the process.
推荐答案
简单的冒泡排序示例
我认为由于 goto reset
语句,您的 sort
函数正在进入无限循环.如果您想实现一个简单的冒泡排序算法,可以这样做:
Simple bubble-sort example
I think that your sort
function is entering an infinite loop because of the goto reset
statement. If you want to implement a simple bubble-sort algorithm, you can do it like this:
#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 takes a vector, and for every consecutive pair of elements in the vector, if they're out of order, it swaps them. This results in the smallest element "bubbling" to the top of the vector. The process is repeated until all the elements are in order.
如果我们对其进行测试,我们会看到它打印出正确的答案:
If we test it, we see that it prints the right 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';
}
使用 std :: sort
可以更快地执行此操作
标准库提供了 sort
函数,该函数可以对几乎所有内容进行排序. std :: sort
确实实现得很好,它比冒泡排序更有效,并且非常易于使用.
Using std::sort
to do this faster
The standard library provides a sort
function that'll sort pretty much anything. std::sort
is really well implemented, it's more efficient than bubble sort, and it's really easy to use.
默认情况下, std :: sort
以升序排序,尽管更改起来很容易,因此它可以降序运行.有两种方法可以做到这一点.第一种方法使用反向迭代器对向量进行排序(这使您假装向量是相反的顺序),第二种方法使用 std :: greater
对向量进行排序,这告诉了 std:: sort
以相反的顺序对事物进行排序.
By default, std::sort
orders things in ascending order, although it's easy to change it so that it works in descending order. There are two ways to do this. The first way sorts the vector using the reverse iterators (which allow you to pretend the vector is in reverse order), and the second way sorts the vector using std::greater
, which tells std::sort
to sort things 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);
我们可以使用 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';
}
这篇关于矢量元素按降序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!