选择在++中对并行数组排序

Selection Sort Parallel Arrays in ++

本文关键字:并行 数组排序 选择      更新时间:2023-10-16

免责声明:我知道并行数组很糟糕,应该避免,而且选择排序不是最有效的排序,但在这种情况下,老板希望这样做。我看了很多不同的网站,没有一个能真正找到答案。此外,最好指出我是C++的新手,只知道相当基本的编码和调试。

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

以下是我的输出:

1(jibberish)
2(jibberish)
3(jibberish)
4(jibberish)
5(jibberish)

在我使用(jibberish)的地方,控制台没有形成任何可识别的字母,只是形成奇怪的形状(如果有用的话,输出的最后一个元素是心形)。

以下是应该的样子:

1 a
2 b
3 c
4天
5 e

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

有什么方法可以让这些数组正确排列吗?我一天中的大部分时间都在努力解决这个问题,我相信这是一件非常简单的事情,但我的大脑被击中了。

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

#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(jibberish)
2(jibberish)
3(jibberish)
4(jibberish)
5 e

EDIT 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;
}
}

最好的解决方案可能是更改

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]);

参见此行:

alph[minIndex] = num[startScan];

第二条故障线路:

alph[startScan] = alph[index];

应该是:

alph[startScan] = alph[minIndex];

当代码退出内部循环时,size的值超过了数组的大小。

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