以下 C++ 代码,尝试对有 n 个整数的数组 arr 进行排序。这个代码实现了选择排序算法。
n
arr
for (int i = 0; i < n - 1; ++i) { int minIndex = i; for (int j = i + 1; j < n; ++j) { if (arr[j] < arr[minIndex]) minIndex = j; } if (minIndex != i) swap(arr[i], arr[minIndex]); }
正确 √
错误 ×