如何修复我的选择排序?仅返回"0"
How do I fix my selection sort? only returns "0"
你好,作为一名计算机科学专业的一年级学生,我对c++编码还很陌生,我正在一个实验室工作,在这个实验室里,我必须填充一个数组,进行多种类型的排序和搜索,而我一直坚持选择排序,因为我的代码只会返回0。感谢您的帮助<3^_^
如何使用
- 第一个选项";1〃;填充数组
- 输入10个数字(最好按乱序输入
- 代码将返回主菜单
- 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';
相关文章:
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- C++ 合并排序返回原始向量
- 算法中的排序函数返回错误
- 排序函数未正确返回数组
- 合并排序返回错误的值
- 从排序的数字数组中返回数字范围的最快方法是什么?
- 使用霍尔分区方案的快速排序算法返回原始未排序列表
- (C++)如何创建一个函数来接收两个排序的链表并返回出现在两个列表中的第三个元素列表
- 合并排序 - 返回新数组,而不是将合并的数组复制到输入数组
- 为什么我的选择排序返回原始向量中不在的值
- 如何按值对**boost::unorderede_map**进行排序,并按该顺序仅返回键
- c++std::使用函数返回值对std::向量进行排序
- 为什么一位数组返回零?合并排序测试
- 返回指向排序列表的指针;C++中的链表
- 为什么我的合并排序实现返回奇怪的数字
- 当前尝试使用气泡排序对数组进行排序,然后获取平均值,但返回的平均值是错误的
- std::使用个人类排序返回分段错误
- 不知道如何从气泡排序函数返回值
- 如何在std::map上迭代返回基于键值排序的元素
- 对std::集进行迭代是如何返回排序结果的