当中间元素选择为枢轴时,快速排序C 不起作用
Quick Sort in C++ not working when middle element chosen as pivot
我尝试在C 中实现快速排序。我面临一个问题。如果我任意选择枢轴作为第一个或最后一个元素,则程序运行良好,但是如果我选择中间元素作为枢轴(((beg end)/2),则输出不是完全正确的。大多数元素按顺序排序,只有一些是随机的,不正确的位置。以下是我的代码:
#include <iostream>
#include <cstdlib>
#include <chrono>
#include <fstream>
using namespace std;
using namespace std::chrono;
void quickSort(int[], int, int);
void print(int);
void print(int n) //prints 50 random numbers in a file
{
ofstream of("List.txt");
int i;
for (i = 0; i < n; i++)
{
int x = rand() % 50 + 1;
of << x << endl;
}
of.close();
}
int sortf() //calls the quicksort function and sends it array of elements which
{ //were previously stored in the file and outputs sorted values to another file
int arr[50];
int n = 50;
ifstream f("List.txt");
int counter = 0;
int i;
while (!f.eof() && counter < 50)
{
f >> arr[counter];
counter++;
}
quickSort(arr, 0, 49);
ofstream of("ListOut.txt");
for (i = 0; i < n; i++)
{
of << arr[i] << endl;
}
}
void quickSort(int arr[], int start, int end) //applies quicksort algorithm
{
if (start < end)
{
int a = start;
int b = end;
int p = arr[(a + b) / 2];
int x = (a + b) / 2;
int temp;
while (a < b)
{
while (arr[b] > p)
{
b--;
}
while (arr[a] <= p && a <= b)
{
a++;
}
if (a < b)
{
temp = arr[b]; //swapping left and right position elements
arr[b] = arr[a];
arr[a] = temp;
}
}
temp = arr[b]; //bringing pivot in the middle, so that
arr[b] = arr[x]; //elements smaller than pivot are to the left
arr[x] = temp; //and elements greater than pivot are to the right
quickSort(arr, start, b - 1);
quickSort(arr, b + 1, end);
}
}
int main()
{
print(50); //printing 50 numbers in the file
high_resolution_clock::time_point t1 = high_resolution_clock::now();
sortf();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(t2 - t1).count();
cout << "nTime taken:n" << duration; //outputs time taken for input, sorting and output.
}
执行后的输出文件包含以下元素列表:
3 16 7 9 25 12 10 12 13 14 24 18 13 16 18 18 21 19 20 20 22 22 23 23 24 27 27 28 30 28 30 30 31 33 34 35 36 41 36 37 37 37 38 41 43 43
44 44 49 50
请帮助我纠正我的代码,因为我在这个小问题上浪费了很多小时。
我使用调试器自己找出解决方案。请参阅大写字母中的评论,以了解为纠正代码所做的更改。以下是用于10000输入值的QuickSort实现的工作代码:
#include <iostream>
#include <cstdlib>
#include <chrono>
#include <fstream>
using namespace std;
using namespace std::chrono;
void quickSort(int[], int, int);
void print(int);
void print(int n) //prints 50 random numbers in a file
{
ofstream of("List.txt");
int i;
for (i = 0; i < n; i++)
{
int x = rand() % 10000 + 1;
of << x << endl;
}
of.close();
}
void sortf(int x) //calls the quicksort function and sends it array of elements which
{ //were previously stored in the file and outputs sorted values to another file
int arr[x];
int n = x;
ifstream f("List.txt");
int counter = 0;
int i;
while (!f.eof() && counter < x)
{
f >> arr[counter];
counter++;
}
quickSort(arr, 0, x-1);
ofstream of("ListOut.txt");
for (i = 0; i < n; i++)
{
of << arr[i] << endl;
}
}
void quickSort(int arr[], int start, int end) //applies quicksort algorithm
{
if (start < end)
{
int a = start;
int b = end;
int p = arr[(a + b) / 2];
int x = (a + b) / 2;
int temp;
while (a < b)
{
while (arr[b] > p)
{
b--;
}
while (arr[a] <= p && a <= b)
{
a++;
}
if (a < b)
{
temp = arr[b]; //swapping left and right position elements
arr[b] = arr[a];
arr[a] = temp;
}
}
arr[b] = p; //CHANGED THIS ***************
quickSort(arr, start, b - 1);
quickSort(arr, b + 1, end);
}
}
int main()
{
print(10000); //printing 50 numbers in the file
high_resolution_clock::time_point t1 = high_resolution_clock::now();
sortf(10000);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(t2 - t1).count();
cout << "nTime taken:n" << duration; //outputs time taken for input, sorting and output.
}
感谢您提供的有用建议。非常感谢你们坚持使用调试器。学到了一些新的东西,并且确实有用,现在我的代码效果很好,用于10000个值的输入。欢呼!
相关文章:
- 排序不起作用 c++
- 在 C++ 中使用 10 个随机整数的数组进行气泡排序不起作用
- 当中间元素选择为枢轴时,快速排序C 不起作用
- 插入排序不起作用
- 分而治之合并排序不起作用
- C 排序字符串的动态阵列 - 排序不起作用
- 为什么tha std :: list ::排序不起作用
- C++向量<Struct>排序不起作用
- 快速排序不起作用(分段错误)
- 使用qsort对字符串进行排序不起作用
- 快速排序算法不起作用
- 快速排序模板不起作用,并使我的程序无响应
- 自定义结构排序不起作用
- 为什么我的快速排序不起作用
- 快速排序算法不起作用
- 简单std::排序不起作用
- 为什么泡沫排序不起作用
- std::矢量排序不起作用
- 对向量进行排序不起作用
- 为什么这个基数排序不起作用