选择排序输出不正确
selection sort output is not correct
我正在尝试用C++实现选择排序。我不知道我在这段代码中做错了什么,但结果没有正确排序。请告诉我我做错了什么。谢谢你的帮助。
输出结果:23 31 4 89 2 8 10 11
#include <iostream>
using namespace std;
void printElement(int arr[],int size)
{
for(int i = 0; i < size; i++)
cout << arr[i] << " ";
}
// arr[] = {31, 23, 4, 89, 2, 8, 10, 11}
void selectionSort(int arr[], int size)
{
int i, j, min, min_id, tmp;
for(i = 0; i < size-1; i++){
min = arr[i];
for(int j = i + 1; j < size; j++){
if (arr[j] < min){
min = arr[j];
min_id = j;
}
tmp = arr[i]; //tmp = min; - this is wrong by msl
arr[i] = arr[min_id]; //min = arr[min_id]; this is wrong by msl
arr[min_id] = tmp;
}
printElement(arr, size);
cout << endl;
}
}
int main()
{
int size = 8;
int arr[] = {31, 23, 4, 89, 2, 8, 10, 11};
selectionSort(arr, size);
printElement(arr, size);
system("PAUSE");
return 0;
}
tmp = arr[i]; //tmp = min; - this is wrong by msl
arr[i] = arr[min_id]; //min = arr[min_id]; this is wrong by msl
arr[min_id] = tmp;
以上内容位于循环的错误部分。在选择排序中,交换发生在外循环中,而不是内循环。
相关文章:
- 不正确的比较和交换计数器输出用于快速排序功能
- Windows 10命令提示符的输出不正确
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- C++ - 为什么 unicode 输出不正确?
- 连接的组件程序产生不正确的输出
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 我的叉子输出不正确
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 堆排序,我无法弄清楚我的代码出了什么问题,输出顺序不正确
- C++使用 CEIL 和长度输出不正确的值
- 使用 iconv 进行 UTF8 转换的输出不正确
- C strftime()输出不正确
- 不正确的输出和变量未用Eclipse CDT初始化构造函数
- 当行数和列不匹配时,输出不正确
- 合并排序不正确的输出 c++
- 从堆栈中读取字符后,如何修复不正确的文件输出
- C++ 嵌套循环输出不正确
- 矩阵和矢量乘法,输出不正确的产品
- 我对这个问题有问题,我的输出不正确
- 测试整数是否在范围内时,逻辑错误和不正确输出