// 方法一
func bubbleSort(sortedList: inout [Int]){
var i : Int =1
var flag = true
while i < sortedList.count && flag {
flag = false
for j in0..< sortedList.count-i {if sortedList[j]> sortedList[j+1]{
flag = true
sortedList.swapAt(j, j+1)}}
i++}}// 方法二
func bubbleSort(data :[Int])->[Int]{
var list = data
for i in0..< list.count {for j in i+1..< list.count {if list[j]> list[i]{
let temp = list[j]
list[j]= list[i]
list[i]= temp
}}}return list
}
// 方法一
func simpleSelectSort(sortedList : inout [Int]){for j in0..< sortedList.count-1{for i in j+1..< sortedList.count {if sortedList[j]> sortedList[i]{
sortedList.swapAt(j, i)}}}}// 方法二
func chooseSort(data:[Int])->[Int]{
var list = data
for i in0..< list.count {// 记录当前最小的数,比较i+1后更大的数进行记录
var min = i
for j in i+1..< list.count {if list[j]< list[min]{
min = j
}}
let temp = list[min]
list[min]= list[i]
list[i]= temp
}return list
}
// 方法一:通过移动
func insertSort(sortedList: inout [Int]){for j in1..< sortedList.count {if sortedList[j]< sortedList[j-1]{
let temp = sortedList[j]
var i : Int = j-1while i >=0&& sortedList[i]> temp {
sortedList[i+1]= sortedList[i]
i--}
sortedList[i+1]= temp
}}}// 方法二:通过交换
func insertSort(data:[Int])->[Int]{
var list = data
for i in1..<list.count {// 从i往前找,符合条件交换
var y = i
while y >0&& list[y]> list[y-1]{
let temp = list[y]
list[y]= list[y-1]
list[y-1]= temp
y -=1}}return list
}// 方法三
func insertSort(list:[Int])->[Int]{// 建立一个空数,符合条件的插入,没插入的尾后添加
var nlist =[list[0]]for i in1..< list.count {
var max: Int?= nil
for j in0..< nlist.count {if list[i]> nlist[j]{
max = i
nlist.insert(list[i], at: j)break}}if max == nil {
nlist.append(list[i])}}return nlist
}
// 方法一
func shellSort(sortedList : inout [Int]){
let length = sortedList.count
var increment = length
repeat {
increment = increment/3+1for i in0..< length {if i >= increment && sortedList[i]< sortedList[i-increment]{
let temp = sortedList[i]
var j : Int = i-increment
while j >=0&& sortedList[j]> temp {
sortedList[j+increment]= sortedList[j]
j -= increment
}
sortedList[j+increment]= temp
}}}while increment >1}// 方法二
func shellSort(arr: inout [Int]){
var j: Int
// 获取增量
var gap = arr.count /2while gap >0{for i in0..< gap {
j = i + gap
while j < arr.count {if arr[j]< arr[j - gap]{
let temp = arr[j]
var k = j - gap
// 插入排序while(k >=0&& arr[k]> temp){
arr[k + gap]= arr[k]
k -= gap
}
arr[k + gap]= temp
}
j += gap
}}// 增量减半
gap /=2}}
最好时间复杂度:快速排序不断的把数组分成两边,相当于一棵二叉树,由二叉树的知识可以知道,完全二叉树的深度最小,为depth = log n +1,也就是说,当数据比较均匀的分布在二叉树的左右两边,则时间复杂度最小。假设快速排序的时间复杂度为:T(n),第一次需要遍历整个数据,然后把数据分成均匀的两部分,则时间复杂度为:T(n)=2T(n/2)+n,同理,T(n/2)=2T(n/4)+n/2,T(n/4)=2T(n/8)+n/8,则T(n)=2T(n/2)+n=2(2T(n/4)+n/2)+n=4T(n/4)+2n=4(2T(n/8)+n/4)+2n=8T(n/8)+3n=…=nT(n/n)+nlogn=nT(1)+nlogn=nlogn。因为完全二叉树的深度为logn,所以递归调用了logn次,并且直到分到叶子结点,也就是T(1),T(1)=0,所以,T(n)=nlogn。因此,快速排序的时间复杂度为:O(nlogn)。
func bucketSort(sortedList:[Int])->[Int]{
let max = sortedList.max()!
let min = sortedList.min()!
let bucketSize =20
let bucketCount =(max-min)/bucketSize+1
var buckets = Array.init(repeating:[Int](), count: bucketCount)for num in sortedList {
let i =(num-min)/bucketSize
var bucket = buckets[i]
bucket.append(num)
buckets[i]= bucket
}
var result =[Int]()for var bucket in buckets {// 桶里采用插入排序insertSort(sortedList:&bucket)
result.append(contentsOf: bucket)}return result
}
func radixSort(sortedList:[Int])->[Int]{
let max = sortedList.max()!
var result = Array.init(sortedList)
var buckets = Array.init(repeating:[Int](), count:10)
let maxDigit ="\(max)".count
for i in0..< maxDigit {// d
let mod =(pow(10, i+1) as NSDecimalNumber).intValue
for num in result {// n
let j = num%mod/(mod/10)
var bucket = buckets[j]
bucket.append(num)
buckets[j]= bucket
}
var index =0for j in0..< buckets.count {// k
let bucket = buckets[j]for k in0..<bucket.count {
result[index+k]= bucket[k]}
index = index+bucket.count
buckets[j]=[]}}return result
}