swift 归并排序

归并排序,简单来说就是先将数组不断细分成最小的单位,然后每个单位分别排序,排序完毕后合并,重复以上过程最后就可以得到排序结果。该算法采用经典的分治策略(分治法将问题分成一些小的问题然后递归求解,而治的阶段则将分的阶段得到的各答案"修补"在一起,即分而治之)。

(上图摘自网络,如有侵权删除).png

其代码如下:

//排序入口
func mergeSort(arr:inout [Int]) {
        if arr.count <= 1 {
            return
        }
        process(arr: &arr, left: 0, right: arr.count - 1)
    }
    //该方法主要用作二分
    func process(arr:inout [Int],left:Int,right:Int) {
        if left == right {
            return
        }
        let middle = left + (right - left) >> 1
        process(arr: &arr, left: left, right: middle)
        process(arr: &arr, left: middle + 1, right: right)
        merge(arr: &arr, left: left, middle: middle, right: right)
        
        
    }
    //将二分后的数组进行合并
    func merge(arr:inout [Int],left:Int,middle:Int,right:Int){
        var help:[Int] = [Int]()
        
        var leftLocation:Int = left
        var rightLocation:Int = middle + 1
        
        while (leftLocation <= middle && rightLocation <= right) {
            if arr[leftLocation] <= arr[rightLocation] {
                help.append(arr[leftLocation])
                leftLocation += 1
            }else{
                help.append(arr[rightLocation])
                rightLocation += 1
            }
        }
        
        while leftLocation <= middle {
            help.append(arr[leftLocation])
            leftLocation += 1
            
        }
        while rightLocation <= right {
            help.append(arr[rightLocation])
            rightLocation += 1
        }
        var index = left
        for item in help {
            arr[index] = item
            index += 1
        }
        
    }

其核心为merge方法:

  • 1、准备两个变量分别指向二分后数组的左端,上方代码分别为leftLocationrightLocation
  • 2、比较arr[leftLocation]arr[rightLocation]位置上的数谁小,谁小取谁,并位置+1,直到其中一个走到终点;
  • 3、然后把未走到终点的数组添加到help数组中,然后修改回原数组。

具体merge如下图:


image.png

归并排序的非递归实现

//===  通过步长来解决--非递归版的归并排序
    func mergeSortFeidiGui(arr:inout [Int]) {
        if arr.count <= 1 {
            return
        }
        let maxCount = arr.count
        //步长
        var step:Int = 1
        
        while step < maxCount  {
            var left:Int = 0
            while left < maxCount {
                let middle = left + step - 1
                if middle > maxCount {
                    break
                }
                let right:Int = (middle + step) > (maxCount - 1) ? (maxCount - 1) : (middle + step)
                
                merge(arr: &arr, left: left, middle: middle, right: right)
                left = right + 1
            }
            if step > (maxCount / 2) {
                break
            }
            step = step << 1
        }
        
    }

其核心思想-借助于步长的概念:

  • 1、先让步长为1,然后使数组0-1、2-3、4-5、6-7、…………n-1到n,范围上的数组有序
  • 2、然后步长翻倍,使数组0-4、4-8、8-12、…………n-3到N范围上的数有序
  • 3、然后再步长翻倍,使数组有序
  • 4、不断循环直到数组有序

小和问题

在一个数组中,每一个数左边比当前数小的数累加起来,叫做这个数组的小和。求一个数组 的小和。
例子:
[1,3,4,2,5]
1左边比1小的数,没有;
3左边比3小的数,1;
4左边比4小的数,1、3;
2左边比2小的数,1;
5左边比5小的数,1、3、4、2;
所以小和为1+1+3+1+1+3+4+2=16

