题解 | #对其元素按照升序或降序进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309?tpId=37&tqId=21324&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fdifficulty%3D1%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=1&judgeStatus=undefined&tags=&title=
import Foundation while let line = readLine() { var array = readLine()!.components(separatedBy: " ") let flag = Int(String(readLine()!))! for i in 0..<array.count { for j in 0..<array.count-i-1 { let first = Int(array[j])! let second = Int(array[j+1])! if (first > second) && flag == 0 { array.swapAt(j,j+1) } else if (first < second) && flag == 1 { array.swapAt(j,j+1) } } } print(array.joined(separator:" ")) }