課本中的作業問題「修改第9.6節的qsort.c程序,使低,高和中指向數組元素而不是整數,指針分裂並不需要返回一個指針整數。」C編程修改快速排序
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語言編程和我不能確定如何正確地做這個工作方案。這種嘗試成功地進行了調試,但是它只是在不對其進行排序的情況下吐出輸入。任何幫助表示讚賞。
非常感謝,這使得更多的意義 – 2013-05-11 17:34:23
不客氣。 – BLUEPIXY 2013-05-11 17:52:27