【问题标题】:Bubble sort won't work(It's actually sort, I'm sorry)冒泡排序不起作用(实际上是排序,对不起)
【发布时间】:2016-02-01 01:28:05
【问题描述】:

我已经学习 C 一个星期了。今天学习了数组以及如何使用冒泡排序对它们进行排序,所以我按照书中的代码编写了一些代码,它单独运行很好,但它不适用于更大的程序,只会跳过循环。我试图调试它,直到第 20 行,由于某种原因,外部将立即等于 9,因此内部将等于 10,程序将继续运行。这是我的代码:

#include <stdio.h>

int main(void) {

int ctr; //loop counter
int idSearch; //Customer to look for (the key)
int found = 0; // 1 (true) if the customer is found

/* Defines the 10 elements in each of the parallel arrays */

int custID[10] = {313, 454, 502, 101, 892, 475, 792, 912, 343, 644};
float custBal[10] = {0.00, 45.43, 71.23, 301.56, 9.08, 192.41, 389.00, 229.67,
                     18.31, 59.54};
int tempID, inner, outer; // For sorting float temBal
tempID = inner = outer = 0;
float tempBal =0;

/* first, sort the arrays by customer ID */

for (outer=0; outer < 9; outer++);
    {
        for (inner = (outer+1); inner < 10; inner++)
            {
                if (custID[inner] < custID[outer])
                    {
                        tempID = custID[inner]; // must switch both arrays
                        tempBal = custBal[inner]; // or they won't be linked
                        custID[inner] = custID[outer];
                        custBal[inner] = custBal[outer];
                        custID[outer] = tempID;
                        custBal[outer] = tempBal;
                    }
            }
    }
/* Interact with the user looking to find a balance */

printf("**Customer Balance Lookup**\n");
printf("What is the customer's number? ");
    scanf(" %d", &idSearch);

/* Now look for the ID in the array */

for (ctr = 0; ctr <10; ctr++)
    {
        if (idSearch == custID[ctr]) //Do they match?
        {
            found = 1; // Yes, match flag is set to True
            break;
        }
        if (custID[ctr] > idSearch) // No need to keep searching
            {
                break;
            }
    }

// Once the loop has completed, the ID was either found if not

if (found)
    {
        if (custBal[ctr] > 100)
            {
                printf("\n**That customer's balance is $%.2f.**\n", custBal[ctr]);
                printf("No additional credit\n");
            } else {
                printf("\n**The customer's balance is good!**");
            }
    }else {         printf("\n**You have entered an incorrect customer ID.**");
        printf("\n ID %d was not found in the list.\n", idSearch);
    }

return (0);

}

【问题讨论】:

  • 发布custID[]custBal[]的声明。
  • @chux 你能解释一下吗?我很新,所以我不明白你想说什么。
  • 发布的代码缺少变量 custID[], custBal[], tempID , tempBal, outer 的声明。将它们作为您问题的一部分发布。当前的代码按原样肯定不会编译。
  • 第 3 行是 int main(void) {。请正确识别出错的行。
  • 去掉这一行的分号:for (outer=0; outer &lt; 9; outer++); 。分号是循环正在执行的语句,而不是后面的块。在这一行设置断点应该可以很快识别出错误。

标签: c algorithm sorting


【解决方案1】:

您似乎正在尝试对custID 数组进行冒泡排序,并且在此过程中还对custBal 数组进行了排序。但是,您进行冒泡排序的逻辑很复杂。尝试改用这个双 for 循环:

for (outer = 0; outer < ( n - 1 ); outer++) {
    for (d=inner = 0; inner < n - outer - 1; inner++) {
        if (custID[inner] > custID[inner+1])
        {
            tempID           = custID[inner];
            tempBal          = custBal[inner];
            custID[inner]    = custID[inner+1];
            custBal[inner]   = custBal[inner+1];
            custID[inner+1]  = tempID;
            custBal[inner+1] = tempBal;
        }
    }
}

【讨论】:

  • OP 进行冒泡排序的逻辑是什么?对我来说,它看起来不像是在实施冒泡排序。
  • 逻辑一团糟。这就是我刚刚从头开始的原因。
  • 我相信你的代码和我的代码完全相同,但在我的代码中,inner always = outer+1 所以我不需要像你在切换位置时那样输入 [inner+1]
  • @TimBiegeleisen 慢慢来。 :)
  • @H.Malkawi 不,inner 开始outer+1,但随着它的增加很快就会失去同步。您实现的不是冒泡排序,尽管它和冒泡排序一样慢。
【解决方案2】:

我真的很不好意思写这个,但是在查看我的代码 30 分钟后,逐行浏览它,我在第 20 行的末尾发现了一个分号for (outer=0; outer &lt; 9; outer++);

谢谢你们帮助我,我真的很感激。

【讨论】:

  • 抱歉让你的派对崩溃了,但你的逻辑是错误的。
  • 怎么样?每个数组有 10 个值。外部排序和内部排序用于比较值,外部值在每次传递期间保持不变,而内部值在变化,如果满足要求(内部小于外部),则将从两个数组中切换,因为它们是连在一起的。
  • 您当前的逻辑没有交换值,也没有正确进行冒泡排序。
  • 它确实看起来更像选择排序而不是冒泡排序:en.wikipedia.org/wiki/Selection_sort - 在冒泡排序中,唯一的比较和交换是在相邻元素之间。
  • @moreON 是正确的,您的排序比冒泡排序更像选择排序 - 尽管它执行的交换次数比必要的多。仅仅因为你已经成功实现了一种排序并不意味着它是正确的。
猜你喜欢
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 2014-03-16
  • 2014-12-30
  • 1970-01-01
相关资源
最近更新 更多