std :: sort函数未对向量进行排序,我也不知道为什么?

萨斯瓦塔·米什拉(Saswata Mishra)

我已经编写了一个代码来在c ++中实现quicksort算法,它可以工作,但是std :: sort()函数根本不能工作。请向我解释原因。

#include "header.h"

using namespace std;
using namespace std::chrono;
bool myfunction (int i,int j) { return (i<j); }
int Partition(vector<int>& A, int start, int end){
    int pivot_idx = (rand() % (end - start + 1)) + start;
    Xswap(A[pivot_idx], A[end]);
    int pivot = A[end];
    int P_index = start;
    for(int i=start; i < end; i++){
        if(A[i] <= pivot){
            Xswap(A[i], A[P_index]);
            P_index++;
        }
    }
    Xswap(A[P_index], A[end]);
    return P_index;
}

void Qsort(vector<int>& A, int start, int end){
    if(start < end){
        int middle = Partition(A, start, end);
        Qsort(A, start, middle-1);
        Qsort(A, middle+1, end);
    }
}

void quick_sort(void){
    srand(time(NULL));
    int n;
    cin >> n;
    vector<int> v;
    v.reserve(n);
    //v.shrink_to_fit();
    
    for(int i=0; i<n; i++)
        v[i] = rand() % 1000;
    
    /*for(int i=0; i<n; i++)
        cout << v[i] << " ";
    cout << endl << endl;*/
    auto start = high_resolution_clock::now(); 
    
    //Qsort(v, 0, n-1);
    sort(v.begin(), v.end(), myfunction);
    
    auto stop = high_resolution_clock::now(); 
    auto duration = duration_cast<microseconds>(stop - start);
    for(int i=0; i<n; i++)
        cout << v[i] << " ";
    cout << endl;
    cout << duration.count() << endl; 
}

如果您想知道在调用执行代码段后显示的输出: 程序输出

如果要查看头文件的内容:

#ifndef HEADER_H
#define HEADER_H

#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <vector>
#include <chrono>
#include <time.h>
void bble_sort(void);
void improved_bble_sort(void);
void quick_sort(void);
void ins_sort(void);
void Qsort(std::vector<int>& A, int start, int end);

template <typename T>
void Xswap(T& a, T& b);

#endif /* HEADER_H */

输出中的向量完全未排序;我已经尝试过基于范围的for循环和迭代器,但没有任何帮助。请给我一些解决方案或想法,我完全感到困惑。

比尔·林奇

std::vector::reserve对容器的提示是启用较少的重新分配。实际上并没有使其v[5]有效。而是使用std::vector::resize()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用.sort对数字数组进行排序,但我不知道为什么这样做

来自分类Dev

函数已执行,我不知道为什么

来自分类Dev

C ++我不知道在使用回调函数时使用“ this”和“ std :: placeholders :: _ 1”意味着什么

来自分类Dev

我不知道为什么我的适配器未更新我的recyclerview

来自分类Dev

插入运算符不适用于向量,我不知道为什么

来自分类Dev

简单尝试对内联进行排序会返回无,不知道为什么

来自分类Dev

我的for嵌套循环未执行,我也不知道为什么

来自分类Dev

我不知道为什么我得到AttributeError

来自分类Dev

我的陈述无效,我也不知道为什么

来自分类Dev

我不知道为什么我得到AttributeError

来自分类Dev

在 F# 中映射自动排序,但我不知道为什么

来自分类Dev

compareTo正在要求我进行投射,但我不知道为什么

来自分类Dev

执行任务的角函数我不知道为什么是这个函数

来自分类Dev

我不知道为什么我得到“未定义的不是函数”错误

来自分类Dev

我的质数函数坏了,但我不知道为什么

来自分类Dev

VLOOKUP给#N / A我不知道为什么

来自分类Dev

无限循环我不知道为什么(DichotomySearch)

来自分类Dev

PyGame不断崩溃,我不知道为什么

来自分类Dev

C ++ Segfault,我不知道为什么?

来自分类Dev

VLOOKUP给#N / A我不知道为什么

来自分类Dev

声音消失了,我不知道为什么

来自分类Dev

df挂起,我不知道为什么

来自分类Dev

不知道为什么我的python输出循环

来自分类Dev

标题缩进,我不知道为什么

来自分类Dev

不知道为什么我收到 StopIteration 错误

来自分类Dev

SMTP 错误,我不知道为什么

来自分类Dev

不知道为什么我的 cursorForObjectInConnection 返回 null?

来自分类Dev

算法停止,我不知道为什么

来自分类Dev

Python函数正在更改输入的值,但我不知道为什么

Related 相关文章

  1. 1

    使用.sort对数字数组进行排序,但我不知道为什么这样做

  2. 2

    函数已执行,我不知道为什么

  3. 3

    C ++我不知道在使用回调函数时使用“ this”和“ std :: placeholders :: _ 1”意味着什么

  4. 4

    我不知道为什么我的适配器未更新我的recyclerview

  5. 5

    插入运算符不适用于向量,我不知道为什么

  6. 6

    简单尝试对内联进行排序会返回无,不知道为什么

  7. 7

    我的for嵌套循环未执行,我也不知道为什么

  8. 8

    我不知道为什么我得到AttributeError

  9. 9

    我的陈述无效,我也不知道为什么

  10. 10

    我不知道为什么我得到AttributeError

  11. 11

    在 F# 中映射自动排序,但我不知道为什么

  12. 12

    compareTo正在要求我进行投射,但我不知道为什么

  13. 13

    执行任务的角函数我不知道为什么是这个函数

  14. 14

    我不知道为什么我得到“未定义的不是函数”错误

  15. 15

    我的质数函数坏了,但我不知道为什么

  16. 16

    VLOOKUP给#N / A我不知道为什么

  17. 17

    无限循环我不知道为什么(DichotomySearch)

  18. 18

    PyGame不断崩溃,我不知道为什么

  19. 19

    C ++ Segfault,我不知道为什么?

  20. 20

    VLOOKUP给#N / A我不知道为什么

  21. 21

    声音消失了,我不知道为什么

  22. 22

    df挂起,我不知道为什么

  23. 23

    不知道为什么我的python输出循环

  24. 24

    标题缩进,我不知道为什么

  25. 25

    不知道为什么我收到 StopIteration 错误

  26. 26

    SMTP 错误,我不知道为什么

  27. 27

    不知道为什么我的 cursorForObjectInConnection 返回 null?

  28. 28

    算法停止,我不知道为什么

  29. 29

    Python函数正在更改输入的值,但我不知道为什么

热门标签

归档