【发布时间】:2021-10-13 01:00:51
【问题描述】:
我正在努力在我的快速排序程序执行时显示排序的中间步骤。
在 Essence 中,每次迭代后,控制台窗口都必须显示正在进行的数组排序的当前情况。
我能够在我的程序中添加交换和比较的总计数,但无法找到在现有代码中添加逻辑的方法,从而程序还显示每次迭代后的中间步骤。
#include <cstdlib>
#include <iostream>
using namespace std;
void quick_sort(int C[],int low,int high, int & quick_count);
void partition ( int C[], int low, int high, int &m, int &n, int & quick_count );
void swap(int* a, int* b);
int main()
{
int num_of_items;
int quick_count = 0;
cout<<"Enter The Number Of Elements To Be Sorted: ";
cin>>num_of_items;
int quick[num_of_items];
for(int i=0;i<num_of_items;i++)
{
cout<<"Element "<<i<<": ";
cin>>quick[i];
}
cout<<endl;
cout<<"Unsorted: "<<endl;
for(int i=0;i<num_of_items;i++)
cout<<quick[i]<<endl;
cout<<"----------------------------------------"<<endl<<endl;
cout<<"Sorted: "<<endl;
quick_sort(quick,0,num_of_items-1, quick_count);
for(int i=0;i<num_of_items;i++)
cout<<quick[i]<<endl;
cout<<"Quick sort count: "<<quick_count<<endl;
}
//preconditions: an array of integers is passed to the function,an integer that represents the low value, an integer that
// represents the high value in the array, an integer that is passed by reference that acts as the step counter for the sort
//postcondition: the array is sorted and the step counter is maintained back to the main since it was passed by reference.
void quick_sort (int C[], int low, int high, int & quick_count )
{
int m, n;
if ( low < high )
{
partition ( C, low, high, m, n, quick_count );
quick_sort ( C, low, m, quick_count );
quick_sort ( C, n, high, quick_count );
}
}
//preconditions: an array of integers is passed to the function,an integer that represents the low value,an integer that
// represents the mid value in a section and an integer that represents the high value in the array,
// an integer that is passed by reference that acts as marker for one section, an integer that is passed by reference that acts as another marker,
// and an integer that is passed by reference that acts as the step counter for the sort.
//Postconditions: The array is shifted into the partitions that make the quick sort function.
void partition ( int C[], int low, int high, int &m, int &n, int & quick_count)
{
int pivot = C[low];
int lastS1 = low - 1;
int firstU = low;
int firstS3 = high + 1;
while ( firstU < firstS3 )
{
quick_count++;
if ( C[firstU] < pivot ) // S1
{
++lastS1;
swap ( C[firstU],C[lastS1] );
++firstU;
}
else if ( C[firstU] == pivot ) // S2
{++firstU;}
else // C[firstU] > pivot // S3
{
--firstS3;
swap ( C[firstU], C[firstS3] );
}
}
m = lastS1;
n = firstS3;
}
//preconditions: two integer pointer variables are passed to the function
//postconditions: The values that are pointed to by the pointers, swap address locations.
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
就特定于所需的输出而言,
我正在尝试如下,
说大小为 5 的数组
初始数组:12,3,54,6,32,87
迭代 1:
迭代 2: ....
....
....
等等……
直到
排序数组:3,6,12,32,64,87
【问题讨论】:
-
我的建议是选择要打印行的特定级别(可能基于数组的总长度)。然后,只要您对恰好包含 n 个元素的部分进行了排序,您就会打印出一个新数组(如果数组末尾不是 n 的倍数,则可能对数组末尾进行特殊处理)。然后,您甚至可以在您刚刚排序的(n 个元素的)部分下划线。另一种选择——如果你想走那么远——将是一个 GUI,你可以突出显示当前排序的数组,并突出显示每两个交换的元素。
-
我可能会在
partition()返回之前打印出整个输入数组以及low和high。 --- 由于快速排序确实分而治之,它本身并没有真正进行迭代。
标签: c++ algorithm sorting quicksort divide-and-conquer