编辑代码

let origin = [8, 54, 4, 3, 2, 1, 45, 55, 456, 32]
print("Origin array: \(origin)")

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
    }

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

    func merge(_ left: Array<Int>, _ right: Array<Int>) -> Array<Int>  {
        var result = [Int]()
        var leftIndex = 0
        var rightIndex = 0
        while leftIndex < left.count && rightIndex < right.count {
            if left[leftIndex] < right[rightIndex] {
                result.append(left[leftIndex])
                leftIndex += 1
            }else {
                result.append(right[rightIndex])
                rightIndex += 1
            }
        }

        if leftIndex < left.count {
            for i in (leftIndex..<left.count) {
                result.append(left[i])
            }
        }
        if rightIndex < right.count {
            for i in (rightIndex..<right.count) {
                result.append(right[i])
            }
        }
        return result
    }

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

func insertSort(_ array: Array<Int>) {
    guard array.count > 1 else {
        print("Wrong input")
        return
    }
    var sorted = array 
    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]
                sorted[j] = value
            }else {
                break
            }
        }
    }

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

func bubbleSort(_ array: Array<Int>) {
    guard array.count > 1 else {
        print("Wrong Input")
        return
    }
    var sorted = array 
    for i in 0..<sorted.count - 1{
        for j in 1..<sorted.count - i {
            if sorted[j] > sorted[j-1] {
                sorted.swapAt(j, j-1)
            }
        }
    }

    print("Bubble 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 index = i
        for j in i..<sorted.count {
            if sorted[j] > sorted[index] {
                index = j
            }
        }
        sorted.swapAt(i, index)
    }
    print("Select sort result: \(sorted)")
}

quickSort(origin)
mergeSort(origin)
insertSort(origin)
bubbleSort(origin)
selectSort(origin)