具有双*值的快速排序 [英] Quicksort with double* values
问题描述
我得到了一个用C编写的程序,该程序在具有int值的数组上实现Quicksort。我需要将其转换为将对具有double *值的数组执行Quicksort的程序。我以为我只需要将 int声明更改为 double *,但是由于某些原因,当我用非整数值测试数组时,该程序不再起作用。
I am given a programme in C which implements Quicksort on arrays with int values. I need to convert it into a programme which will implement Quicksort on arrays with double* values. I thought that I just need to change "int" declarations into "double*", but for some reason the programme no longer works when I test arrays with other values than integers.
有人可以帮忙吗?我真的对C语言编程一无所知,也不知道如何进行。这是 int程序:
Can someone please help? I really know almost nothing about programming in C and have no idea how to go on. Here is the "int" programme:
void quicksort(int a[], int n)
{
if (n <= 1) return;
int p = a[n/2];
int b[n], c[n];
int i, j = 0, k = 0;
for (i=0; i < n; i++) {
if (i == n/2) continue;
if ( a[i] <= p) b[j++] = a[i];
else c[k++] = a[i];
}
quicksort(b,j);
quicksort(c,k);
for (i=0; i<j; i++) a[i] =b[i];
a[j] = p;
for (i= 0; i<k; i++) a[j+1+i] =c[i];
}
int main(void) {
int i;
/* das Array zum Sortieren */
int test_array[] = { 5, 2, 7, 9, 6, 4, 3, 8, 1 };
int N = sizeof(test_array)/sizeof(int);
quicksort(test_array, N);
for(i = 0; i < N; i++)
printf("%d ", test_array[i]);
printf("\n");
return 0;
}
推荐答案
当您替换 int
和 double
指针,则需要更改比较-比较指向的值,而不是指针本身。
When you replace the int
s with double
pointers, you need to change the comparisons - to compare the pointed to values, not the pointers themselves.
这篇关于具有双*值的快速排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!