在对完全降序的数组进行排序时,快速排序效率低下是正常的吗?

时间:2018-09-13 23:36:39

标签: algorithm sorting

#include <iostream>
#include<stdio.h>
#include<fstream>
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high- 1; j++)
{
    if (arr[j] <= pivot)
    {
        i++;
        swap(&arr[i], &arr[j]);
    }
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
    int pi = partition(arr, low, high);
    quickSort(arr, low, pi - 1);
    quickSort(arr, pi + 1, high);
}
}
int main()
{
int arr[100000];
int i;
ifstream fin;
         int n = 20000;
fin.open("reverse20k.txt");
if(fin.is_open())
{
    for(i=0;i<n;i++)
        fin>>arr[i];
}
quickSort(arr, 0, n-1);
return 0;
}

排序一个20k的纯降序数组需要大约1.25秒,而合并排序仅需0.05。快速排序在对降序数组进行排序时是否效率极低,还是算法有问题?

0 个答案:

没有答案