简体   繁体   English

C 问题中的快速排序

[英]Quick Sort in C Issue

I'm been stuck at a certain point in my quick sort program in C. Can anybody point out where the error is?我在 C 语言的快速排序程序中遇到了某个问题。有人能指出错误在哪里吗? My code doesn't run and I've tried comparing it with multiple online programs.我的代码无法运行,我尝试将其与多个在线程序进行比较。 I've got a test tomorrow and I want to know what the error is before I attempt it.我明天有一个测试,我想在尝试之前知道错误是什么。

#include <stdio.h>

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

int partition(int a[],int l,int h){
    int pi=l,i=l,j=h;
    while(i<j){
            while(a[i]<=a[pi])
                i++;
            while(a[j]>a[pi])
                j--;
            if(i<j){
                swap(a[i],a[j]);
            }
        }
    swap(a[j],a[pi]);
    return j;
}

void quicksort(int a[],int l,int h){
    int j;
    j=partition(a,l,h);
    quicksort(a,l,j-1);
    quicksort(a,j+1,h);
}

int main(){
    int n,i;
    printf("Enter the number of elements: ");
    scanf("%d",&n);
    int a[n];
    printf("Enter the elements: ");
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    quicksort(a,0,n-1);
    printf("The array after sorting is: ");
    for(i=0;i<n;i++){
        printf("%d",a[i]);
    }
    return 0;
}

Function arguments are passed by value in C. Your swap function:函数参数在 C 中按值传递。您的swap函数:

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

gets passed two integer arguments, int a and int b .被传递了两个整数参数, int aint b Within the function, they behave like local variables, so changes to a and b within the swap function have no effect on the caller.在函数内,它们的行为类似于局部变量,因此swap函数内对ab更改对调用者没有影响

To mimic the effect of passing by reference , you can use pointers to pass the addresses of the objects whose values are to be modified, and dereference the pointers inside the function to access the objects that are to be modified.为了模拟通过引用传递的效果,您可以使用指针传递要修改值的对象的地址,并在函数内部取消引用指针以访问要修改的对象。

For example, your swap function can be modified as follows:例如,您的swap函数可以修改如下:

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

The calls to this function also need to be modified to pass the addresses of the objects whose values are to be swapped.对该函数的调用也需要修改以传递其值将被交换的对象的地址。 For example, swap(a[i],a[j]);例如, swap(a[i],a[j]); needs to be changed to swap(&a[i],&a[j]);需要改为swap(&a[i],&a[j]); (or equivalently, changed to swap(a+i,a+j); ). (或等效地,更改为swap(a+i,a+j); )。

Your code has several issues.您的代码有几个问题。

First, your swap function swaps the local variables a and b , but it won't affect the array a in partition .首先,您的交换函数交换局部变量ab ,但它不会影响partition的数组a Ian Abbott has explained this more thoroughly. Ian Abbott 对此进行了更彻底的解释。

One way to solve that is to pass the addresses of the array elements via pointers, so that swap can access and change them.解决这个问题的一种方法是通过指针传递数组元素的地址,以便swap可以访问和更改它们。 A simpler way is to write a swap function that operates on indices of an array:一种更简单的方法是编写一个对数组索引进行操作的交换函数:

void swap(int array[], int i, int j)
{
    int t = array[i]; array[i] = array[j]; array[j] = t;
}

Second, must stop the recursion at some time.其次,必须在某个时间停止递归。 When you have an array with only one element or with no elements at all, there's nothing to do, because the array (or subarray) is already sorted.当你有一个只有一个元素或根本没有元素的数组时,没有什么可做的,因为数组(或子数组)已经排序。

So don't do anything in that case.所以在这种情况下不要做任何事情。 Or, rather, only do something when you have at least two elements.或者,更确切地说,仅当您至少有两个元素时才做某事。 That has the nice side effect that the recusrion actually stops.这具有很好的副作用,即 recusrion 实际上停止了。 :)

void quicksort(int a[], int l, int h)
{
    if (l < h) {
        int j;

        j = partition(a, l, h);
        quicksort(a, l, j - 1);
        quicksort(a, j + 1, h);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM