如何修复我的选择排序?仅返回"0"

How do I fix my selection sort? only returns "0"

本文关键字:返回 排序 何修复 我的 选择      更新时间:2024-09-27

你好,作为一名计算机科学专业的一年级学生,我对c++编码还很陌生,我正在一个实验室工作,在这个实验室里,我必须填充一个数组,进行多种类型的排序和搜索,而我一直坚持选择排序,因为我的代码只会返回0。感谢您的帮助<3^_^

如何使用

  1. 第一个选项";1〃;填充数组
  2. 输入10个数字(最好按乱序输入
  3. 代码将返回主菜单
  4. DO OPTION;4〃

代码

#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
//Fill In Array
void fillData(int array[])
{
cout << "Enter 10 numbers to fill in the array ";
for (int i = 0; i < 10; i++)
{
cin >> array[i];
}
}
//Linear search 
int lSearch(int array[], int size, int target, int option)
{
if (option == 2)
{
cout << "What number are you looking for?";
cin >> target;
for (int index = 0; index < size; index++)
if (target == array[index])
return index;
}
else
return -1;
}
//Binary Search 
int bSearch(const int array[], int size, int value)
{
cout << "What number are you looking for?";
cin >> value; 

int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false; 
while (!found && first <= last)
{
middle = (first + last) / 2; 
if (array[middle] == value)
{
found = true;
position = middle;
}
else if (array[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
//Selection Sort
int selectionSort(int array[], int size )
{
int i{};
for (int i = 0; i < size; i++)
{
int smallest = array[i]; 
int smallestIndex = i; 

for (int m = i; m < 10; m++)
{
if(array[m] < smallest)
{
smallest = array[m]; 
smallestIndex = m; 
}
}
swap(array[i], array[smallestIndex]); 
}
return array[i]; 

}
//everything together 
int main()
{
int option{};
int array[10];
int result;
int target{};
//display menu
do
{
cout << setw(20) << setfill('-') << "" << "MENU" << setw(20) << setfill('-') << "" << endl;
cout << "1. Fill Data" << endl;
cout << "2. Linear Search" << endl;
cout << "3. Binary Search" << endl;
cout << "4. Selection Sort" << endl;
cout << "5. Bubble Sort" << endl;
cout << "6. Display Data" << endl;
cout << endl;
cout << "Which Option Would you like to do? ";
cin >> option;
cout << endl; 

switch (option)
{
case 1:
fillData(array);
break;
case 2:
//Linear search
result = lSearch(array, 10, target, option);
cout << " That number is inside of index # " << result << endl;
cout << endl;
break;

case 3: 
//Binary search
result = bSearch(array, 10, option);
cout << " That number is inside of index # " <<  result << endl;
case 4:
//Selection Sort
result = selectionSort(array, 10);  
cout << "selection sort is " << result << endl; 

}


} while (option != -1);
system("pause");
return 0;
}

问问自己,对一个数组进行排序的结果应该是什么。我希望你能看到它应该是另一个数组。如果将数组{4, 8, 1, 3}(例如(按升序排序,则结果为已排序的数组{1, 3, 4, 8}

但是您的代码假设对数组进行排序的结果是一个整数。

result = selectionSort(array, 10); 

这适用于其他操作,如线性搜索或二进制搜索,其中结果是数组索引,但对于排序来说,这根本没有意义。

您编写的selectionSort函数看起来还可以(尽管我还没有测试它(。这就是所谓的就地排序,这意味着它不创建和返回新的数组,而是修改给定的数组,以便对数组本身进行排序。

这对你的程序意味着你的排序例程根本不需要返回任何东西。相反,它会对给定的数组进行排序。因此,更改selectionSort函数以不返回

void selectionSort(int array[], int size )

并在调用selectionSort以打印传递给selectionSort的数组之后更改代码,现在应该对其进行排序。像这样的

selectionSort(array, 10); 
cout << "selection sort is";
for (int i = 0; i < 10; i++)
cout << ' ' << array[i];
cout << 'n';