欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

iOS 版气泡排序

最编程 2024-04-17 13:05:52
...

algo

冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

算法过程描述
  1. 比较相邻的元素。如果第一个比第二个大,就交换它们两个;
  2. 对每一对相邻元素作同样的操作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数;
  3. 针对所有的元素重复以上的步骤,除了最后一个;
  4. 重复步骤1~3,直到没有任何一对数字需要比较。
动图演示
bubbleSort.gif
复杂度

假设序列有n个元素,n>1,根据算法步骤,第1轮需在n个元素中两两比较(n-1)次,第2轮需要在剩余的(n-1)个元素中两两比较(n-2)次,第(n-1)轮需在最后2个元素中仅比较1次。

函数表达式为:
f(n) = (n-1) + (n-2) +...+ 2 + 1
f(n) = n*(n-1)/2
f(n) = (n² - n)/2

大O表示法,忽略常量、低阶和常数系数。

时间复杂度为:O(n²)
空间复杂度为:并未开辟额外空间, 所以为O(1)
稳定性: 稳定

代码实现(Swift)

假设要对以下数组进行冒泡排序:

let numbers = [1, 4, 3, 2, 0, 5, 6, 7, 8, 9]

n个元素进行冒泡排序,总共需要重复遍历(n-1)轮,每一轮遍历结束后,最后一个元素会是排序数列中最大的元素,下一轮遍历可少遍历一个数,因此第i轮遍历需要比较(n-1-i)次。

func simpleBubbleSort(numbers: [Int]) -> [Int] {
    
    var sortedNumbers = numbers
    
    for i in 0..<sortedNumbers.count - 1 {
        
        print("\n\(sortedNumbers) (\(i)th circle begin)")
        
        for j in 0..<(sortedNumbers.count - 1 - i){
            
            if sortedNumbers[j] > sortedNumbers[j+1] {
                sortedNumbers.swapAt(j, j+1)
                print("\(sortedNumbers) (swap at \(j) and \(j+1))")
            }
        }
    }
    return sortedNumbers
}

let sortedNumbers = simpleBubbleSort(numbers: numbers)
print("\n\(sortedNumbers) (sample bubble sort result)")

终端打印结果:

[1, 4, 3, 2, 0, 5, 6, 7, 8, 9] (0th circle begin)
[1, 3, 4, 2, 0, 5, 6, 7, 8, 9] (swap at 1 and 2)
[1, 3, 2, 4, 0, 5, 6, 7, 8, 9] (swap at 2 and 3)
[1, 3, 2, 0, 4, 5, 6, 7, 8, 9] (swap at 3 and 4)

[1, 3, 2, 0, 4, 5, 6, 7, 8, 9] (1th circle begin)
[1, 2, 3, 0, 4, 5, 6, 7, 8, 9] (swap at 1 and 2)
[1, 2, 0, 3, 4, 5, 6, 7, 8, 9] (swap at 2 and 3)

[1, 2, 0, 3, 4, 5, 6, 7, 8, 9] (2th circle begin)
[1, 0, 2, 3, 4, 5, 6, 7, 8, 9] (swap at 1 and 2)

[1, 0, 2, 3, 4, 5, 6, 7, 8, 9] (3th circle begin)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (swap at 0 and 1)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (4th circle begin)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (5th circle begin)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (6th circle begin)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (7th circle begin)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (8th circle begin)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (sample bubble sort result)

可以看到,第3轮遍历后已经排序完成:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

通过第4轮遍历发现不再进行位置交换,可以确定已为有序数组,那么后面的遍历和比较其实毫无意义了,因此可以增加一个变量来优化冒泡排序。

代码优化

增加一个Bool变量swapped来记录每次是否发生位置交换,如果没有发生位置交换,说明已经是有序数列了,可以提前结束排序。

func betterBubbleSort(numbers: [Int]) -> [Int] {
    
    var sortedNumbers = numbers
    
    for i in 0..<sortedNumbers.count - 1 {
    
        var swapped = false
        
        print("\n\(sortedNumbers) (\(i)th circle begin, 1..<\(sortedNumbers.count-i))");
        
        for j in 0..<sortedNumbers.count - 1 - i {
            
            if sortedNumbers[j] > sortedNumbers[j+1] {
                sortedNumbers.swapAt(j, j+1)
                swapped = true
                print("\(sortedNumbers) (swap at \(j) and \(j+1))")
            }
        }
        
        if !swapped {
            print("排序已提前完成")
            break
        }
    }
    return sortedNumbers
}

