0

教科书“修改 9.6 节的 qsort.c 程序,使 low、high 和 middle 是指向数组元素而不是整数的指针。split 函数将需要返回一个指针而不是整数”。

qsort.c 代码:

#include <stdio.h>
#define N 10

void quicksort(int a[], int low, int high);

int split(int a[], int low, int high);

int main(void)

{

int a[N], i;

printf("Enter %d numbers to be sorted: :", N);

for (i=0; i<N; i++)

    scanf("%d", &a[i]);

    quicksort(a, 0, N - 1);

    printf("In sorted order: ");
    for (i=0; i<N; i++)
        printf("%d ", a[i]);
    printf("\n");

    return 0;
}

void quicksort(int a[], int low, int high)
{
    int middle;

    if (low >= high) return;
    middle = split(a, low, high);
    printf("Low");
    printf("%d", low);
    quicksort(a, low, middle - 1);
    quicksort(a, middle + 1, high);
}

int split(int a[], int low, int high)
{
    int part_element = a[low];

    for (;;) {
        while (low < high && part_element <= a[high])
            high--;
        if (low >= high) break;
        a[low++] = a[high];

        while (low < high && a[low] <= part_element)
            low++;
        if (low >= high) break;
        a[high--] = a[low];

    }

    a[high] = part_element;
    return high;
}

我对解决方案的尝试:

#include <stdio.h>

#define N 10

void quicksort(int a[], int *low, int *high);
int split(int a[], int *low, int *high);

int main(void)
{
int a[N], i;
int zero, nminus;
printf("Enter %d numbers to be sorted: :", N);
for (i=0; i<N; i++)
    scanf("%d", &a[i]);
    zero=0;
    nminus=N-1;
    quicksort(a, &zero, &nminus);

    printf("In sorted order: ");
    for (i=0; i<N; i++)
        printf("%d ", a[i]);
    printf("\n");

    return 0;
}

void quicksort(int a[], int *low, int *high)
{
    int *middle;
    int splitt;
    if (low >= high) return;
    splitt = split(a, low, high);
    middle = &splitt;

    quicksort(a, low, middle - 1);
    quicksort(a, middle + 1, high);
}

int split(int a[], int *low, int *high)
{
    int low1, high1;
    low1= *low;
    high1= *high;
    int part_element = a[low1];

    for (;;) {
        while (low1 < high1 && part_element <= a[high1])
            high1--;
        if (low1 >= high1) break;
        a[low1++] = a[high1];

        while (low1 < high1 && a[low1] <= part_element)
            low1++;
        if (low1 >= high1) break;
        a[high1--] = a[low1];

    }

    a[high1] = part_element;
    return high1;
}

我是 c 编程的新手,不确定如何使这个程序正常工作。此尝试成功调试,但它只是吐出输入而不对其进行排序。任何帮助表示赞赏。

4

3 回答 3

1

如果您选择顶部,然后复制并粘贴它,那么实际上必须进行以下更改:

每次比较高、低或中间时,只需保留 -- 或 ++,然后取消引用它。例如:

 a[high1--] = a[low1];

 *(high--) = *low. 

然后,您所要做的就是将指针传递给高、低和中间。

于 2013-05-10T18:52:53.357 回答
1

它说:

split 函数需要返回一个指针而不是整数。

int split(int a[], int *low, int *high);

返回一个指针而不是整数。

int *split(int a[], int *low, int *high);

于 2013-05-10T18:55:12.670 回答
0

也许像这样

#include <stdio.h>

#define N 10

void quicksort(int a[], int *low, int *high);
int* split(int a[], int *low, int *high);

int main(void)
{
    int a[N], i;
    printf("Enter %d numbers to be sorted: :", N);
    for (i=0; i<N; i++)
        scanf("%d", &a[i]);
    quicksort(a, &a[0], &a[N-1]);

    printf("In sorted order: ");
    for (i=0; i<N; i++)
        printf("%d ", a[i]);
    printf("\n");

    return 0;
}

void quicksort(int a[], int *low, int *high)
{
    int *middle;
    if (low >= high) return;
    middle = split(a, low, high);

    quicksort(a, low, middle - 1);
    quicksort(a, middle + 1, high);
}

int*  split(int a[], int *low, int *high)
{
    int part_element = *low;

    for (;;) {
        while (low < high && part_element <= *high)
            high--;
        if (low >= high) break;
        *low++ = *high;

        while (low < high && *low <= part_element)
            low++;
        if (low >= high) break;
        *high-- = *low;

    }

    *high = part_element;
    return high;
}

对应表

 when sizeof(int) is 4
 address | array a | index of array
 a(top)  |   a[0]  |        0
 a + 4   |   a[1]  |        1
 a + 8   |   a[2]  |        2
 ...

 &a[1] is (a + 4) : address of a[1]
 *(a+4) is a[1] : (a+4) is a + 1*sizeof(int), *(a+1) in c
于 2013-05-10T21:50:16.390 回答