gpt4 book ai didi

c++ - 快速排序不适用于 10k 元素

转载 作者:行者123 更新时间:2023-11-30 01:23:19 24 4
gpt4 key购买 nike

我的老师给了我一个快速排序函数来使用和测试执行时间,但是当它到达一个包含 10,000 个元素的列表时它会抛出堆栈溢出,我不明白为什么。我在几台不同的计算机上对其进行了测试,结果相同,在 10,000 个元素中解析了大约 9375 个元素。

快速排序文件

#include "swap.h"

/** Chooses a pivot for quicksort's partition algorithm and swaps
* it with the first item in an array.
* @pre theArray[first..last] is an array; first <= last.
* @post theArray[first] is the pivot.
* @param theArray The given array.
* @param first The first element to consider in theArray.
* @param last The last element to consider in theArray. */
void choosePivot(int theArray[], int first, int last){
//cerr << "choosePivot(array, " << first << ", " << last << ")\n";
int mid = (last - first) / 2;
if( (theArray[first] <= theArray[mid] &&
theArray[mid] <= theArray[last]) ||
(theArray[last] <= theArray[mid] &&
theArray[mid] <= theArray[first]) ){
// value at mid index is middle of values at first and last indices
swap(theArray[first], theArray[mid]);
}else if( (theArray[first] <= theArray[last] &&
theArray[last] <= theArray[mid]) ||
(theArray[mid] <= theArray[last] &&
theArray[last] <= theArray[first])){
// value at last index is middle of values
swap(theArray[first], theArray[last]);
}
}


/** Partitions an array for quicksort.
* @pre theArray[first..last] is an array; first <= last.
* @post Partitions theArray[first..last] such that:
* S1 = theArray[first..pivotIndex-1] < pivot
* theArray[pivotIndex] == pivot
* S2 = theArray[pivotIndex+1..last] >= pivot
* @param theArray The given array.
* @param first The first element to consider in theArray.
* @param last The last element to consider in theArray.
* @param pivotIndex The index of the pivot after partitioning. */
void partition(int theArray[],
int first, int last, int& pivotIndex){
// place pivot in theArray[first]
choosePivot(theArray, first, last);
int pivot = theArray[first]; // copy pivot

// initially, everything but pivot is in unknown
int lastS1 = first; // index of last item in S1
int firstUnknown = first + 1; // index of first item in
// unknown

// move one item at a time until unknown region is empty
for (; firstUnknown <= last; ++firstUnknown)
{ // Invariant: theArray[first+1..lastS1] < pivot
// theArray[lastS1+1..firstUnknown-1] >= pivot

// move item from unknown to proper region
if (theArray[firstUnknown] < pivot)
{ // item from unknown belongs in S1
++lastS1;
swap(theArray[firstUnknown], theArray[lastS1]);
} // end if

// else item from unknown belongs in S2
} // end for

// place pivot in proper position and mark its location
swap(theArray[first], theArray[lastS1]);
pivotIndex = lastS1;
} // end partition

/** sorts the items in an array into ascending order.
* @pre theArray[first..last] is an array.
* @post theArray[first..last] is sorted.
* @param theArray The given array.
* @param first The first element to consider in theArray.
* @param last The last element to consider in theArray. */
void quicksort(int theArray[], int first, int last){
int pivotIndex;

if (first < last)
{ // create the partition: S1, pivot, S2
partition(theArray, first, last, pivotIndex);

// sort regions S1 and S2
quicksort(theArray, first, pivotIndex-1);
quicksort(theArray, pivotIndex+1, last);
} // end if
} // end quicksort

swap.h 文件

#ifndef _SWAP_H
#define _SWAP_H

/** Swaps two items.
* @pre x and y are the items to be swapped.
* @post Contents of actual locations that x and y represent are
* swapped.
* @param x Given data item.
* @param y Given data item. */
void swap(int& x, int& y){
int temp = x;
x = y;
y = temp;
} // end swap

#endif /* _SWAP_H */

和实现文件

//main.cpp
//Angelo Todaro
//Main driverto clock the timing efficiency of different sort algorithms for different sized lists

#include "quickSort.cpp"
#include <iostream>
#include <time.h>
using namespace std;

double diffclock(clock_t,clock_t);

int main(){
clock_t begin, end;//clocks to store number of ticks at beginning and end
srand(time(NULL));//initialize seed
cout << "# of Elements\tQuick\n";
for(int n = 10; n < 100000; n*=10){
int* array = new int[n];
cout << n << "\t\t";
for(int i =0; i < n; i++){
array[i]=rand()%1000;
}


//quick sort
begin=clock();
quicksort(array,0,n);
end=clock();
cout << diffclock(end,begin) << "\t";

}

return 0;
}


double diffclock(clock_t clock1, clock_t clock2){
double diffticks = clock1-clock2;//finds difference between ticks
double diffmili=diffticks/CLOCKS_PER_SEC;//turns tickes into miliseconds
return diffmili;
}

最佳答案

这是一个递归快速排序实现,也没有选择很好的主元。给定一些输入,它可能会对每个元素进行函数调用。堆栈上的 10k 次调用很难处理。在这一点上,只有具有随机主元的迭代就地快速排序才是一个好的算法。

关于c++ - 快速排序不适用于 10k 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15165625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com