对大数组进行排序,导致程序在运行时冻结



我正在对一个未知长度的数组进行排序。当我对长度小于2101的代码进行排序时,它的排序很好,但一旦我增加了这个数字,它就会停止排序。我不确定是因为我电脑的内存还是代码本身。对于排序,我使用快速排序算法。

如果有人能发现问题,下面是一份分类的副本

int partitionFunct(Person arr[], int low, int high) {
Person pivotVal=arr[low];
int i=low+1;
int j=high;
while(i<=j)
{
while(arr[i]<pivotVal&&i<=j)
i++;
while(pivotVal<arr[j]&&i<=j)
j--;
if((i<j))
{
Person temp=arr[i];
arr[i++]=arr[j];
arr[j--]=temp;
}
}
arr[low]=arr[j];
arr[j]=pivotVal;
return j;
}
void quickSortInternal (Person arr[], int low, int high) {
if (low >= high) return; 
int pivot=partitionFunct( arr,  low,  high);
quickSortInternal(arr,low,pivot-1);
quickSortInternal(arr,pivot+1,high);
}

请从以下位置尝试:http://www.cplusplus.com/reference/cstdlib/qsort/

拥有2000多个元素。

/* qsort example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort */
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++)
printf ("%d ",values[n]);
return 0;
}

你可能会感兴趣:尝试将qsort与矢量一起使用

最新更新