我在做選擇排序,它完全運行但結果不正確。選擇在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} } –