简体   繁体   English

用于排序的递归 C 程序提供意外输出

[英]Recursive C program for sorting giving unexpected output

I am trying to make a recursive function to sort an array.我正在尝试使用递归函数对数组进行排序。 The idea is to swap two elements whenever the one with smaller index is larger, because we want to sort into ascending order.这个想法是在索引较小的元素较大时交换两个元素,因为我们要按升序排序。 The following is the program in C that I have written for this以下是我为此编写的 C 程序


void sort(int a[30], int n)
{
    int m = 0,i,temp;
    for(i = 0;i<n-1,m==0;i++)
        {
            printf("The array when i is %d is %d",i,a[0]);
            for(i=1;i<n;i++)
            printf(",%d",a[i]);
            printf("\n");
            if(a[i]>a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                m = 1;
            }
        }
    if(m==0)
    {
        printf("The sorted array is %d",a[0]);
        for(i=1;i<n;i++)
        printf(",%d",a[i]);
    }
    else
    sort(a,n);
}

int main()
{
    int a[30],n;
    printf("Enter the number of elements\n");
    scanf("%d",&n);
    if(n>30||n<1)
    printf("The number of elements should be a natural number not exceeding 30");
    else
    {
        int i;
        for(i=0;i<n;i++)
        {
            printf("Enter element number %d\n",i+1);
            scanf("%d",&a[i]);
        }
        sort(a,n);
    }
    return 0;
}

This program is not giving desired output, it runs into a very long loop (even for n=4), and then suddenly stops.这个程序没有给出想要的输出,它运行了一个很长的循环(即使 n=4),然后突然停止。

Can somebody please detect the problem??有人可以请检测问题吗?

In the condition of the if statement在 if 语句的条件下

for(i = 0;i<n-1,m==0;i++)

there is used the comma operator.使用了逗号运算符。 Its value is the value of the right hand operand that is of m==0.它的值是右侧操作数的值,即 m==0。

I think you mean我想你的意思

int m = 1;
for (i = 0; m != 0 && i<n-1; i++ )
{
    m = 0;
    //...

That is if in the inner loop there was not swapping elements of the array that means that the array is already sorted then m will be equal to 0 and the outer loop will stop its iterations.也就是说,如果在内循环中没有交换数组的元素,这意味着数组已经排序,则 m 将等于 0,外循环将停止其迭代。

Also withing the inner loop you have to use another variable for the index not i .同样在内部循环中,您必须使用另一个变量作为索引而不是i

Here is a demonstrative program that shows how the function can be implemented based on the method bubble sort.这是一个演示程序,展示了如何基于冒泡排序方法实现该功能。

#include <stdio.h>

void bubble_sort( int a[], size_t n )
{
    if ( !( n < 2 ) )
    {
        size_t last = 1;

        for ( size_t i = last; i < n; i++ )
        {
            if ( a[i] < a[i-1] )
            {
                int tmp = a[i];
                a[i] = a[i-1];
                a[i-1] = tmp;

                last = i;
            }
        }

        bubble_sort( a, last );
    }
}

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

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", a[i] );
    }

    putchar( '\n' );

    bubble_sort( a, N );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", a[i] );
    }

    putchar( '\n' );

    return 0;
}

The program output is程序输出是

9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 9 

you declare int i once here int m = 0,i,temp;你在这里声明int i一次int m = 0,i,temp; then use it for every loop然后在每个循环中使用它

look:看:

    for(i = 0;i<n-1,m==0;i++)
        {
            printf("The array when i is %d is %d",i,a[0]);
     //error       for(i=1;i<n;i++)//this is your infinitive loop
            printf(",%d",a[i]);
            printf("\n");
            if(a[i]>a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                m = 1;
            }
        }

every time you you enter this loop每次你进入这个循环

for(i=1;i<n;i++)
            printf(",%d",a[i]);

your i become 1 and then after loop , it will be 3 and so this will effect your base loop:(after first time)您的i变为 1,然后在循环之后,它将变为 3,因此这将影响您的基本循环:(第一次之后)

for(i = 0;i<n-1,m==0;i++)

your i here will always be 4 so this is an infinitive loop你在这里的i永远是4所以这是一个不定式循环

look at this看这个

void swap(int array[], int index1, int index2) {
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;

}

void sort(int array[], int startpoint, int arraylength) {

    if (startpoint == arraylength - 1) return;

    int minindex = startpoint;

    for (int i = startpoint; i < arraylength; i++) if (array[i] < array[minindex]) minindex = i;

    swap(array, startpoint, minindex);


    sort(array, startpoint + 1, arraylength);

}
int main()
{
    int a[30], n;
    printf("Enter the number of elements\n");
    scanf("%d", &n);
    if (n > 30 || n < 1)
        printf("The number of elements should be a natural number not exceeding 30");
    else
    {
        int i;
        for (i = 0; i < n; i++)
        {
            printf("Enter element number %d\n", i + 1);
            scanf("%d", &a[i]);
        }
        sort(a,0, n);
    }
    return 0;
}

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

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