C++ 不确定是否使用快速排序
我正在为学校做一个项目,并获得了快速排序功能的代码。我们必须编写一个程序来比较通过4种不同排序方法进行的传递数和交换数。我有前三个工作,但我不确定为什么我的快速排序不工作,我相信这与我需要传递到函数的开始和停止位置有关。我已经尝试了我能想到的一切,有时它甚至不会返回快速排序的值,其他版本它只会返回0个循环和交换。下面是代码,此版本返回0计数C++ 不确定是否使用快速排序,c++,C++,我正在为学校做一个项目,并获得了快速排序功能的代码。我们必须编写一个程序来比较通过4种不同排序方法进行的传递数和交换数。我有前三个工作,但我不确定为什么我的快速排序不工作,我相信这与我需要传递到函数的开始和停止位置有关。我已经尝试了我能想到的一切,有时它甚至不会返回快速排序的值,其他版本它只会返回0个循环和交换。下面是代码,此版本返回0计数 #include <iostream> #include <cstdlib> #include <array> #inc
#include <iostream>
#include <cstdlib>
#include <array>
#include <ctime>
#include <iomanip>
#include <cctype>
using namespace std;
int partition(int array[], int start, int stop);
// variables Bubble
long int loop_bubble = 0;
long int swap_bubble = 0;
// variables Selection
long int loop_selection = 0;
long int swap_selection = 0;
// variables ShellSort
long int loop_shell = 0;
long int swap_shell = 0;
// variables QuickSort
long int loop_quick = 0;
long int swap_quick = 0;
class AssortedSorter
{
private:
// Declaring array atleast 20001 large
int array_class[20001];
public:
void BubbleSort(int arr[], int);
void SelectionSort(int arr[], int);
void ShellSort(int array[], int sequence[], int start, int stop);
void QuickSort(int array[], int start, int stop);
};
int main()
{
// array initialization
int bubble_array[20001];
int selection_array[20001];
int shell_array[20001];
int quick_array[20001];
int sequence[] = { 364, 121, 40, 13, 4, 1, 0 };
int UserNumofNumbers;
int start = 0;
cout << "Please enter a number between 10 and 20,000.\n" << "Or enter '0' for default\n";
cin >> UserNumofNumbers;
if (UserNumofNumbers == 0)
{
UserNumofNumbers = 10000;
}
// Bubble Random Number Generator
{
srand(time(0));
for (int i = 0; i < UserNumofNumbers; i++)
bubble_array[i] = rand() % 1000000 + 1;
}
// Selection Random Number Generator
{
srand(time(0));
for (int i = 0; i < UserNumofNumbers; i++)
selection_array[i] = rand() % 1000000 + 1;
}
// Shell Random Number Generator
{
srand(time(0));
for (int i = 0; i < UserNumofNumbers; i++)
shell_array[i] = rand() % 1000000 + 1;
}
// Quick Random Number Generator
{
srand(time(0));
for (int i = 0; i < UserNumofNumbers; i++)
quick_array[i] = rand() % 1000000 + 1;
}
// test object
AssortedSorter test;
// member function calls and outputs
test.BubbleSort(bubble_array, UserNumofNumbers);
cout << "The Bubble sorter had " << loop_bubble << " loops and " << swap_bubble << " swaps.\n";
test.SelectionSort(selection_array, UserNumofNumbers);
cout << "The Selection sorter had " << loop_selection << " loops and " << swap_selection << " swaps.\n";
test.ShellSort(shell_array, sequence, 1, UserNumofNumbers);
cout << "The Shell sorter had " << loop_shell << " loops and " << swap_shell << " swaps.\n";
test.QuickSort(quick_array, UserNumofNumbers, UserNumofNumbers);
cout << "The QuickSort sorter had " << loop_quick << " loops and " << swap_quick << " swaps.\n";
system("pause");
}
// function defenitions
// bubble
void AssortedSorter::BubbleSort(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < ((size) - (1)); count++)
{
loop_bubble += 1;
if (array[count] > array[count + 1])
{
swap_bubble += 1;
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// selection
void AssortedSorter::SelectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < ((size) - (1)); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
loop_selection += 1;
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
swap_selection += 1;
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
// ShellSort
void AssortedSorter::ShellSort(int array[], int sequence[], int start, int stop)
{
int step, i;
for (int step = 0; sequence[step] >= 1; step++)
{
int inc = sequence[step];
for (i = start + inc; i <= stop; i++)
{
int j = i;
int val = array[i];
while ((j >= start + inc) && val < array[j - inc])
{
loop_shell += 1;
array[j] = array[j - inc];
j -= inc;
}
swap_shell += 1;
array[j] = val;
}
}
}
// QuickSort
void AssortedSorter::QuickSort(int array[], int start, int stop)
{
int i, s = 0, stack[20001];
stack[s++] = start;
stack[s++] = stop;
while (s > 0)
{
stop = stack[--s];
start = stack[--s];
if (start >= stop)
continue;
i = partition(array, start, stop);
if (i - start > stop - i)
{
stack[s++] = start;
stack[s++] = i - 1;
stack[s++] = i + 1;
stack[s++] = stop;
}
else
{
stack[s++] = i + 1;
stack[s++] = stop;
stack[s++] = start;
stack[s++] = i - 1;
}
}
}
// Partitioning Definition
int partition(int array[], int start, int stop)
{
int up = start, down = stop - 1, part = array[stop];
if (stop <= start)
return start;
while (true)
{
while (array[up] < part)
loop_quick += 1;
up++;
while ((part < array[down]) && (up < down))
down--;
if (up >= down)
break;
swap_quick += 1;
swap(array[up], array[down]);
up++;
loop_quick += 1;
down--;
}
swap_quick += 1;
swap(array[up], array[stop]);
return up;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int分区(int数组[],int开始,int停止);
//变量气泡
长整数循环_bubble=0;
长整数交换_泡泡=0;
//变量选择
长整数循环_选择=0;
长整数交换_选择=0;
//变量外壳排序
长整数循环_shell=0;
long int swap_shell=0;
//变量快速排序
长整数循环_quick=0;
long int swap_quick=0;
分类分拣机
{
私人:
//声明数组至少为20001大
int数组_类[20001];
公众:
void BubbleSort(int-arr[],int);
无效选择排序(int arr[],int);
void shell排序(int数组[],int序列[],int开始,int停止);
无效快速排序(int数组[],int开始,int停止);
};
int main()
{
//数组初始化
int-bubble_数组[20001];
int选择_数组[20001];
int shell_数组[20001];
int quick_数组[20001];
int序列[]={364,121,40,13,4,1,0};
int UserNumofNumbers;
int start=0;
cout用户数;
if(UserNumofNumbers==0)
{
UserNumofNumbers=10000;
}
//气泡随机数发生器
{
srand(时间(0));
对于(int i=0;i