如何在没有可用方法的情况下手动快速执行整数排序数组? [英] how to perform sort array of integer in swift manually without methods available?
本文介绍了如何在没有可用方法的情况下手动快速执行整数排序数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入:
let arrayInt = [7,8,3,4,5,9,1,2,6]
输出
let newArray = [1,2,3,4,5,6,7,8,9]
如何不使用Swift中可用的 .sort
方法来做到这一点?我只是在编程测试中失败了,所以我想知道答案:(
how to do that WITHOUT using .sort
method that available in Swift? I just failed in programming test, so I want to know the answer :(
推荐答案
嘿,这可能对您有所帮助多于1种可能性:
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Sorting%20Algorithms/sorting.html
Hey look at this may help you, there are more then 1 possibilities: https://www.cs.cmu.edu/~adamchik/15-121/lectures/Sorting%20Algorithms/sorting.html
有示例:
https://gist.github.com/tmdvs/d8edeb9bf26f2f5c3e50
编辑:这里有个例子:
var unsortedArray = [7,8,3,4,5,9,1,2,6]
for i in stride(from: unsortedArray.count-1, to: 0, by: -1) {
for j in 1...i {
if unsortedArray[j-1] > unsortedArray[j] {
let tmp = unsortedArray[j-1]
unsortedArray[j-1] = unsortedArray[j]
unsortedArray[j] = tmp
}
}
}
之后,unsortedArray为排序。
气泡排序
After that the unsortedArray is sorted. Bubble Sort
这篇关于如何在没有可用方法的情况下手动快速执行整数排序数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文