let sortedNumbers = betterBubbleSort(numbers: numbers)
print("\n\(sortedNumbers) (better bubble sort result)")

终端打印结果如下:

[1, 4, 3, 2, 0, 5, 6, 7, 8, 9] (0th circle begin, 1..<10)
[1, 3, 4, 2, 0, 5, 6, 7, 8, 9] (swap at 1 and 2)
[1, 3, 2, 4, 0, 5, 6, 7, 8, 9] (swap at 2 and 3)
[1, 3, 2, 0, 4, 5, 6, 7, 8, 9] (swap at 3 and 4)

[1, 3, 2, 0, 4, 5, 6, 7, 8, 9] (1th circle begin, 1..<9)
[1, 2, 3, 0, 4, 5, 6, 7, 8, 9] (swap at 1 and 2)
[1, 2, 0, 3, 4, 5, 6, 7, 8, 9] (swap at 2 and 3)

[1, 2, 0, 3, 4, 5, 6, 7, 8, 9] (2th circle begin, 1..<8)
[1, 0, 2, 3, 4, 5, 6, 7, 8, 9] (swap at 1 and 2)

[1, 0, 2, 3, 4, 5, 6, 7, 8, 9] (3th circle begin, 1..<7)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (swap at 0 and 1)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (4th circle begin, 1..<6)
排序已提前完成

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (better bubble sort result)

算法优化后,虽然数组为有序,可提前结束遍历;

但是仍可以发现,0th轮遍历最后一次交换发生在3, 4位置,即index 4开始后面的为有序数列。1th轮遍历时,仅需遍历1..<4,但可以该算法依然遍历1..<9

因此可以继续优化

代码再优化

增加一个变量lastSwappedIndex来记录最后一次交换的位置,下一轮遍历时,只需遍历1..<lastSwappedIndex即可。

func bestBubbleSort(numbers: [Int]) -> [Int] {
    
    var sortedNumbers = numbers
    
    var lastSwappedIndex = sortedNumbers.count
    
    for i in 0..<sortedNumbers.count - 1 {
        
        print("\n\(sortedNumbers) (\(i)th circle begin, 1..<\(lastSwappedIndex))");
        
        var swapped = false
        
        for j in 0..<lastSwappedIndex - 1 {
            
            if sortedNumbers[j] > sortedNumbers[j+1] {
                sortedNumbers.swapAt(j, j+1)
                swapped = true
                lastSwappedIndex = j+1
                print("\(sortedNumbers) (swap at \(j) and \(j+1))")
            }
        }
        
        if !swapped || lastSwappedIndex == 1 {
            print("排序已提前完成")
            break
        }
    }
    return sortedNumbers
}

let sortedNumbers = bestBubbleSort(numbers: numbers)
print("\n\(sortedNumbers) (best bubble sort result)")

终端打印结果如下:

[1, 4, 3, 2, 0, 5, 6, 7, 8, 9] (0th circle begin, 1..<10)
[1, 3, 4, 2, 0, 5, 6, 7, 8, 9] (swap at 1 and 2)
[1, 3, 2, 4, 0, 5, 6, 7, 8, 9] (swap at 2 and 3)
[1, 3, 2, 0, 4, 5, 6, 7, 8, 9] (swap at 3 and 4)

[1, 3, 2, 0, 4, 5, 6, 7, 8, 9] (1th circle begin, 1..<4)
[1, 2, 3, 0, 4, 5, 6, 7, 8, 9] (swap at 1 and 2)
[1, 2, 0, 3, 4, 5, 6, 7, 8, 9] (swap at 2 and 3)

[1, 2, 0, 3, 4, 5, 6, 7, 8, 9] (2th circle begin, 1..<3)
[1, 0, 2, 3, 4, 5, 6, 7, 8, 9] (swap at 1 and 2)

[1, 0, 2, 3, 4, 5, 6, 7, 8, 9] (3th circle begin, 1..<2)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (swap at 0 and 1)
排序已提前完成

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (best bubble sort result)

可以看到,0th遍历0..<10,第1th遍历1..<4,后面有序的不再遍历。

回目录:常用的排序算法

结语

路漫漫其修远兮,吾将上下而求索~

作者简书

作者掘金

作者GitHub

.End