程序有两处错误:

  1. 第26行while循环条件中有一个中文字符,应该改为英文字符。

  2. 第33行中的key应该更新为end,以保证key位置上的元素被正确放置。

修改后的程序如下:

#include <stdio.h>

void swap(int *a,int *b) { int temp = *a; *a = *b; *b = temp; }

void q_sort(int arr[],int begin,int end) { if(begin >= end) return; int left = begin; int right = end; int key = begin;

while(begin < end && left <right)
{
    while(arr[end] >= arr [key] && begin < end)
    {
        --end;
    }
    while(arr[begin] <= arr[key] && begin < end)
    {
        begin++;
    }
    swap(&arr[begin],&arr[end]);
}
swap(&arr[key], &arr[end]);
key = end;
q_sort(arr,left,key-1);
q_sort(arr,key+1,right);

}

int main() { int arr[] = {1,2,3,9,6}; int n = sizeof(arr) / sizeof(arr[0]); q_sort(arr,0,n-1); for(int i=0; i < n; i++) { printf("%d\n", arr[i]); } return 0;

#include stdiohvoid swapint aint b	int temp = a;	a = b;	b = temp;void q_sortint arrint beginint end	ifbegin = end		return;	int left = begin;	int right = end;	int key = begin;	whilebegin end && left r

原文地址: http://www.cveoy.top/t/topic/eyZc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录