簡體   English   中英

按降序對向量元素進行排序

[英]Sorting vector elements in descending order

請告訴我我的方法有什么問題。 當我運行代碼時,計算看到結果需要很長時間。

#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 個整數序列按降序排序。 請幫忙。

我的計划是在整個向量 x 中搜索最大的整數並將其移動到向量 y 並重復該過程。

簡單的冒泡排序示例

我認為由於goto reset語句,您的sort函數正在進入無限循環。 如果你想實現一個簡單的冒泡排序算法,你可以這樣做:

#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); 
        }
    }
}

這個函數接受一個向量,對於向量中的每一對連續的元素,如果它們亂序,它就會交換它們。 這導致最小元素“冒泡”到向量的頂部。 重復該過程,直到所有元素都按順序排列。

如果我們測試它,我們會看到它打印出正確的答案:

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實現得非常好,它比冒泡排序更有效,而且真的很容易使用。

默認情況下, std::sort按升序排列事物,盡管很容易更改它以使其按降序工作。 有兩種方法可以做到這一點。 第一種方法使用反向迭代器對向量進行排序(它允許您假裝向量按相反順序排列),第二種方法使用std::greater對向量進行std::sort ,它告訴std::sort以相反的順序對事物進行排序。

// 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';
}

為什么不能只使用 std:sort? 你可以這樣做:

sort(vec.begin(), vec.end(), [](const int a, const int b) {return a > b; });  //1

正如評論中所建議的,上述有兩種選擇:

std::sort(vec.begin(), vec.end(), std::greater<>());  //2

和:

std::sort(vec.rbegin(), vec.rend());  //3

(2) 和 (3) 避免使用自定義比較函數,並且 (2) 可以說更明確地說明了它的意圖。 但我對性能很感興趣,所以我對三者進行了快速的板凳比較。

使用 Clang 12.0,(1) 是最快的:

在此處輸入圖片說明

Clang 結果在這里

但是,在 GCC 10.3 中,這三個幾乎相同:

在此處輸入圖片說明

海合會結果在這里

有趣的結果! 使用 GCC,您可以選擇喜歡哪個版本; 否則我會選擇(1)或(2)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM