编辑代码



let array = [ 8, 31, 321, 88, 67, 3, 35, 36, 37]
print("The origin array: \(array)")

func bubbleSort(_ input: Array<Int>) {
    guard input.count > 1 else { 
        print("wrong input")
        return
    }
    var sorted = input

    for i in 0..<sorted.count - 1 {
        var swaped = false
        for j in 0..<sorted.count - i - 1 {
            if sorted[j] < sorted[j + 1] {
                sorted.swapAt(j, j+1)
                swaped = true
            }
        }
        if swaped == false {
            print("Bubble sort result: \(sorted)")
            return
        }
    }

    print("Bubble sort result: \(sorted)")
}

func insertSort(_ input: Array<Int>) {
    guard input.count > 1 else {
        print("Wrong input!")
        return
    }
    var sorted = input
    
    for i in 1..<sorted.count {
        let value = sorted[i]
        for j in (0..<i).reversed(){
            if sorted[j] > value {
                sorted[j+1] = sorted[j]
            }else{
                break
            }
            sorted[j] = value
        }
    }

    print("Insert sort result: \(sorted)")
}

func selectSort(_ array: Array<Int>) {
    guard array.count > 1 else {
        print("Wrong input")
        return
    }

    var sorted = array

    for i in 0..<sorted.count {
        var targetIndex = i 
        for j in i..<sorted.count {
            if sorted[j] < sorted[targetIndex] {
                targetIndex = j
            }
        }
        sorted.swapAt(i, targetIndex)
    }
    print("Select sort result: \(sorted)")
}


func quickSort(_ array: Array<Int>) {
    guard array.count > 1 else {
        print("Wrong input")
        return
    }

    var sorted = array

    func quick_sort(_ array: inout Array<Int>, _ low: Int, _ high: Int) {
        guard low < high else {
            return
        }
        let pivot = partition(&array, low, high)
        quick_sort(&array, low, pivot - 1)
        quick_sort(&array, pivot + 1, high) 
    }

    func partition(_ array: inout Array<Int>, _ low: Int, _ high: Int) -> Int {
        let value = array[high]
        var pivot = low
        for i in low..<high {
            if array[i] < value {
                array.swapAt(i, pivot)
                pivot+=1
            }
        }
        array.swapAt(high, pivot)
        return pivot
    }

    quick_sort(&sorted, 0, sorted.count - 1)
    print("Quick sort result: \(sorted)")
}

func mergeSort(_ array: Array<Int>) {
    guard array.count > 1 else {
        print("Wrong input")
        return
    }
    // var sorted = array

    func merge_sort(_ array: Array<Int>) -> Array<Int> {
        guard array.count > 1 else {
            return array
        }
        let mid = (array.count)/2
        let leftArray = Array(array[0..<mid])
        let rightArray = Array(array[mid..<array.count])
        return merge(merge_sort(leftArray), merge_sort(rightArray))
    }

    func merge(_ left: Array<Int>, _ right: Array<Int>) -> Array<Int> {
        var temp = Array<Int>()
        var leftIndex = 0
        var rightIndex = 0 
        while leftIndex < left.count && rightIndex < right.count {
            if left[leftIndex] > right[rightIndex] {
                temp.append(left[leftIndex])
                // print("left : \(leftIndex)  \(temp)")
                leftIndex += 1
            }else {
                temp.append(right[rightIndex])
                // print(rightIndex, temp)
                rightIndex += 1
            }
        }
        if leftIndex < left.count {
            // temp.append(contentsOf:array[leftIndex...mid])
            for i in (leftIndex..<(left.count)) {
                temp.append(left[i])
            }
        }
        else {
            // temp.append(contentsOf:array[rightIndex...high])
            for i in (rightIndex..<(right.count)) {
                temp.append(right[i])
            }
        }
        // for i in low...high {
        //     array[i] = temp[i]
        // }
        // print("temp \(temp)")
        return temp
    } 

    print("Merge sort result: \( merge_sort(array))")
}

bubbleSort(array)
insertSort(array)
selectSort(array)
quickSort(array)
mergeSort(array)