我正在使用快速排序来对FFT函数中的数据进行排序,因此我可以使用四分位数范围来查找离群值。目前,我不确定为什么通过quicksort的数据没有真正分类。下面是我使用的功能(修改为使用双打):Quicksort无法处理小数字?
void quickSort(double arr[], int left, int right) {
int i = left, j = right;
int tmp;
double pivot = arr[(left + right)/2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
我不知道如何把输出在这里,因为它是相当长的。这里是如何将所有的未排序的数据几乎是这样的:
0.01033861 0.00861337 0.00861337 -0.00326733 -0.00326733 0.00098514 0.00098514 -0.01022199 -0.01022199 -0.00303045 -0.00303045 -0.00435644 -0.00435644 -0.00217089 -0.00217089 -0.00171707 -0.00171707 -0.00073572 -0.00073572 -0.00283767 -0.00283767 0.00008432 0.00008432 -0.00288364 -0.00288364 -0.00162750 -0.00162750 -0.00222617 -0.00222617 -0.00017057 -0.00017057 0.00101272 0.00101272 0.00332283 0.00332283 -0.00115711 -0.00115711
它似乎并不像排序是正确的,因为大部分产量由非常小的数据(0.00000000)的,其余的有斑点,看起来像这样:
0.00000000 0.00000000 -0.00002053 0.00000000 -0.00002051 -0.00002051 -0.00002050 0.00000000 -0.00002048 0.00000000 -0.00002045 -0.00002039 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 -0.00002025 0.00000000 -0.00002020 0.00000000 -0.00002019 0.00000000 0.00000000 -0.00002005 0.00000000
这里我为了获得这项输出:
int size = sizes[i];
int numElements = (int)pow(2.0, ceil(log((double)size)/log(2.0))); //next power of 2
double *X = (double *) malloc((2*numElements+1) * sizeof(double));
double *p = ptr[i]; //ptr[i] is a void *ptr;
//X is filled with data
for (j = 0; j < size; j++){ //put numbers in
if ((double)*(p+j) < 1000 && (double)*(p+j) > -1000) {
X[2*j+1] = (double)*(p+j);
} else{
X[2*j+1] = 0.0;
}
X[2*j+2] = 0.0;
}
for (j = size; j < numElements; j++){ //fill the rest with zeros
X[2*j+1] = 0.0;
X[2*j+1] = 0.0;
}
printf("\nStarting FFT()..."); fflush(stdout);
four1(X, numElements, 1, pData);
for (j = 0; j < numElements; j++){
//first block of data is printed
fprintf(pData->pFile, "%.8f %.8f ", X[2*j+1], X[2*j+1]);
}
//create a copy of the array for storage
double *array = (double *) malloc((maxIndex-minIndex+1) * sizeof(double));
for (j = 0; j < maxIndex-minIndex+1; j++){
array[j] = X[2*(j+minIndex)+1];
}
quickSort(X, 1, 2*(long)size+2); //don't need to sort everything
//print out the output of the quicksort
for (j = 1; j < 2*(long)size+2; j++){
//second block of data is printed
fprintf(pData->pFile2, "%.8f ", X[j]);
}
//use interquartile range
double q1, q3, iqr;
q1 = X[(long)size/2+1]; //size is even
q3 = X[3*(long)size/2+1];
iqr = q3-q1;
printf("q1: %.5f, q3: %.5f, iqr: %.5f", q1, q3, iqr);
//check if any of the elements in array[] are outliers
for (j = 0; j < maxIndex-minIndex+1; j++) {
if (array[j] > 3*(q3+iqr)/2){
printf(" A match!"); fflush(stdout);
break;
}
}
为什么不选它应该工作?
您需要直接在问题中发布代码,而不是通过链接。 – skrrgwasme
我不知道你发布的数据甚至意味着什么,但是如果你的排序算法输出的是不同于输入的数据,那么它就会被破坏。显然,如果我们看不到**你的**代码,我们不能告诉你什么是坏的。在某些网站上找到的示例代码不计算在内。 –
有一个名为'qsort'的库函数,以防你错过了它。 – user3386109