C++ 使用函数实现选择排序。?
我正在执行选择排序算法步骤 我做了两个函数:一个用于查找最小值的索引,另一个用于选择排序 输出不正确。我发现了错误,但我不知道如何修复。 错误在find_min函数中。我想我得更新索引了 请帮忙C++ 使用函数实现选择排序。?,c++,C++,我正在执行选择排序算法步骤 我做了两个函数:一个用于查找最小值的索引,另一个用于选择排序 输出不正确。我发现了错误,但我不知道如何修复。 错误在find_min函数中。我想我得更新索引了 请帮忙 int find_min(int ar[], int size) { int index = 0; bool found = false; for(int i = 1; i < size; i++) { if(ar[index] > ar[i]
int find_min(int ar[], int size)
{
int index = 0;
bool found = false;
for(int i = 1; i < size; i++)
{
if(ar[index] > ar[i])
{
index = i;
found = true;
}
}
if (found)
{
return index;
}
else
return -1;
}
void selection_sort(int arr[], int size)
{
int loc;
for(int count = 0; count < size; count++)
{
loc = find_min(arr, size - count);
if (loc >= 0)
{
exchange(arr[count], arr[loc]);
}
}
}
int find_min(int ar[],int size)
{
int指数=0;
bool-found=false;
对于(int i=1;iar[i])
{
指数=i;
发现=真;
}
}
如果(找到)
{
收益指数;
}
其他的
返回-1;
}
无效选择\排序(整数arr[],整数大小)
{
int loc;
对于(int count=0;count=0)
{
交换(arr[计数]、arr[定位];
}
}
}
考虑以下代码:
#include <iostream>
using namespace std;
// Exchanges two values in array arr, specified by indices i and j
void exchange(int arr[], int i, int j)
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
// Prints an array arr of length len
void print_array(int arr[], int len)
{
for(int i=0; i<len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
// Finds the index of the minimum value in array arr of length len between indices
// init and the last element (len-1)
int find_min(int arr[], int len, int init)
{
int index = init;
for(int i = init; i < len; i++)
{
if(arr[index] > arr[i])
{
index = i;
}
}
return index;
}
// Sort the array using the selection sort algorithm
void selection_sort(int arr[], int len)
{
int loc;
// We don't need the last iteration when there is only a single element left
for(int pos = 0; pos < (len-1); pos++)
{
cout << "Before iteration " << pos << " array is: ";
print_array(arr, len);
loc = find_min(arr, len, pos);
cout << " Minimum between indices " << pos << " and " << (len-1) << " is: ";
cout << arr[loc] << " at index " << loc << endl;
if (loc != pos)
{
cout << " Exchanging indices " << loc << " and " << pos << endl;
exchange(arr, pos, loc);
}
else
{
cout << " No exchange necessary this iteration. " << endl;
}
cout << "After iteration " << pos << " array is: ";
print_array(arr, len);
// Print an extra line newline, space things out a bit
cout << endl;
}
}
int main(void)
{
int arr[] = {2,6,7,3,1,8};
selection_sort(arr, 6);
}
#包括
使用名称空间std;
//交换数组arr中由索引i和j指定的两个值
无效交换(整数arr[],整数i,整数j)
{
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
//打印长度为len的数组arr
无效打印数组(int arr[],int len)
{
对于(int i=0;i修复find_min
函数
int find_min(int ar[],int s, int size) // s denotes the start index
{
int index = s-1; //take min pos as start
bool found = false;
for(int i = s; i < size; i++)
{
if(ar[index] > ar[i])
{
index = i;
found = true;
}
}
if (found)
{
return index;
}
else
return -1;
}
int find_min(int ar[],int s,int size)//s表示开始索引
{
int index=s-1;//以最小位置为起点
bool-found=false;
对于(int i=s;iar[i])
{
指数=i;
发现=真;
}
}
如果(找到)
{
收益指数;
}
其他的
返回-1;
}
现在使用:
loc=find_min(arr,count+1,size);
直到count
arr已排序
请参见find_min
应为std::min_element
@chris如果OP实际替换了它,只需名称:dh最小的元素究竟如何才能“not found”?数组中的某个元素不是总是最小的吗?出于某种深不可测的原因,find_min
选择在最小元素位于索引0时返回-1。我想使用自己的函数。.“find_min”不是来自库。:/但它不起作用。.感觉就像我在海洋中心&溺水。:(
int find_min(int ar[],int s, int size) // s denotes the start index
{
int index = s-1; //take min pos as start
bool found = false;
for(int i = s; i < size; i++)
{
if(ar[index] > ar[i])
{
index = i;
found = true;
}
}
if (found)
{
return index;
}
else
return -1;
}