Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
快速排序算法在c语言中的实现_C_Algorithm_Sorting - Fatal编程技术网

快速排序算法在c语言中的实现

快速排序算法在c语言中的实现,c,algorithm,sorting,C,Algorithm,Sorting,这是c语言中快速排序算法的实现,对于给定的输入,我的程序将以45-343534 1的形式输出,我是编程新手,如果您能指出我的错误,那将非常有帮助 #include<stdio.h> #include<stdlib.h> int partition(int* A, int start, int end) { int pivot = A[end]; int pindex = start; for (int

这是c语言中快速排序算法的实现,对于给定的输入,我的程序将以
45-343534 1
的形式输出,我是编程新手,如果您能指出我的错误,那将非常有帮助

    #include<stdio.h>
    #include<stdlib.h>
    int partition(int* A, int start, int end) {
        int pivot = A[end];
        int pindex = start;
        for (int i = 0; i < end; i++) {
            if (A[i] <= pivot) {
                int temp = A[i];
                A[i] = A[pindex];
                A[pindex] = temp;
                pindex = pindex + 1;
            }
        }
        int temp = A[end];
        A[end] = A[pindex];
        A[pindex] = temp;
        return pindex;
    }
    void quicksort(int* A, int start, int end) {
        if (start < end) {
            int pindex = partition(A, start, end);
            quicksort(A, start, pindex - 1);
            quicksort(A, pindex+1, end);
        }
    }
    int main() {
        int* a = (int*)malloc(sizeof(int) * 4);
        a[0] = 1;
        a[1] = 5;
        a[2] = 4;
        a[3] = 2;
        quicksort(a, 0, 3);
        printf("%d %d %d %d", a[0], a[1], a[2], a[3]);
        system("pause");
    }
#包括
#包括
int分区(int*A,int开始,int结束){
int pivot=A[结束];
int pindex=开始;
for(int i=0;i如果(A[i]您的问题来自
分区
函数:

i
变量应启动
start
而不是在0:

int partition(int* A, int start, int end) 
{
    int pivot = A[end];
    int pindex = start;
    for (int i = start; i < end; i++) {
        if (A[i] <= pivot) {
            int temp = A[i];
            A[i] = A[pindex];
            A[pindex] = temp;
            pindex = pindex + 1;
        }
    }
    int temp = A[end];
    A[end] = A[pindex];
    A[pindex] = temp;
    return pindex;
}
给出:

83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36 11 68 67 29 82 30 62 23 67 35 29 2 
2 11 15 21 23 26 26 27 29 29 30 35 35 36 40 49 59 62 62 63 67 67 68 72 77 82 83 86 86 90 92 93     

我认为一个问题是你没有使用标准函数。但是如果这是学校(或类似的)作业,那是很自然的。至于你展示的代码的问题,我建议你。尝试“执行”纸上的代码。很久没有写过C了,但如果我没记错的话,指针的位置错了。这可能会让你对错误有所了解。一个明显的错误:
for(int i=0;i
应该是
for(int i=start;i
83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36 11 68 67 29 82 30 62 23 67 35 29 2 
2 11 15 21 23 26 26 27 29 29 30 35 35 36 40 49 59 62 62 63 67 67 68 72 77 82 83 86 86 90 92 93