Swift Program to sort an array in ascending order using selection sort


Selection sort algorithm in swift is a sorting algorithm, in which it select smallest or largest element from the given unsorted array and place that element at the beginning of the unsorted array. This process continue till the last unsorted element. So now we sort an array in ascending order using selection sort.

For example −

Array: [6, 10, 3, 7]
  • 1st iteration − For the first position iterate through whole array starting from in 0 to 3. After traversing through whole array we find 3 is the smallest number so for the 1st position swap 6 with 3.

Resultant Array: [3, 10, 6, 7]
  • 2nd iteration − Again traverse through whole array and find element for 2nd position.

Resultant Array: [3, 6, 10, 7]
  • 3rd iteration − Again traverse through whole array and find element for 3rd position.

Resultant Array: [3, 6, 7, 10]
So the sorted array in ascending order is [3, 6, 7, 10]

Algorithm

  • Step 1 − Create a function to sort the array in ascending order using selection sort algorithm.

  • Step 2 − Inside the function, initialise minimum value to mIndex variable.

  • Step 3 − Use nested for-in loop iterate through each element of the array to find the minimum element.

  • Step 4 − While traversing if we find element that is smaller than mIndex, then swap both the values.

  • Step 5 − Then increment the value of mIndex to the next element.

  • Step 6 − Now outside the function create an array of integer type.

  • Step 7 − Call the function and pass the array into it.

  • Step 8 − Print the sorted array.

Example

In the following example, we will create a function named as selectionSortAlgo(). This function takes an array as input and sorts the given array into ascending order with the help of the selection sort algorithm. This function takes the first element of the array and assumes that the array is the minimum, then compare that element with the remaining array elements and checks for the smallest element. If the smallest element is found, then update the index to that element. After that, we iterate through the array and swap the first element with the minimum element. This process continues till the last unsorted element and finally displays the sorted array in ascending order

import Foundation
import Glibc
 
func selectionSortAlgo(arr: [Int]) -> [Int] {

   // Creating a copy of original array
   var nArr = arr 
    
   // Selecting minimum element and 
   // then place it at the beginning of the array
   for x in 0..<nArr.count {
      var mIndex = x
      for y in x+1..<nArr.count {
         if nArr[y] < nArr[mIndex] {
            mIndex = y
         }
      }
      if mIndex != x {
         nArr.swapAt(x, mIndex)
      }
   }
   return nArr
}

let array = [9, 1, 8, 1, 3, 5, 10, 2, 8, 23, 6]
let resultantArr = selectionSortAlgo(arr: array)
print("Sorted array in ascending order:", resultantArr)

Output

Sorted array in ascending order: [1, 1, 2, 3, 5, 6, 8, 8, 9, 10, 23]

Conclusion

So this is how we can sort an array in ascending order using selection sort. Here we implement selection sort using iterative methods. So the time complexity of the selection sort is O(N2). Although selection sort is simple and easy but the implementation is not stable. Also it does not work well with large datasets.

Updated on: 24-Apr-2023

690 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements