【问题标题】:Selection Sort Parallel Arrays in ++++中的选择排序并行数组
【发布时间】:2015-04-04 01:29:15
【问题描述】:

免责声明:我知道并行数组很糟糕,应该避免,选择排序不是最有效的排序,但在这种情况下,这就是老板想要的方式。我查看了很多不同的网站,但没有一个似乎真正确定了答案。另外,最好指出我是 C++ 新手,只知道相当基本的编码和调试。

我有两个简单的并行数组,我正在尝试设计一个简单的选择排序,对其中一个数组进行排序,然后相应地交换第二个数组中的元素。我有选择排序部分工作,但它似乎没有正确交换我的第二个数组中的元素。

这是我的输出的样子:

1(胡言乱语)
2(胡言乱语)
3(胡言乱语)
4(胡言乱语)
5(胡言乱语)

我有(乱码)控制台没有形成任何可识别的字母,只是奇怪的形状(如果有帮助,输出的最后一个元素是一颗心)。

这是假设的样子:

1个
2个
3c
4天
5 电子

现在我意识到在这种情况下我可以轻松地在第二个数组上运行选择排序,但我的意思是让第二个数组交换与选择排序对第一个数组所做的相应的元素。

有没有办法让这些数组正确排列?我一天中的大部分时间都在尝试解决这个问题,我确信这是一件相当简单的事情,但我的大脑被击中了。

以下是我的代码,提前感谢您查看它。

#include "stdafx.h"
#include <iostream>

using namespace std;


//Function Prototypes
void sort(int num[], char alph[], int size);





//Selection sort function
void sort(int num[], char alph[], int size)
{
int startScan;
int minIndex;
int minValue;

for (startScan = 0; startScan < (size - 1); startScan++)    //Moves through the elements
{
    minIndex = startScan;
    minValue = num[startScan];

    int index = 0;

    for (index = startScan + 1; index < size; index++)  //Compares the elements
    {
        if (num[index] < minValue)
        {
            minValue = num[index];
            minIndex = index;
        }
    }

    num[minIndex] = num[startScan];
    num[startScan] = minValue;

    alph[minIndex] = alph[startScan];
    alph[startScan] = alph[index];
}
}






//Main
int _tmain(int argc, _TCHAR* argv[])
{
int num[] = {5, 3, 1, 4, 2};
char alph[] = { 'e', 'c', 'a', 'd', 'b' };
int const SIZE = 5;


//Prints out unsorted array
cout << "This is the unsorted arrays." << endl;
cout << endl;

for (int count = 0; count < SIZE; count++)
{
    cout << num[count] << " \t ";
    cout << alph[count] << endl;
}

cout << endl;
cout << endl;


//Calls the sort function
sort(num, alph, SIZE);


//Prints out the sorted array
cout << "This is the sorted array." << endl;
cout << endl;

for (int count = 0; count < SIZE; count++)
{
    cout << num[count] << " \t";
    cout << alph[count] << endl;
}


//Pause
char temp[50];
cin >> temp;


return 0;
}

编辑:我编辑了

alph[minIndex] = num[startScan]

问题,所以它现在正确读取为:

alph[minIndex] = alph[startScan]

我现在将其作为输出:

1(胡言乱语)
2(胡言乱语)
3(胡言乱语)
4(胡言乱语)
5 电子

编辑 2: 我在之前的编辑下编辑了代码行,现在数组正确排列,我不再收到一堆乱码输出。以下是我的代码的编辑排序功能:

//NOTICE temp VARIABLE CHANGES!
void sort(int num[], char alph[], int size)
{
int startScan;
int minIndex;
int minValue;
int temp;

for (startScan = 0; startScan < (size - 1); startScan++)    //Moves through the elements
{
    minIndex = startScan;
    minValue = num[startScan];
    temp = alph[startScan];

    int index = 0;

    for (index = startScan + 1; index < size; index++)  //Compares the elements
    {
        if (num[index] < minValue)
        {
            minValue = num[index];
            minIndex = index;
            temp = alph[index];
        }
    }

    num[minIndex] = num[startScan];
    num[startScan] = minValue;

    alph[minIndex] = alph[startScan];
    alph[startScan] = temp;
}
}

【问题讨论】:

  • 另一个问题是您刚刚修复的行下方的行,您存储了 minValue 的值,当您更改汽车数组时需要执行相同操作。
  • 做到了。我添加了一个新变量 temp,并让它基本上作为另一个 minValue 起作用。我会为其他试图解决这个问题的人发布我的新代码。谢谢你的帮助。

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


【解决方案1】:

最好的解决办法可能是改变你的

num[minIndex] = num[startScan];
num[startScan] = minValue;

char  temp=alph[minIndex];
alph[minIndex] = alph[startScan];
alph[startScan] = temp;

到此为止,它确实可以完成这项工作,而且真的不能再简单了。

std::swap(num[minIndex], num[startScan]);
std::swap(alph[minIndex],alph[startScan]);

【讨论】:

  • 这是一个非常简单、优雅的解决方案。我以前见过“.swap”,但它从来没有用过(显然它只适用于字符串?)。无论如何,这很好,并且稍微清理了代码。谢谢!
  • 不,它几乎适用于任何东西,请查看 cpprefference 的列表:en.cppreference.com/w/cpp/algorithm/swap
  • 我爱那些人,他们很棒。当谷歌过去让你禁止搜索结果时,我会禁止 cplusplus.com 只获得 cppreference。
【解决方案2】:

看到这一行:

alph[minIndex] = num[startScan];

第二条故障线:

alph[startScan] = alph[index];

应该是:

alph[startScan] = alph[minIndex];

到代码退出内部循环时,size 的值已超出数组大小的 1。

我的建议:使用 IDE 和调试器来跟踪代码执行并检查变量。此外,我的第一个提示应该让您查看不正确的索引。默认情况下,C++ 不关心检查数组边界。当您越界或跟随不正确的指针时,您通常会得到垃圾。您可以通过选择编译器选项来检查数组边界来解决第一个问题。这会在开发期间减慢您的应用程序的速度,但一旦一切正常,就可以将其删除。

【讨论】:

  • 感谢您指出这一点!在一切开始看起来一样之前,您只能盯着事物看很长时间。我现在正在正确打印第二个数组的最后一个元素,但前四个仍然是乱码。
  • 感谢您的提示!我忘记了 C++ 编译器不会检查任何越界错误!我一定会学习更多有关调试的信息,再次感谢您的提示和提示!
  • 我忘了提到你需要一个临时变量来正确交换字母数字字符。
猜你喜欢
  • 1970-01-01
  • 2016-08-15
  • 2021-11-02
  • 1970-01-01
  • 2023-03-15
  • 2013-08-30
  • 2014-12-13
相关资源
最近更新 更多