美文网首页
Swift Merge Sort

Swift Merge Sort

作者: _清欢有味_ | 来源:发表于2017-03-02 19:32 被阅读0次
    func mergeSort<T: Comparable>(_ array: [T]) -> [T] {
      guard array.count > 1 else { return array }
     
      let middleIndex = array.count / 2
     
      let leftArray = mergeSort(Array(array[0..<middleIndex]))
      let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
     
      return merge(leftArray, rightArray)
    }
     
    func merge<T: Comparable>(_ left: [T], _ right: [T]) -> [T] {
      var leftIndex = 0
      var rightIndex = 0
     
      var orderedArray: [T] = []
     
      while leftIndex < left.count && rightIndex < right.count {
        let leftElement = left[leftIndex]
        let rightElement = right[rightIndex]
     
        if leftElement < rightElement {
          orderedArray.append(leftElement)
          leftIndex += 1
        } else if leftElement > rightElement {
          orderedArray.append(rightElement)
          rightIndex += 1
        } else { 
          orderedArray.append(leftElement)
          leftIndex += 1
          orderedArray.append(rightElement)
          rightIndex += 1
        }
      }
     
      while leftIndex < left.count {
        orderedArray.append(left[leftIndex])
        leftIndex += 1
      }
     
      while rightIndex < right.count {
        orderedArray.append(right[rightIndex])
        rightIndex += 1
      }
     
      return orderedArray
    }
    

    相关文章

      网友评论

          本文标题:Swift Merge Sort

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