我在做选择排序,它完全运行但结果不正确。选择在C++中排序不能正常工作
这是我的代码:
#include<iostream>
using namespace std;
int main()
{
int array[5], temp, min, i, j;
for(i = 0; i <= 4; i++)
{
cout << "Enter the value"<<endl;
cin >> array[i];
}
cout << "Values before sorting:" << endl;
for(i = 0; i <= 4; i++)
cout << array[i] << " ";
cout << endl;
for(i = 0; i <= 4; i++)
{
min = i;
for(j = i + 1; j <= 4; j++)
{
if(array[j] < array[min])
min = j;
if(min != i)
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
cout << "values after sorting" << endl;
for(i = 0; i <= 4; i++)
cout << array[i] << " ";
}
}
谁能告诉我什么是我的代码错误
您应该复制粘贴该代码,而不是创建图像。 – Xorifelse
尝试很多,但无法发布,所以贴图像= D –
#include using namespace std; int main() { \t int array [5],temp,min,i,j; (i = 0; i <= 4; i ++) \t \t { \t \t cout <<“输入值”<< endl; \t \t cin >> array [i]; \t} \t cout <<“排序前的值”<< endl; (i = 0; i <= 4; i ++) \t \t { \t \t cout << array [i] <<“”; \t} \t cout << endl; (i = 0; i <= 4; i ++) \t \t { \t \t min = i; \t \t为(J = + 1;Ĵ<= 4; J ++) \t \t { \t \t \t如果(阵列[j]的<阵列[分钟]) \t \t \t { \t \t \t \t分钟=焦耳; \t \t \t \t \t \t \t} \t \t \t如果(分钟!= 1) \t \t \t { \t \t \t \t温度=阵列[J]; \t \t \t \t array [j] = array [j + 1]; \t \t \t \t array [j + 1] = temp; \t \t \t} \t \t} \t \t COUT << “排序后的值” << ENDL; \t \t为(I = 0; I <= 4;我++) \t \t { \t \t \t COUT <<阵列[I] <<”“; \t \t} \t} } –