func getArrMinCount(arr:inout [Int]) -> Int{
        if arr.count < 2 {
            return 0
        }
        return getProcess(arr: &arr,left: 0,right: arr.count - 1)
    }
    //该方法主要用作二分
    func getProcess(arr:inout [Int],left:Int,right:Int) -> Int {
        if left == right {
            return 0
        }
        let middle = left + (right - left) >> 1
        let leftCount = getProcess(arr: &arr, left: left, right: middle)
        let rightCount = getProcess(arr: &arr, left: middle + 1, right: right)
        let mergeCount = getMerge(arr: &arr, left: left, middle: middle, right: right)
        return leftCount + rightCount + mergeCount
    }
    
    func getMerge(arr:inout [Int],left:Int,middle:Int,right:Int) -> Int{
        var res:Int = 0
        var help:[Int] = [Int]()
        var leftLocation:Int = left
        var rightLocation:Int = middle + 1
        while leftLocation <= middle && rightLocation <= right {
            if arr[leftLocation] < arr[rightLocation] {
                help.append(arr[leftLocation])
                let numberCount = right - rightLocation + 1
                res += arr[leftLocation] * numberCount
                print("===>\(arr[leftLocation]) --- > \(numberCount)")
                
                leftLocation += 1
                
            }else if arr[leftLocation] == arr[rightLocation]{
                help.append(arr[rightLocation])
                rightLocation += 1
            }else{
                help.append(arr[rightLocation])
                rightLocation += 1
            }
        }
        while leftLocation <= middle {
            help.append(arr[leftLocation])
            leftLocation += 1
        }
        while rightLocation <= right {
            help.append(arr[rightLocation])
            rightLocation += 1
        }
        var index:Int = left
        for item in help {
            arr[index] = item
            index += 1
        }
        
        return res
    }

在num数组中任意位置右边的数*2后依然比该位置数小,求数组中这样数的个数


    func getArrNumberDouble(arr:inout [Int]) -> Int{
        if arr.count < 2 {
            return 0
        }
        return getNumberDoubleProcess(arr: &arr,left: 0,right: arr.count - 1)
    }
    //该方法主要用作二分
    func getNumberDoubleProcess(arr:inout [Int],left:Int,right:Int) -> Int {
        if left == right {
            return 0
        }
        let middle = left + (right - left) >> 1
        let leftCount = getNumberDoubleProcess(arr: &arr, left: left, right: middle)
        let rightCount = getNumberDoubleProcess(arr: &arr, left: middle + 1, right: right)
        let mergeCount = getNumberDoubleMerge(arr: &arr, left: left, middle: middle, right: right)
        return leftCount + rightCount + mergeCount
    }
    func getNumberDoubleMerge(arr:inout [Int],left:Int,middle:Int,right:Int) -> Int{
        var help:[Int] = [Int]()
        var res:Int = 0
        
        var l:Int = left
        var r:Int = middle + 1
        
        //[26,9,4,1]
        print(" OOOOOOOOOO--- L >\(left) --- M >\(middle) --- R >\(right)")
        while l <= middle {
            while r <= right && arr[l] > arr[r] * 2{
                r += 1
            }
            res = res + r - middle - 1
            print(" --- > \(res) --- L >\(left) --- M >\(middle) --- R >\(right)")

            l += 1
        }
        
        var leftLocation:Int = left
        var rightLocation:Int = middle + 1
        while leftLocation <= middle && rightLocation <= right {
            if arr[leftLocation] < arr[rightLocation]{
                help.append(arr[leftLocation])
                leftLocation += 1
            }else{
                help.append(arr[rightLocation])
                rightLocation += 1
            }
        }
        while leftLocation <= middle {
            help.append(arr[leftLocation])
            leftLocation += 1
        }
        while rightLocation <= right {
            help.append(arr[rightLocation])
            rightLocation += 1
        }
        var index = left
        for item in help {
            arr[index] = item
            index += 1
        }
        return res
    }

思路如下:
1、在进行merge之前,先对数组中符合要求的数据做过滤并保存
2、边merge边进行符合要求数组的记录保存
符合要求的数据做过滤及保存逻辑如下

    /**
     左数组               右数组
     [100,300,500,800]  [20,200,220,260]
        L                R                     100>20 * 2满足要求 记录res+1,R往右走
                              R                100<200 * 2不满足要求。 停止内层循环 L+1
            L                 R                300<200*2 不满足要求。 停止内层循环 L+1
                L             R                500>200*2 满足要求 记录res+1,R往右走
                L                  R           500>220*2 满足要求 记录res+1,R往右走
                L                      R       500<260*2 不满足要求。 停止内层循环 L+1
                    L                          800>260*2 满足要求 记录res+1,R往右走 触发r <= right
                                                结束内层循环L+1后再触发外层循环,结束循环
     
     */

你可能感兴趣的:(swift 归并排序)