为什么我的选择排序返回的值不在原始向量中?
Why is my selection sort returning a value that is not in the original vector?
我已经对它进行了一段时间的修改,我已经很接近了!现在输出似乎连续打印零作为 "sorted" 向量的第一个值。这是关于如何在 C++ 中创建选择排序的作业。
示例输出
矢量:6、2、11、1、12、4
排序向量:0、2、11、6、12、4
代码
void selectionSort (vector<int>& data)
{
int min, temp, n=data.size(), i, j;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (data[min]>data[j])
{
min=j;
}
temp=data[min];
data[min]=data[i];
data[i]=temp;
}
return;
}
}
int main()
{
int n;
vector<int> data;
cout<<"Vector length?: "<<endl;
cin>>n;
srand(time(0));
for (int i=0; i<n; i++)
{
data.push_back(rand()%20+1);
}
cout<<"Vector: "<<endl;
for (int i=0; i<n; i++)
{
cout<<data[i]<<" "<<endl;
}
selectionSort(data);
cout<<"Selection Sorted Vector: "<<endl;
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<" "<<endl;
}
system("Pause");
return 0;
}
给定一个包含 n
个元素的数组,选择排序执行 n
个交换。
- 您执行的交换远不止于此。
- 您还意外提前致电
return
。
让我们看一下排序的正确实现:
#include <iostream>
#include <vector>
using namespace std;
void selectionSort (vector<int>& data)
{
const int n = data.size();
for (int i=0; i<n; i++)
{
int min = i;
for (int j=i+1; j<n; j++)
if (data[min]>data[j])
min = j;
swap(data[min], data[i]);
}
}
int main() {
vector<int> data = {6, 2, 11, 1, 12, 4};
selectionSort(data);
for (auto element : data)
cout << element << " ";
cout << "\n";
}
输出:
1 2 4 6 11 12
考虑以下选择排序的正确实现并将其与您的比较:
#include <iostream>
#include <vector>
void selection_sort(std::vector<int> &numbers)
{
// Iterate through all possible start indices.
for (int i = 0; i < numbers.size(); ++i)
{
// Determine the index of the minimum for this iteration.
int index_of_min = i;
for (int j = i; j < numbers.size(); ++j)
{
if (numbers[j] < numbers[index_of_min])
{
index_of_min = j;
}
}
// Swap the minimum element with the element at the start index.
int temp = numbers[i];
numbers[i] = numbers[index_of_min];
numbers[index_of_min] = temp;
}
}
int main()
{
std::vector<int> numbers = { 20, 17, 13, 12, 25 };
selection_sort(numbers);
for (size_t i = 0; i < numbers.size(); ++i)
{
std::cout << numbers[i] << " ";
}
}
我已经对它进行了一段时间的修改,我已经很接近了!现在输出似乎连续打印零作为 "sorted" 向量的第一个值。这是关于如何在 C++ 中创建选择排序的作业。
示例输出
矢量:6、2、11、1、12、4
排序向量:0、2、11、6、12、4
代码
void selectionSort (vector<int>& data)
{
int min, temp, n=data.size(), i, j;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (data[min]>data[j])
{
min=j;
}
temp=data[min];
data[min]=data[i];
data[i]=temp;
}
return;
}
}
int main()
{
int n;
vector<int> data;
cout<<"Vector length?: "<<endl;
cin>>n;
srand(time(0));
for (int i=0; i<n; i++)
{
data.push_back(rand()%20+1);
}
cout<<"Vector: "<<endl;
for (int i=0; i<n; i++)
{
cout<<data[i]<<" "<<endl;
}
selectionSort(data);
cout<<"Selection Sorted Vector: "<<endl;
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<" "<<endl;
}
system("Pause");
return 0;
}
给定一个包含 n
个元素的数组,选择排序执行 n
个交换。
- 您执行的交换远不止于此。
- 您还意外提前致电
return
。
让我们看一下排序的正确实现:
#include <iostream>
#include <vector>
using namespace std;
void selectionSort (vector<int>& data)
{
const int n = data.size();
for (int i=0; i<n; i++)
{
int min = i;
for (int j=i+1; j<n; j++)
if (data[min]>data[j])
min = j;
swap(data[min], data[i]);
}
}
int main() {
vector<int> data = {6, 2, 11, 1, 12, 4};
selectionSort(data);
for (auto element : data)
cout << element << " ";
cout << "\n";
}
输出:
1 2 4 6 11 12
考虑以下选择排序的正确实现并将其与您的比较:
#include <iostream>
#include <vector>
void selection_sort(std::vector<int> &numbers)
{
// Iterate through all possible start indices.
for (int i = 0; i < numbers.size(); ++i)
{
// Determine the index of the minimum for this iteration.
int index_of_min = i;
for (int j = i; j < numbers.size(); ++j)
{
if (numbers[j] < numbers[index_of_min])
{
index_of_min = j;
}
}
// Swap the minimum element with the element at the start index.
int temp = numbers[i];
numbers[i] = numbers[index_of_min];
numbers[index_of_min] = temp;
}
}
int main()
{
std::vector<int> numbers = { 20, 17, 13, 12, 25 };
selection_sort(numbers);
for (size_t i = 0; i < numbers.size(); ++i)
{
std::cout << numbers[i] << " ";
}
}