簡體   English   中英

氣泡排序問題,升序排列

[英]Bubble sort issue with ascending order

我這里有一個程序,要求用戶輸入最多20個數字。 然后,它顯示輸入的數字,其中刪除了重復項,然后使用冒泡排序以刪除它們的升序顯示它們。 我的問題在泡沫中。 當它以升序列出編號時,最后一個編號始終會被刪除。 有人可以通過顯示原因來幫助我。

#include <stdio.h>    

/* This program asks the user to enter up to 20 numbers. It then displays the numbers entered, removes duplicates
 and then list the numbers with the duplicates removed in ascending order.
 */
int main (void)
{
    setbuf(stdout, NULL);

    int nums[20] , i , j, k, swap ;
    int count=0;

    {
        printf("Enter integers. (Negative -1 to stop):\n");
        for (i=0; i < 20; i++)
        {
            scanf("%d", &nums[i]);
            count = count +1;

            if(nums[i] == -1 ) // If user enters -1 stops the program from expecting anymore number inputs
                break;
        }
    }

    printf("The numbers you entered are:\n"); // outputs the numbers you entered one number per line
    for(i=0;i<count;++i)
    {
        printf("%d\n", nums[i]);
    }

    printf("\n Your numbers with the duplicate numbers removed:\n ");
    // for loop for removing the duplicate numbers that the user enters.
    for(i=0;i<count;i++)
    {
        for(j=i+1;j<count;)
        {
            if(nums[j]==nums[i])
            {
                for(k=j;k<count-1;++k)
                {
                    nums[k]=nums[k+1];
                }
                count--;
            }
            else
            {
                j++;
            }
        }
    }

    for(i=0;i<count;i++) // outputs the numbers you entered with the duplicates removed one number per line
        printf("%d\n ",nums[i]);

    // start of the bubble sort for listing the numbers in ascending order. Can replace ">" with "<" to list in descending order
    for(i=0; i<(k-1); i++)
    {
        for(j=0; j < k - i; j++)
        {
            if (nums[j] > nums[j+1])
            {
                swap = nums[j];
                nums[j] =nums[j+1];
                nums[j+1] = swap;
            }
        }
    }
    printf("\nYour numbers sorted in to ascending order with the duplicates removed:\n");

    for(j=0;j<i;j++) // outputs the numbers in ascending order. One number per line
        printf("%d\n ",nums[j]);


    return 0;
}

更換

 for(i=0; i<(k-1); i++)
 {
     for(j=0; j < k - i; j++)

 for(i=0; i<k; i++)
 {
     for(j=0; j < k - i-1; j++)

要么

for(i=0; i<=(k-1); i++)
{
     for(j=0; j < k - i-1; j++)

您的排序例程將k作為條件。 上面的代碼表明,根據您擁有的數據, k可能正確初始化,也可能沒有正確初始化。 也許您想使用count

與上一個循環相同。

一個建議:不要重用循環變量,它會引起各種各樣的麻煩。 您的代碼確實很難閱讀,可能還有更多問題。

PS您可以在每個以{開頭的塊中聲明一個變量。 在循環中聲明變量,以防止不必要的重用。

有兩個問題。

請記住,刪除重復項后,您的變量count就有條目count

所以這樣做會更容易

for(i=0; i < count - 1; i++)
{
    for(j=0; j < count - 1 - i; j++)
    {
        .....

注意第二個循環中的-1 由於您使用的是j+1 ,因此可以防止迭代越界

第二個錯誤只是在您的打印循環中。

由於您已經在count存儲了要打印的數字量,因此請更改

for(j = 0; j < i; j++)

for(j = 0; j < count; j++)

暫無
暫無

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

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