Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何修复此排序?_C++_Visual Studio_Visual C++_Visual C++ 2010 - Fatal编程技术网

C++ 如何修复此排序?

C++ 如何修复此排序?,c++,visual-studio,visual-c++,visual-c++-2010,C++,Visual Studio,Visual C++,Visual C++ 2010,使用Knuth优化的快速排序,其中快速排序操作所有分区>k元素。数组以这种方式进行部分排序,然后调用单个插入排序来优化结果 使用pivot=x[随机元素] 给定具有以下格式的输入文件“abc.txt”,读入数据并使用上述排序对其进行排序。第一个数字是要排序的元素数 输入“abc.txt”为: 输出应该是一行升序数字 这是我的代码,但它不起作用。任何人都可以给我帮助: #include <fstream> #include <iostream> #include <r

使用Knuth优化的快速排序,其中快速排序操作所有分区>k元素。数组以这种方式进行部分排序,然后调用单个插入排序来优化结果

使用pivot=x[随机元素]

给定具有以下格式的输入文件“abc.txt”,读入数据并使用上述排序对其进行排序。第一个数字是要排序的元素数

输入“abc.txt”为:

输出应该是一行升序数字

这是我的代码,但它不起作用。任何人都可以给我帮助:

#include <fstream>
#include <iostream>
#include <random>
using namespace std;

void Qsort(int a[], int low, int high)
{
    if(low >= high)
    {
        return;
    }
    int first = low;
    int last = high;
    int key = a[(rand() % (last - first + 1)) + first];

    while(first < last)
    {
        while(first < last && a[last] >= key)
        {
            --last;
        }

        a[first] = a[last];

        while(first < last && a[first] <= key)
        {
            ++first;
        }

        a[last] = a[first];    

    }
    a[first] = key;
    Qsort(a, low, first-1);
    Qsort(a, first+1, high);
}
int main()
{
    std::fstream myfile("C:\\abc.txt", std::ios_base::in);
    int y = 0;

    myfile >> y;
    int a[100000] = {}; 

    for (int i = 0; i < y; i++) {
        myfile >> a[i];
    }

    Qsort(a, 0, y-1); 
    for(int i = 0; i < y ; i++)
    {
        cout << a[i] << " ";
    }
    system("pause"); 
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效Qsort(整数a[],整数低,整数高)
{
如果(低>=高)
{
返回;
}
int first=低;
int last=高;
int key=a[(rand()%(last-first+1))+first];
while(第一次<最后一次)
{
while(first=键)
{
--最后;
}
a[第一次]=a[最后一次];
while(firsty;
int a[100000]={};
for(int i=0;i>a[i];
}
Qsort(a,0,y-1);
for(int i=0;icout很可能,您给了
myfile
一个不正确的路径。我自己测试了代码,它按照我的预期工作。如果在正确的位置没有
abc.txt
,程序将运行并且没有输出。我假设这就是您正在经历的,尽管“不工作”有点模糊

如果您在
C:
中确实有一个
abc.txt
,我的下一个猜测是,您的读取请求会因为没有访问文件的正确权限而被操作系统拒绝。请尝试将文件放在您的documents文件夹中


另外,
rand
位于
,而不是
。虽然看起来
肯定会包括
,但我不相信这一点。您还需要使用
srand
,或者(作为更好的选择)研究如何使用
。一开始它比较复杂,但要好得多。

std::sort
非常好。通常比DIY解决方案好,而且工作量肯定更少。无论如何,我认为这个问题应该在代码审查网站上,而不是在这里,因为它与语言无关,完全与代码的实现有关算法。我建议对问题进行以下改进(无论是否移动):(1)详细说明“不起作用”,提供示例输入、预期输出、实际输出;(2)对于第一部分,说明它是作业文本,并将其设为块引号,每行开头都有一个按钮,但本质上是一个
;(3)删除注释外的代码,因为它没有作用,只是噪音。你的问题相当于“我的代码不起作用”,这是远远不够的。此外,我不认为你真的是Angelababy,尽管她很酷。路径就是你的文件所在的位置。假设你的操作系统有类似Windows的功能,打开“abc.txt”的属性并复制完整的路径应该可以解决它。只需将每个“\”替换为“\ \”并使用该路径,而不是现在程序中的路径。
#include <fstream>
#include <iostream>
#include <random>
using namespace std;

void Qsort(int a[], int low, int high)
{
    if(low >= high)
    {
        return;
    }
    int first = low;
    int last = high;
    int key = a[(rand() % (last - first + 1)) + first];

    while(first < last)
    {
        while(first < last && a[last] >= key)
        {
            --last;
        }

        a[first] = a[last];

        while(first < last && a[first] <= key)
        {
            ++first;
        }

        a[last] = a[first];    

    }
    a[first] = key;
    Qsort(a, low, first-1);
    Qsort(a, first+1, high);
}
int main()
{
    std::fstream myfile("C:\\abc.txt", std::ios_base::in);
    int y = 0;

    myfile >> y;
    int a[100000] = {}; 

    for (int i = 0; i < y; i++) {
        myfile >> a[i];
    }

    Qsort(a, 0, y-1); 
    for(int i = 0; i < y ; i++)
    {
        cout << a[i] << " ";
    }
    system("pause"); 
    return 0;
}