++中的选择排序并行数组
Selection Sort Parallel Arrays in ++
免责声明:我知道并行数组很糟糕,应该避免,选择排序不是最有效的排序,但在这种情况下,这就是老板想要的方式它完成了。我查看了很多不同的网站,none 似乎确实找到了答案。另外,最好指出我是 C++ 的新手,只知道相当基本的编码和调试。
我有两个简单的并行数组,我正在尝试设计一个简单的选择排序来对其中一个数组进行排序,然后相应地交换第二个数组中的元素。我有选择排序部分工作,但它似乎没有正确交换我的第二个数组中的元素。
这是我的输出结果:
1 (jibberish)
2 (jibberish)
3 (jibberish)
4 (jibberish)
5 (jibberish)
我的地方(乱码)控制台没有形成任何可识别的字母,只有奇怪的形状(如果有帮助,输出的最后一个元素是一颗心)。
这是应该的样子:
1 a
2 b
3 c
4 d
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
编辑 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;
}
}
看到这一行:
alph[minIndex] = num[startScan];
第二个故障线路:
alph[startScan] = alph[index];
应该是:
alph[startScan] = alph[minIndex];
当代码退出内循环时,size 的值超出了数组大小。
我的建议:使用 IDE 和调试器跟踪代码执行并检查变量。另外,我的第一个提示应该让您查看了不正确的索引。默认情况下,C++ 不关心检查数组边界。当越界或跟随不正确的指针时,你通常会得到垃圾。您可以通过选择一个编译器选项来检查数组边界来解决第一个问题。这会在开发期间减慢您的应用程序,但可以在一切正常运行后将其删除。
最好的解决办法可能是改变你的
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]);
免责声明:我知道并行数组很糟糕,应该避免,选择排序不是最有效的排序,但在这种情况下,这就是老板想要的方式它完成了。我查看了很多不同的网站,none 似乎确实找到了答案。另外,最好指出我是 C++ 的新手,只知道相当基本的编码和调试。
我有两个简单的并行数组,我正在尝试设计一个简单的选择排序来对其中一个数组进行排序,然后相应地交换第二个数组中的元素。我有选择排序部分工作,但它似乎没有正确交换我的第二个数组中的元素。
这是我的输出结果:
1 (jibberish)
2 (jibberish)
3 (jibberish)
4 (jibberish)
5 (jibberish)
我的地方(乱码)控制台没有形成任何可识别的字母,只有奇怪的形状(如果有帮助,输出的最后一个元素是一颗心)。
这是应该的样子:
1 a
2 b
3 c
4 d
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
编辑 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;
}
}
看到这一行:
alph[minIndex] = num[startScan];
第二个故障线路:
alph[startScan] = alph[index];
应该是:
alph[startScan] = alph[minIndex];
当代码退出内循环时,size 的值超出了数组大小。
我的建议:使用 IDE 和调试器跟踪代码执行并检查变量。另外,我的第一个提示应该让您查看了不正确的索引。默认情况下,C++ 不关心检查数组边界。当越界或跟随不正确的指针时,你通常会得到垃圾。您可以通过选择一个编译器选项来检查数组边界来解决第一个问题。这会在开发期间减慢您的应用程序,但可以在一切正常运行后将其删除。
最好的解决办法可能是改变你的
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]);