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


Quick sort algorithm in swift is a sorting algorithm which works on divide and conquer approach. In this sort, we first divide the array into subarrays by selecting a pivot element. Here the division takes place in such a way, in which the smallest elements placed at the left side of the pivot element and the largest element placed at the right side of the pivot element. Now the right and left subarrays are also divide using the same approach. This process continue till all the subarrays contain one element. At this point the array element are sorted now we combine them into one array to display them on the output. So now we sort an array in ascending order using quick sort.

Algorithm

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

  • Step 2 − Inside the function, first we check the length of the array. If it has 0 or 1 element, then return the array is sorted.

  • Step 3 − Select the pivot element from the middle of the given array.

  • Step 4 − Filter the elements that are less than the pivot

  • Step 5 − Filter the elements that are equal to the pivot element

  • Step 6 − Filter the elements that are greater than the pivot element

  • Step 7 − Now recursively sort the sub-arrays and concatenate them into the single array using + operator.

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

  • Step 9 − Print the sorted array.

Example

In the following example, we will create a function named as quickSortAlgo(). This function takes an array as input and sort the given array into ascending order with the help of quick sort. Then we first select the middle element as a pivot element from the array. Then divide the array into three sub arrays containing elements: less than the pivot element, greater than the pivot element, and equal to pivot element. Now we recursively apply quick sort to lessEle and greaterEle and then concatenate all the sorted subarrays using + operator and finally display the sorted array in descending order.

import Foundation
import Glibc

// Function to sort the array in ascending order using quick sort 
func quickSortAlgo(arr: [Int]) -> [Int] {

   // Array is already sorted if it has 0 or 1 elements
   guard arr.count > 1 else { return arr } 
    
   // Select a pivot element in the middle
   let pivotEle = arr[arr.count/2] 
    
   // Filter the elements that are less than the pivot
   let lessEle = arr.filter { $0 < pivotEle } 
    
   // Filter the elements that are equal to the pivot element
   let equalEle = arr.filter { $0 == pivotEle } 
    
   // Filter the elements that are greater than the pivot element
   let greaterEle = arr.filter { $0 > pivotEle } 
    
   // Now recursively sort the sub-arrays and concatenate them into the single array
   return quickSortAlgo(arr: lessEle) + equalEle + quickSortAlgo(arr: greaterEle) 
}

let arr = [3, 6, 1, 8, 1, 56, 23, 12, 5]
let resultantArr = quickSortAlgo(arr: arr)
print("Sorted Array:", resultantArr)

Output

Sorted Array: [1, 1, 3, 5, 6, 8, 12, 23, 56]

Conclusion

So this is how we can sort an array in ascending order using quick sort. Here we use filter(_:) method to implement quick sort but you can also use other methods. The quick sort solves the problem easily due to its divide and conquer approach but it has the worst case time complexity that is O(n2). It works well with large dataset but does not good choice for small dataset.

Updated on: 24-Apr-2023

328 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements