1

I'm curious which sorting algorithm is used in case of native .sort Swift method?

e.g.:

here we have Bubble Sort algorithm example in Swift:

var uArray = [0, 6, 7, 3, 4, 2, 1]
var n = uArray.count

for j in 0...n-2 {
    for i in 0...n-2 {
        if uArray[i] > uArray[i + 1] {
            let valueHolder = uArray[i]
            uArray[i] = uArray[i + 1]
            uArray[i + 1] = valueHolder
        }
    }
}

now I'm curious which algorithm is used in this case:

var unsortedArr = [0, 6, 7, 3, 4, 2, 1]
let result = unsortedArr.sort { $0 < $1 }

... as we can see the result is the same in both cases but which is faster and what algorithm is hidden behind .sort method?

Robert
  • 3,790
  • 1
  • 27
  • 46

0 Answers0