题解 | #排序#
排序
https://www.nowcoder.com/practice/2baf799ea0594abd974d37139de27896
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 将给定数组排序 * @param arr int整型vector 待排序的数组 * @return int整型vector */ vector<int> MySort(vector<int>& arr) { int n = arr.size(); bool changed = true; for (int i = n-1; i >= 0; i--) { if (!changed) break; changed = false; for (int j = 0; j < i; j++) { if (arr[j+1] < arr[j]) { std::swap(arr[j+1], arr[j]); changed = true; } } } return arr; } };