美文网首页
swift排序代码

swift排序代码

作者: 纱云 | 来源:发表于2021-05-19 14:50 被阅读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.选择排序

    算法描述

    • n个记录的直接选择排序可经过n-1趟直接选择排序得到有序结果。具体算法描述如下:
      • 初始状态:无序区为R[1..n],有序区为空;
      • 第i趟排序(i=1,2,3…n-1)开始时,当前有序区和无序区分别为R[1..i-1]和R(i..n)。该趟排序从当前无序区中-选出关键字最小的记录 R[k],将它与无序区的第1个记录R交换,使R[1..i]和R[i+1..n)分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区;
        n-1趟结束,数组有序化了。
     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;
        }
    

    相关文章

      网友评论

          本文标题:swift排序代码

          本文链接:https://www.haomeiwen.com/subject/tkepjltx.html