Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.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_Arrays_Sorting_Quicksort_Partitioning - Fatal编程技术网

C语言中不排序的快速排序实现

C语言中不排序的快速排序实现,c,arrays,sorting,quicksort,partitioning,C,Arrays,Sorting,Quicksort,Partitioning,已经两天了,我试图用C编写一个快速排序实现,但它不起作用。我的意思是,它确实可以编译,但是输出不是我所期望的 我一直在学习一本数据结构书,它被翻译成葡萄牙语tho,我的母语,无论如何。。。我将通过下面的说明以及我的代码 // //快速排序V2.c //IFTM演习 // //由莱尔·费雷拉于19年7月9日创作。 //版权所有©2019 Lelre Ferreira。版权所有。 // #定义尺寸5 #包括 void printfarray(int*数组); 无效接收数组(int*数组); in

已经两天了,我试图用C编写一个快速排序实现,但它不起作用。我的意思是,它确实可以编译,但是输出不是我所期望的

我一直在学习一本数据结构书,它被翻译成葡萄牙语tho,我的母语,无论如何。。。我将通过下面的说明以及我的代码

//
//快速排序V2.c
//IFTM演习
//
//由莱尔·费雷拉于19年7月9日创作。
//版权所有©2019 Lelre Ferreira。版权所有。
//
#定义尺寸5
#包括
void printfarray(int*数组);
无效接收数组(int*数组);
int QuickSortPartition(int*数组、int开始、int结束);
void QuickSortFunction(int*数组、int开始、int结束);
int main(int argc,const char*argv[]{
int数组[大小];
接收阵列(阵列);
打印法拉利(阵列);
返回0;
}
无效接收数组(int*array){
int i=0;
对于(i=0;i对于(j=begin;j当您编写函数时,请在程序中使用它之前对其进行测试

函数
QuickSortPartition
显然是错误的

考虑以下演示程序和您的函数实现

#include <stdio.h>

int QuickSortPartition(int *array, int begin, int end){


    int pivot = array[end];
    int i = (begin - 1), j = 0;

    for (j = begin; j <= end - 1; j++) {
        if (array[j] <= pivot) {
            i++;
            array[i] = array[j];
        }
    }
    array[i + 1] = array[end];
    return (i + 1);
}

int main( void )
{
    int a[] = { 5, 4, 2, 1, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );

    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    putchar( '\n' );

    QuickSortPartition( a, 0, ( int )( N - 1 ) );

    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    putchar( '\n' );
}    
您需要交换函数中的值,而不是使用简单的赋值

#include <stdio.h>

size_t QuickSortPartition( int *array, size_t begin, size_t end )
{
    const int pivot = array[end];

    size_t i = begin - 1;

    for ( size_t j = begin; j < end; j++ ) 
    {
        if ( array[j] <= pivot ) 
        {
            int tmp = array[++i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }

    int tmp = array[++i];
    array[i] = array[end];
    array[end] = tmp;

    return i;
}

int main( void )
{
    int a[] = { 5, 4, 2, 1, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );

    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    putchar( '\n' );

    size_t partition = QuickSortPartition( a, 0, N - 1 );

    printf( "%zu: ", partition );
    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    putchar( '\n' );
}    
对于索引,我使用类型
size\u t
(您也应该这样做),而不是类型
int

在函数
QuickSortFunction
中,您需要调用它本身,而不是函数
QuickSortPartition

void QuickSortFunction(int *array, size_t begin, size_t end){

    if (begin < end) {
        size_t pivot = QuickSortPartition(array, begin, end);
        QuickSortFunction(array, begin, pivot - 1);
        QuickSortFunction(array, pivot + 1, end);
    }

}
是不好的。(似乎每个人都从一个不好的例子中复制了算法:)。当所有元素都小于或等于pivot的值时,该函数效率低下

您还可以编写一个单独的函数来交换数组的两个元素

下面是一个演示程序,演示如何改进函数
QuickSortPartition
的代码

#include <stdio.h>

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

size_t QuickSortPartition( int *array, size_t begin, size_t end )
{
    const int pivot = array[end];

    size_t i = begin;

    for ( size_t j = begin; j < end; j++ ) 
    {
        if ( array[j] <= pivot ) 
        {
            if ( i != j ) swap( &array[i], &array[j] );
            ++i;
        }
    }

    if ( i != end ) swap( &array[i], &array[end] );

    return i;
}

int main( void )
{
    int a[] = { 5, 4, 2, 1, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );

    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    putchar( '\n' );

    size_t partition = QuickSortPartition( a, 0, N - 1 );

    printf( "%zu: ", partition );
    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    putchar( '\n' );
}    
#包括
无效交换(int*a,int*b)
{
int tmp=*a;
*a=*b;
*b=tmp;
}
大小\u t QuickSortPartition(int*数组、大小\u t开始、大小\u t结束)
{
const int pivot=数组[end];
尺寸=开始;
对于(大小j=begin;j
  • 你从不调用你的排序函数
  • 一旦你修正了(1),你会发现你的配分函数完全被破坏了。你正在尝试使用,但它与任何地方都不匹配
  • 无论您对(2)的算法执行了什么转换,都是错误的。具有固定轴选择的Lomuto分区的通用算法始终位于末尾(一个糟糕的选择,但这超出了本问题的范围),如下所示:

    更新的分区函数

    // added to make the partition algorithm easier to understand.
    void swap_int(int *a, int *b)
    {
        int tmp = *a;
        *a  = *b;
        *b = tmp;
    }
    
    int QuickSortPartition(int *array, int begin, int end){
    
        int i=begin, j;
    
        for (j = begin; j <= end; j++)
        {
            if (array[j] < array[end])
                swap_int(array+j, array + i++);
        }
        swap_int(array+i, array+end);
    
        return i;
    }
    
    输入

     4 1 3 5 2
    
    Value sorted: 1
    Value sorted: 2
    Value sorted: 3
    Value sorted: 4
    Value sorted: 5
    
    输出

     4 1 3 5 2
    
    Value sorted: 1
    Value sorted: 2
    Value sorted: 3
    Value sorted: 4
    Value sorted: 5
    

    在这段代码中还有许多其他不相关的东西需要解决(检查IO,一个更好的轴心选择方案,比如三的中位数,等等),但基本问题已在上面解决。

    谢谢大家!我在前面的代码中修复了这些问题,最后使用while制作了另一个,我将两者都留在这里

    //
    //  Quick sort V2 fixed.c
    //  IFTM Exercises
    //
    //  Created by Lelre Ferreira on 7/11/19.
    //  Copyright © 2019 Lelre Ferreira. All rights reserved.
    //
    
    #define size 5
    #include <stdio.h>
    
    void printArray(int *array);
    void receiveArray(int *array);
    
    void QuickSwap(int* a, int* b);
    void QuickSort(int *array, int begin, int end);
    int  QuickSortPartition(int *array, int begin, int end);
    
    int main (int argc, const char * argv[]){
    
        int array[size];
    
        receiveArray(array);
        QuickSort(array, 0, size-1);
        printArray(array);
    
        return 0;
    }
    
    void printArray(int *array){
    
        int i = 0;
        for (i = 0; i < size; i++) {
            printf("Value sorted: %d\n", array[i]);
        }
    }
    
    void receiveArray(int *array){
    
        int i = 0;
        for (i = 0; i < size; i++) {
            printf("Insert value of [%d]: ", i);
            scanf("%d", &array[i]);
        }
    }
    
    void QuickSwap(int* a, int* b){
    
        int x = *a;
        *a = *b;
        *b = x;
    }
    
    void QuickSort(int *array, int begin, int end){
    
        if (begin < end) {
            int pivot = QuickSortPartition(array, begin, end);
            QuickSort(array, begin, pivot - 1);
            QuickSort(array, pivot + 1, end);
        }
    }
    
    int QuickSortPartition(int *array, int begin, int end){
    
        int pivot = array[end];
        int i = begin - 1, j = 0;
    
        for (j = begin; j <= end - 1; j++) {
            if (array[j] <= pivot) {
                i++;
                QuickSwap(&array[i], &array[j]);
            }
        }
        QuickSwap(&array[i + 1], &array[j]);
        return (i + 1);
    }
    
    //
    //快速排序V2.c
    //IFTM演习
    //
    //由莱尔·费雷拉于19年11月7日创作。
    //版权所有©2019 Lelre Ferreira。保留所有权利。
    //
    #定义尺寸5
    #包括
    无效打印数组(int*数组);
    无效接收数组(int*数组);
    void QuickSwap(int*a,int*b);
    无效快速排序(int*数组、int开始、int结束);
    int QuickSortPartition(int*数组、int开始、int结束);
    int main(int argc,const char*argv[]{
    int数组[大小];
    接收阵列(阵列);
    快速排序(数组,0,大小为1);
    打印阵列(数组);
    返回0;
    }
    无效打印数组(int*数组){
    int i=0;
    对于(i=0;i对于(j=begin;j)您的
    main
    从不调用任何排序函数。学习使用调试器是一个好主意。这样,您可以更轻松地查看调用了哪些函数,中间状态是什么,并找出错误。@Lelre Ferreira显然函数QuickSortPartition不正确。OT:关于:
    int main(int argc,const char*argv[]){
    这将导致编译器输出两条有关未使用参数的警告消息。(
    argc
    argv
    )建议为
    main()
    --
    int main(void)使用其他有效签名
    看来
    QuickSortFunction
    是用来递归的,这是很常见的,但它不是。它应该在哪里调用自己(两次),而是调用
    QuickSortPartition
    。谢谢你的帮助,我犯了一些错误,我有点厌倦了一遍又一遍地编写代码。我做了更正,因为它现在正常工作了。@LelreFerreira这在C中实际上更容易,它允许将数组指针参数重定为递归调用。这意味着ns您只需使用两个参数(指向数组中/的指针和长度参数)即可完成此操作
     4 1 3 5 2
    
    Value sorted: 1
    Value sorted: 2
    Value sorted: 3
    Value sorted: 4
    Value sorted: 5
    
    //
    //  Quick sort V2 fixed.c
    //  IFTM Exercises
    //
    //  Created by Lelre Ferreira on 7/11/19.
    //  Copyright © 2019 Lelre Ferreira. All rights reserved.
    //
    
    #define size 5
    #include <stdio.h>
    
    void printArray(int *array);
    void receiveArray(int *array);
    
    void QuickSwap(int* a, int* b);
    void QuickSort(int *array, int begin, int end);
    int  QuickSortPartition(int *array, int begin, int end);
    
    int main (int argc, const char * argv[]){
    
        int array[size];
    
        receiveArray(array);
        QuickSort(array, 0, size-1);
        printArray(array);
    
        return 0;
    }
    
    void printArray(int *array){
    
        int i = 0;
        for (i = 0; i < size; i++) {
            printf("Value sorted: %d\n", array[i]);
        }
    }
    
    void receiveArray(int *array){
    
        int i = 0;
        for (i = 0; i < size; i++) {
            printf("Insert value of [%d]: ", i);
            scanf("%d", &array[i]);
        }
    }
    
    void QuickSwap(int* a, int* b){
    
        int x = *a;
        *a = *b;
        *b = x;
    }
    
    void QuickSort(int *array, int begin, int end){
    
        if (begin < end) {
            int pivot = QuickSortPartition(array, begin, end);
            QuickSort(array, begin, pivot - 1);
            QuickSort(array, pivot + 1, end);
        }
    }
    
    int QuickSortPartition(int *array, int begin, int end){
    
        int pivot = array[end];
        int i = begin - 1, j = 0;
    
        for (j = begin; j <= end - 1; j++) {
            if (array[j] <= pivot) {
                i++;
                QuickSwap(&array[i], &array[j]);
            }
        }
        QuickSwap(&array[i + 1], &array[j]);
        return (i + 1);
    }
    
    //
    //  Quick sort V1.c
    //  IFTM Exercises
    //
    //  Created by Lelre Ferreira on 7/8/19.
    //  Copyright © 2019 Lelre Ferreira. All rights reserved.
    //
    
    #define size 5
    #include <stdio.h>
    
    void printArray(int *array);
    void QuickSortFunction(int *array, int begin, int end);
    int QuickSortPartition(int *array, int begin, int end);
    
    int main (int argc, const char * argv[]){
    
        int array[] = {2, 3, 1, 5, 4};
    
            QuickSortFunction (array, 0, size-1);
            printArray(array);
    
        return 0;
    }
    
    void printArray(int* array){
    
        int i = 0;
        for (i = 0; i < size; i++) {
            printf("Value sorted: %d\n", array[i]);
        }
    }
    
    int QuickSortPartition(int *array, int begin, int end){
    
        int left, right;
        int pivot, aux;
    
        right = end;
        left = begin;
        pivot = array[begin];
    
        while (left < right) {
            while (array[left] <= pivot)
                left++;
            while (array[right] > pivot)
                    right--;
                    if (left < right) {
                        aux = array[left];
                        array[left] = array[right];
                        array[right] = aux;
                    }
                }
    
                array[begin] = array[right];
                array[right] = pivot;
                return right;
    }
    
    void QuickSortFunction(int *array, int begin, int end){
    
        if (begin < end) {
            int pivot = QuickSortPartition(array, begin, end);
            QuickSortFunction(array, begin, pivot - 1);
            QuickSortFunction(array, pivot + 1, end);
        }
    }