簡體   English   中英

冒泡排序C中的降序和升序不會排序

[英]Bubble sort Descending and Ascending in C won't sort

我給用戶選擇是按升序還是降序對元素進行排序。 我知道我的代碼可以正確排序元素,但在main的某個地方,我認為我在調用函數以正確順序打印升序/降序時出錯。 還是需要像我在bubble_sort函數中那樣使用另一個if語句? 我需要這樣做,以便Main函數將最終結果打印給用戶。 這是我得到的輸出: 輸入元素數3輸入3個整數43 7 90輸入排序順序請輸入A代表升序或D代表降序d降序排序列表:43 7 90

#include <stdio.h>

void bubble_sort(long [], char n);

int main()
{
    long array[100], n, c;

    printf("Enter number of elements\n");
    scanf("%ld", &n);

    printf("Enter %ld integers\n", n);
    for (c = 0; c < n; c++)
        scanf("%ld", &array[c]);

    printf("Enter sort order\n");
    fflush(stdin);
    printf("Please enter A for ascending or D for descending order\n");
    scanf("%ld", &n);
    bubble_sort(array, n);
    printf("Sorted list in descending order:\n");
    for ( c = 0 ; c < n ; c++ )
    {
        printf("%ld\n", array[c]);
    }
    fflush(stdin);
    getchar();
    return 0;
}

void bubble_sort(long list[], char n)
{
    long c, d, temp;

    if(n=='a' || n=='A')
    {
        for (c = 0 ; c < ( n - 1 ); c++)
        {
            for (d = 0 ; d < n - c - 1; d++)
            {
                if (list[d] > list[d+1])
                {
                    temp = list[d];
                    list[d] = list[d+1];
                    list[d+1] = temp;
                }
            }
        }
    }
    if(n=='d' || n=='D')
    {
        long c, d, temp;

        for (c = 0 ; c < ( n - 1 ); c++)
        {
            for (d = 0 ; d > n - c - 1; d++)
            {
                if (list[d] < list[d+1])
                {/* Swapping */
                    temp = list[d];
                    list[d] = list[d+1];
                    list[d+1] = temp;
                }
            }
        }
    }
}

編輯:在這里我添加了交換功能,只是這樣升/降邏輯更有效。 但是我似乎混淆了我認為是一個大問題的變量的使用。 有人會指出並幫助我了解在何處以及為什么需要使用這些變量嗎? 非常感謝!

#include <stdio.h>

void bubble_sort(int list[], int n, char c);
void swap(int x, int y, int array[]);
int main()
{
    int array[100], j, i;
    char c;
    printf("Enter number of elements\n");
    scanf("%d", &j);

    printf("Enter %d integers\n", j);
    for (i = 0; i < j; i++)
        scanf("%d", &array[i]);

    printf("Please enter A for ascending or D for descending order\n");
    scanf("%s", &c);

    bubble_sort(array, j, i);
    printf("Sorted list in descending order:\n");
    for (i = 0 ; i < j ; i++ )
    {
        printf("%d\n", array[i]);
    }
    getchar();
    return 0;
}

void bubble_sort(int list[], int n, char c)
{
    int i, j;

    if(c=='a' || c=='A'){
        for (i = 0; i < (n - 1); i++){
            for (j = 0; j < (n - i) - 1; j++){
                if (list[i] > list[j])
                {
                    swap(i, j, list);                }
            }
        }
    }
    if(c=='d' || c=='D') {
        for (i = 0 ; i < ( n - 1 ); i++) {
            for (j = 0 ; j > (n - i) - 1; j++) {
                if (list[i] < list[j])
                {
                    swap(i, j, list);
                }
            }
        }
    }
}

void swap(int x, int y, int array[])
{
    int hold; //temp hold a number

    hold = array[x];
    array[x] = array[y];
    array[y] = hold;
}

在此聲明中

printf("Please enter A for ascending or D for descending order\n");
scanf("%ld", &n);

您將覆蓋n中存儲的值,這些值在這些語句之前表示數組中元素的數量。 您應該再聲明一個char類型的變量,並將其用於此代碼段。

此外,sort函數應聲明為

void bubble_sort(long list[], int n, char c );

其中n是數組大小,c是'A'或'D'

編輯:您的新代碼包含許多錯字。 嘗試以下

#include <stdio.h>

void swap( int x, int y, int array[] )
{
    int hold; //temp hold a number

    hold = array[x];
    array[x] = array[y];
    array[y] = hold;
}

void bubble_sort( int list[], int n, char c )
{
    int i, j;

    if ( c == 'a' || c == 'A' )
    {
        for ( i = 0; i < n - 1; i++ )
        {
            for ( j = 0; j < n - i - 1; j++ )
            {
                if ( list[j] > list[j+1] )
                {
                    swap( j, j + 1, list);                
                }
            }
        }
    }

    if ( c=='d' || c=='D' ) 
    {
        for ( i = 0 ; i < n - 1; i++ ) 
        {
            for ( j = 0 ; j < n - i - 1; j++ ) 
            {
                if ( list[j] < list[j+1] )
                {
                    swap( j, j + 1, list);
                }
            }
        }
    }
}

int main(void) 
{
    int array[100], j, i;
    char c;

    printf("Enter number of elements: ");
    scanf( "%d", &j);

    printf( "Enter %d integers\n", j );
    for ( i = 0; i < j; i++ ) scanf( "%d", &array[i] );

    printf("Please enter A for ascending or D for descending order: ");
    scanf( " %c", &c );

    printf( "%c\n", c );

    bubble_sort( array, j, c );

    printf( "Sorted list in the selected order:\n" );
    for ( i = 0; i < j; i++ )
    {
        printf( "%d ", array[i] );
    }
    puts( "" );

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM