Sorting Algorithm in Swift: Bubble Sort (Part — 1)

lalit kant
3 min readApr 23, 2022

--

Algorithms are very important in developer life. It help us to write powerful code. All Giant companies like Google, Microsoft, Apple, Facebook in start judge you based on your Logical and Algorithmic skills.

Algorithms are simple

Many developers face problem in writing Algorithms in Swift language. So, to help all iOS developers I am writing Sorting Algorithms in Swift.

I am covering mostly used sorting algorithms.

I will start this series with simple Algo first… then….. so on…..

  1. Bubble Sort(Least preferred sorting algorithm)
  2. Insertion Sort (Basic sorting algorithm)
  3. Merge Sort(Fast sorting algorithm)
  4. Quick Sort(Fast sorting algorithm) — Coming soon

Bubble Sort:

Bubble sort is sorting algorithm that compares two adjacent elements and swaps them until they are not in the intended order.

Just like the movement of air bubbles in the water that rise up to the surface, each element of the array move to the end in each iteration. Therefore, it is called a bubble sort. Sometime it is called as sinking sort.

Implementation(e.g. Descending Order):

Bubble Sort
  1. Start iterations for main array
  2. Starting from the first index, compare the first and the second elements by adding nested loop.
  3. Second nested loop will run from next index of sorted indexes of main array.
  4. Then If the first element is less than the second element, they are swapped.
  5. Now, compare the second and the third elements. Swap them if they are not in order.
  6. The above process goes on until the last element.

Here is Code to use it:

The Solution I have done is to sort all data type of array values e.g. string, Integers etc.

This implementation little more optimised as we are comparing inner loop index data with main array index .

// MARK: Bubble Sort/// I have done Merge sort in Decensding order/// /// If we want to change sorting order of string then change comparator operator > to <func bubbleSort<T: Comparable>(arrTobeSorted: inout [T]) {for i in 0..<arrTobeSorted.count{ for j in i..<arrTobeSorted.count  {   if arrTobeSorted[i] < arrTobeSorted[j]    {      let tempNum = arrTobeSorted[i]      arrTobeSorted[i] = arrTobeSorted[j]      arrTobeSorted[j] = tempNum    }  } }}

How to use


var intArrayToBeSorted = [1,1,45,23,11,76,25,98,34,56]
bubbleSort(arrTobeSorted: &intArrayToBeSorted)print(intArrayToBeSorted)var doubleArrayToBeSorted = [1,1.5,45.1,23.22,11,76,25,98,34,56]bubbleSort(arrTobeSorted: &doubleArrayToBeSorted)print(doubleArrayToBeSorted)var stringToBeSorted = "Sort Bubbles"var stringToArray = Array(stringToBeSorted)bubbleSort(arrTobeSorted: &stringToArray)// Empty space is present in the end of sorted stringprint(String(stringToArray))

Complexities:

Best Case: O(n) When arrays is in sorted order

Worst Case: O(n2) when we try to sort ascending order of array but actually array is in Descending order.

Average Case: O(n2) When try to sort jumbled order value array.

When to use

  • Complexity is not a concern
  • Simple code is preferred

Happy Coding !!!!

--

--

lalit kant

With around 11 years of experience I have worked in all Mobile platforms like iOS, Android, React Native and Flutter. Mainly into iOS.