'How to perform sorting on an array without using .sort()

I am trying to find the easiest way to sort an array without using sort() function. I tried searching but i could not find any questions that were on SWIFT. I found several questions about php and javascript and so far nothing on swift.

var arr = [7,6456,2135,164,1345,4,8,5,87456,123,2,87,724,6523,1]
        var arrSorted = arr
        var index = arr.count
        repeat {
          var previousSwapIndex = 0
          
          for i in 1..<index {
            if (arrSorted[i - 1] as! Int) > (arrSorted[i] as! Int) {
               
                let prevVal = arrSorted[i - 1]
                let currentVal = arrSorted[i]
                arrSorted[i] = prevVal
                arrSorted[i - 1] = currentVal
              previousSwapIndex = i

            }
          }

          index = previousSwapIndex
          
        } while (index != 0)
        print(arrSorted as Array)

This method works but i am looking for something that is better than this and easier than this. (Edit[Clarification] :- better = faster / quicker ,as this iterates 120 times before the array is sorted) Could someone help me out?



Solution 1:[1]

Here's a generic implementation of insertion sort in Swift. It takes an inout array, but you should be able to modify it to return an array if that's what you want.

func sort<T: Comparable>(_ array: inout [T]) { 
    var i = 1 
    while i < array.count { 
        var x = array[i] 
        var j = i - 1 
        while j >= 0 && array[j] > x { 
            array[j+1] = array[j] 
            j -= 1 
        } 
        array[j+1] = x 
        i += 1 
    }
} 

To use it:

var intArr = [1, 7, 3, 6, 4]
sort(&intArr)
print(intArr) // [1, 3, 4, 6, 7]
var stringArr = ["hello", "goodbye", "a", "string", "z", "another string"]
sort(&stringArr)
print(stringArr) // ["a", "another string", "goodbye", "hello", "string", "z"]

It will work on any type that conforms to Comparable.

Solution 2:[2]

You can find about all the different methods of sorting from this git.

https://github.com/raywenderlich/swift-algorithm-club

I checked a few and none of them are using any .sort() functions. Pick whichever feels easier for you.

Solution 3:[3]

var unsortedStringArray = ["B", "C", "Z", "A", "H"]
var unsortedIntArray = [7,8,3,4,5,9,1,2,6]

func sortFunction<T:Comparable>(array: [T]) -> [T]{

var unsortedArray = array
for i in 0..<unsortedArray.count {
    for j in 0..<unsortedArray.count{
        var temp: T
        if unsortedArray[i] < unsortedArray[j] {
            temp = unsortedArray[i]
            unsortedArray[i] = unsortedArray[j]
            unsortedArray[j] = temp
        }
      }
    }
   return unsortedArray
}
let resultStringArray = sortFunction(array: unsortedStringArray)
let resultIntArray = sortFunction(array: unsortedIntArray)

print(resultStringArray) //["A", "B", "C", "H", "Z"]
print(resultIntArray) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam
Solution 2 Zyfe3r
Solution 3