swift排序代码

2021-05-19  本文已影响0人  纱云

1.冒泡排序

算法描述

   func sortBybub()  {
        var arr = [0,8,7,9,6,5,3,4,9,1,2,8]
        for i in 0..<arr.count-1 {
            var j = 1;
            while j<arr.count-i {
                if  arr[j]<arr[j-1] {
                    let tem = arr[j];
                    arr[j] = arr[j-1]
                    arr[j-1] = tem;
                }
                j+=1;
            }
        }
    }

2.选择排序

算法描述

 func sortBychoice()  {
        var arr = [1,2,88,65,76,43,45,32,34,37,68,65,49,67,20,35,38];
        for i in 0..<arr.count-1 {
            var index = i
            for j in i+1..<arr.count {
                if arr[j]<arr[index] {
                    index = j
                }
            }
            if index != i {
                let tem = arr[index]
                 arr[index] = arr[i];
                 arr[i] = tem;
            }
           
        }
        print("choice==\(arr)")
    }

3.插入排序

 func sortByinsert()  {
        var arr = [4,2,3,6,5,7,1,9,8]
        for i in 0..<arr.count-1 {
            var j=i+1;
            while j > 0 {
                if arr[j]<arr[j-1] {
                    let tem = arr[j];
                    arr[j] = arr[j-1]
                    arr[j-1] = tem;
                    j-=1;
                }else{
                    break;
                }
            }
        }
        print("arrsss===\(arr)");
    }

4 希尔排序

    func sortByxe() {
        var arr = [1,2,88,65,76,43,45,32,34,37,68,65,49,67,20,35,38];
        var step = arr.count/2
        while step>0 {
            for i in 0..<arr.count {
               var j = i+step;
                while j>=step&&j<arr.count {
                    if arr[j-step]>arr[j] {
                        let tem = arr[j-step]
                        arr[j-step] = arr[j]
                        arr[j] = tem;
                        j=j-step;
                    }else{
                        break
                    }
                }
            }
            step = step/2;
        }
        print("xr ==\(arr)")

    }

5.归并排序

    func sortBycombine(){
        var arr = [1,2,88,65,76,43,45,32,34,37,68,65,49,67,20,35,38];

      let result =  mergeSort(array: arr)
        print("result\(result)")
    }
    //快速排序

    func mergeSort(array:[Int])->[Int]{
        
        if array.count<2 {
            return array
        }
        let middle = array.count/2
        print("data==\(array)")
        let left = mergeSort(array: Array(array[0..<middle]))
        let right = mergeSort(array: Array(array[middle..<array.count]))

        return merge(left: left, right: right)
        
    }
    func merge(left:[Int],right:[Int]) -> [Int] {
        var leftindex = 0
        var right_index = 0
        var combin_arr = [Int]()
        while leftindex<left.count&&right_index<right.count {
            if left[leftindex]>right[right_index] {
                combin_arr.append(right[right_index])
                right_index+=1
            }else{
                combin_arr.append(left[leftindex])
                leftindex+=1
            }
        }
        while leftindex<left.count {
            combin_arr.append(left[leftindex])
            leftindex+=1
        }
        
        while right_index<right.count {
            combin_arr.append(right[right_index])
            right_index+=1
        }
     
        return combin_arr

    }

6.快速排序

func sortByquick() {
        var arr = [1,2,88,65,76,43,45,32,34,37,68,65,49,67,20,35,38];
        quickSort(arr: &arr, low: 0, high: arr.count-1)
    }
    func quickSort(arr:inout [Int],low:Int,high:Int) {
        if low>high {
            return;
        }
       let index = findIndex(arr: &arr, low: low, high: high)
        quickSort(arr: &arr, low: low, high: index-1)
        quickSort(arr: &arr, low: index+1, high: high)
        
    }
    //不稳定排序
    func findIndex(arr:inout [Int],low:Int,high:Int)->Int{
        let num = arr[high]
        
        var currentIndex = low
        for index in low...high {
            if arr[index]<num {
                if currentIndex != index {
                    arr.swapAt(currentIndex, index)
                }
                currentIndex+=1//currentIndex 前都是小于num的值 最后交换high和currentIndex,即可实现currentIndex前都是小于num的,后都是大于的
            }
        }
        
        if currentIndex != high {
            arr.swapAt(currentIndex, high)
        }
        return currentIndex;
    }
上一篇下一篇

猜你喜欢

热点阅读