[考研真题答案] 用 C 语言编写算法实现以平均值为界值(枢轴)的快速排序方法

文章目录

  • 一、背景
  • 二、分析
    • 1. 什么是快排(Quicksort)?
    • 2. 快排怎么做?
  • 三、代码部分
  • 四、结果

一、背景

某校考研真题:
2. 快速排序算法中,如何选取一个界值(又称为轴元素),影响着快速排序的效 率,而且界值也并不一定是被排序序列中的一个元素。例如,我们可以用被排序序列中所有元素的平均值作为界值。 用 C 语言编写算法实现以平均值为界值 的快速排序方法(注:待排序数据存储在数组 R[ ]中, 数组最小下标为 S,数组最大下标为 T)。

二、分析

1. 什么是快排(Quicksort)?

引入wiki定义:

Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm. Developed by British computer scientist Tony Hoare in 1959[1] and published in 1961,[2] it is still a commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort.[3][contradictory]
Quicksort is a divide-and-conquer algorithm. It works by selecting a ‘pivot’ element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. This can be done in-place, requiring small additional amounts of memory to perform the sorting.
Quicksort is a comparison sort, meaning that it can sort items of any type for which a “less-than” relation (formally, a total order) is defined. Efficient implementations of Quicksort are not a stable sort, meaning that the relative order of equal sort items is not preserved.
Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.

简而言之,快排是一个基于分治思想与比较的非稳定排序算法。

2. 快排怎么做?

同样引入wiki:

Quicksort is a divide and conquer algorithm. It first divides the input array into two smaller sub-arrays: the low elements and the high elements. It then recursively sorts the sub-arrays. The steps for in-place Quicksort are:
Pick an element, called a pivot, from the array.
Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
The base case of the recursion is arrays of size zero or one, which are in order by definition, so they never need to be sorted.
The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm’s performance.

引入这段,主要是看到其他某位校友的博客下面,有人提出:“每一趟排序都应该有一个元素到达最终位置。”

具体请看:

  1. After this partitioning, the pivot is in its final position. //这里的this partitioning 根据前文,就是最传统的快排。

  2. The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm’s performance.
    //这句话提到,pivot的选择,极大地影响了算法的表现。

综上,这是传统快排的一个特性,或者说是某几种排序方法的特性,而不是快排的定义。所以我认为,非传统的快排是可以失去这种特性的。

三、代码部分


#include
using namespace std;
int len = -1;//全局变量
void swap(int *a, int *b)
{
	//交换的方法很多,答题的时候最好不要炫技
	int temp = *a;
	*a = *b;
	*b = temp;
	return;
}

void printArray(int arr[],int len)
{
	for (int i = 0; i < len;++i)
	{
		cout << arr[i] << ' ';
	}
	cout<<endl;
	return;

}

double getMean(int arr[],int low,int high)
{
    
    int temp=0;
    for (int i = low; i <= high;++i)
	{
        temp += arr[i];
    }
    //用Double是为了保留精度,尽量遵守“平均值”的概念,用int也可以
    return temp / (double)(high-low+1);
}

void quickSortByMean(int arr[], int low, int high)
{
    double mean = getMean(arr, low,high);
    int i = low, j = high;
    if(low<high)//保证待处理序列>1
    {
        while (i<j)
        {
            /* code */
            while (j>i&&arr[j]>mean)
            {
                /* code */
                --j;
            }
            while (j>i&&arr[i]<mean)
            {
                /* code */
                ++i;
            }
            if(i<j)
            {
                swap(&arr[i], &arr[j]);
                --j;
                ++i;
            }
        }

        if(low<j&&i<high)
        {
            
            quickSortByMean(arr,low,j);
            
            quickSortByMean(arr,i,high);
        }
		
    }
    printArray(arr, len);
}



int main()
{
    cout << "Please input number of elem: " << endl;
    
    cin >> len;
  
    cout << "Please input elements separated by Spaces: " << endl;
    int *arr = new int[len];

    
    for (int i = 0; i < len;++i)
    {
        cin >> arr[i];
    }
    
    cout << "Original arr is :" << endl;
    printArray(arr, len);

    double mean = getMean(arr,0,len-1);
    cout << "Mean is " << mean << endl;

    cout << "QuickSort is statr..." << endl <<  "------------------------" << endl;

    quickSortByMean(arr, 0, len-1);
    
    cout << "QuickSort is end..." << endl <<  "------------------------" << endl;
    printArray(arr, len);
}


*/

四、结果

这份代码在VSCode + C++11的标准下运行成功,如果您发现有错,欢迎指正。
答题的时候没必要写这么臃肿,主要是为了跑出来方便分析。

  1. example 1
    [考研真题答案] 用 C 语言编写算法实现以平均值为界值(枢轴)的快速排序方法_第1张图片
  2. example 2
    [考研真题答案] 用 C 语言编写算法实现以平均值为界值(枢轴)的快速排序方法_第2张图片

你可能感兴趣的:(Note)