【问题标题】:Selection sort method seems to do nothing选择排序方法似乎什么都不做
【发布时间】:2014-09-21 05:20:43
【问题描述】:

我正在研究一个由一组卡片对象组成的套牌类。我必须为这个套牌类编写的方法之一是排序方法,它应该按数字对卡片进行排序,以一系列卡片的顺序结束:每套 1,每套 2,每套 3,等等。 有了这个卡片类,

class card {
public:
    int number;
    string suit;
    card() {number = 1;suit = "blank";}
    card(int n, string s) {number = n; suit = s;}

    void printCard() {
        cout << number << " of " << suit << "s";
    }
};

我正在尝试为使用选择排序对数组进行排序的甲板类实现一个方法。

这是我的方法 sort() 的代码,在甲板类中声明和定义:

void sort() {
    int min, j;
    for(int i=0;i<numCards-1;i++) {
        min = i;
        for(j=i+1;j<numCards;j++) {
            if(cardArray[i].number < cardArray[min].number)
                min = j;
        }
        if(min != i)
            swapCards(cardArray[i], cardArray[min]);
    }
}

void swapCards(card &a, card &b) {
    card temp = a;
    a = b;
    b = temp;
}

但是,当我在甲板对象 d 上调用它时,似乎什么也没发生。我已经编写了一些其他方法来洗牌或切牌并交换每一半,这两种方法都有效,但是当我调用 sort() 时,什么也没有发生。我一直在通过 printCards() 方法检查数组,当我调用 shuffle() 或 cut() 时它会发生变化,但在调用 sort() 后与之前的打印输出保持一致。

在 main() 中:

deck d;

d.printDeck();
cout << endl;
d.shuffle();
d.printDeck();
cout << endl;
d.sort();
d.printDeck();

调试后打印输出:

1 of hearts
2 of hearts
3 of hearts
4 of hearts
5 of hearts
6 of hearts
7 of hearts
8 of hearts
9 of hearts
10 of hearts
11 of hearts
12 of hearts
13 of hearts
1 of clubs
2 of clubs
3 of clubs
4 of clubs
5 of clubs
6 of clubs
7 of clubs
8 of clubs
9 of clubs
10 of clubs
11 of clubs
12 of clubs
13 of clubs
1 of spades
2 of spades
3 of spades
4 of spades
5 of spades
6 of spades
7 of spades
8 of spades
9 of spades
10 of spades
11 of spades
12 of spades
13 of spades
1 of diamonds
2 of diamonds
3 of diamonds
4 of diamonds
5 of diamonds
6 of diamonds
7 of diamonds
8 of diamonds
9 of diamonds
10 of diamonds
11 of diamonds
12 of diamonds
13 of diamonds

7 of diamonds
6 of spades
5 of diamonds
12 of clubs
2 of diamonds
5 of spades
8 of diamonds
4 of clubs
1 of hearts
8 of clubs
10 of hearts
13 of diamonds
11 of diamonds
12 of spades
2 of hearts
3 of spades
12 of diamonds
7 of spades
11 of hearts
5 of clubs
5 of hearts
1 of spades
10 of diamonds
7 of clubs
4 of diamonds
3 of diamonds
13 of spades
13 of hearts
12 of hearts
6 of clubs
1 of diamonds
11 of spades
9 of hearts
3 of hearts
9 of diamonds
9 of clubs
10 of clubs
10 of spades
9 of spades
2 of spades
6 of hearts
8 of spades
4 of hearts
1 of clubs
11 of clubs
4 of spades
3 of clubs
2 of clubs
6 of diamonds
8 of hearts
13 of clubs
7 of hearts

7 of diamonds
6 of spades
5 of diamonds
12 of clubs
2 of diamonds
5 of spades
8 of diamonds
4 of clubs
1 of hearts
8 of clubs
10 of hearts
13 of diamonds
11 of diamonds
12 of spades
2 of hearts
3 of spades
12 of diamonds
7 of spades
11 of hearts
5 of clubs
5 of hearts
1 of spades
10 of diamonds
7 of clubs
4 of diamonds
3 of diamonds
13 of spades
13 of hearts
12 of hearts
6 of clubs
1 of diamonds
11 of spades
9 of hearts
3 of hearts
9 of diamonds
9 of clubs
10 of clubs
10 of spades
9 of spades
2 of spades
6 of hearts
8 of spades
4 of hearts
1 of clubs
11 of clubs
4 of spades
3 of clubs
2 of clubs
6 of diamonds
8 of hearts
13 of clubs
7 of hearts
Press any key to continue . . .

考虑到我已经成功地为涉及数组和类的其他项目实现了选择排序,我不知道为什么数组似乎没有发生任何事情。我在这里做的唯一新奇的事情是对具有多个变量的对象数组进行排序,但我很确定我正在比较 sort() 中每张卡片之间的正确变量。

如果有人发现明显错误或知道可能导致此问题的原因,请告诉我。这对帮助我解决这个问题大有帮助。

编辑:为甲板类和调试输出中使用的其他方法添加了代码。

【问题讨论】:

  • 请贴出所有相关代码(例如swapCards是如何定义的,numCards是什么)。另外,为什么不在每次排序迭代中添加调试输出?
  • if(cardArray[i].number &lt; cardArray[min].number)。我很确定你会想在比较中以某种方式使用j...(另外,除非你在swapCards 中做一些非常特别的事情,否则std::swap 会很好用。)
  • 有什么理由不能使用std::sort
  • @user657267 排序方法是本项目类的必需部分。

标签: c++ arrays class sorting selection-sort


【解决方案1】:

你的问题出在min的选择上:

for(j=i+1;j<numCards;j++) {
    if(cardArray[i].number < cardArray[min].number)
        min = j;
}

在整个循环中,min 不会改变,所以if 语句没有意义。您可能的意思是检查 cardArray[j] 与当前的最小值:

for(j=i+1;j<numCards;j++) {
    if(cardArray[j].number < cardArray[min].number)
        min = j;
}

【讨论】:

  • 就是这样!是的,通过设置循环的方式,我打算检查 cardArray[j] 与迄今为止的最低值。谢谢,我想我需要更仔细地检查一下我的代码。
猜你喜欢
  • 1970-01-01
  • 2022-01-14
  • 2015-10-29
  • 2012-09-12
  • 2011-03-17
  • 2017-09-11
  • 2017-05-16
  • 2013-05-31
  • 2016-09-22
相关资源
最近更新 更多