2013-05-10 101 views
0

課本中的作業問題「修改第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語言編程和我不能確定如何正確地做這個工作方案。這種嘗試成功地進行了調試,但是它只是在不對其進行排序的情況下吐出輸入。任何幫助表示讚賞。

回答

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 
+0

非常感謝,這使得更多的意義 – 2013-05-11 17:34:23

+0

不客氣。 – BLUEPIXY 2013-05-11 17:52:27

1

如果拿頂,複製和粘貼,下面的變化真的那麼必須進行:

每一個你比較高,低或中等的時間,只是不停的 - 或+ +然後解除引用它。例如:

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

*(high--) = *low. 

然後,所有你需要做的就是通過指針高,低,中等左右。

1

它說:

分割功能將需要返回一個指針不是整數。 (int a [],int * low,int * high);

返回一個指針不是整數。 int * split(int a [],int * low,int * high);