Swift Program to sort an array in descending 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 descending 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 10 is the greatest number so swap 6 with 10.

Resultant Array: [10, 6, 3, 7]

• 2nd iteration − Again traverse through whole array and find element for 2nd position.

Resultant Array: [10, 7, 3, 6]

• 3rd iteration − Again traverse through whole array and find element for 3rd position.

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


Algorithm

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

• Step 2 − Inside the function, initialise maximum value to maxIndex variable.

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

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

• Step 5 − Then increment the value of maxIndex 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 swift example, we will create a function named as selectionSortAlgo(). This function takes an array as input and sort the given array into descending order with the help of selection sort algorithm. This function takes first element of the array and assume that array is the maximum, then compare that element with the remaining array elements and check for the largest element. If the largest element found, then update the index to that element. After that we iterate through the array and swap the first element with the maximum element. This process continue till the last unsorted element and finally display the sorted array in descending order.

import Foundation
import Glibc

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

// Creating a copy of original array
var nArr = arr

// Selecting maximum element and
// then place it at the beginning of the array
for x in 0..<nArr.count {
var maxIndex = x
for y in x+1..<nArr.count {
if nArr[y] > nArr[maxIndex] {
maxIndex = y
}
}
if maxIndex != x {
nArr.swapAt(x, maxIndex)
}
}
return nArr
}

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


Output

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


Conclusion

So this is how we can sort an array in descending order using selection sort. This method can also work for float and double data type with some minor changes in the syntaxes. Here we use iterative method to implement selection sort. So the time complexity of the selection sort is O(N2). Although selection sort is simple and easy but the implementation is not stable and it does not work well with large datasets.

Updated on: 24-Apr-2023

271 Views