意圖是創建一個隨機數組,並按升序排序 數組已創建,但排序不起作用(數字以隨機順序打印) 我有錯誤地應用排序?脫穎而出數組排序功能 - C++
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std;
void mySort(long x[])
{
long min(0), temp(0), minPosition(0), i(0), j(0);
min = x[0];
for (j = 0; j < 10; j++)
{
for (i = j; i < 10; i++)
{
if (x[i] < min)
{
min = x[i];
minPosition = i;
}
}
temp = x[minPosition];
x[minPosition] = x[j];
x[j] = temp;
}
}
int main()
{
long *myArray = new long[10];
int i(0);
srand((unsigned int)time(NULL));
for (i = 0; i < 10; i++)
{
myArray[i] = rand()%11;
}
mySort(myArray);
for (i = 0; i < 10; i++)
{
cout<<'['<<myArray[i]<<']'<<endl;
}
return 0;
}