Swift Program to sort an array in descending order using bubble sort


Bubble sort algorithm in swift, is the easiest search algorithm. This algorithm sorts the elements by repeatedly swapping adjacent elements if they are not present at the right place. So now we sort an array in descending order using bubble sort.

For example −

Array - [4, 7, 1, 8]
  • 1st iteration − compare two elements, if the first element is smaller than second element then swap their position. If not, then move to next pair.

[4, 7, 1, 8] 4<7, swap the position
[7, 4, 1, 8] 4>1, remain same
[7, 4, 1, 8] 1<8, swap the position
[7, 4, 8, 1]
  • 2nd iteration − Again compare two elements and swap their position if first element is smaller than second element.

[7, 4, 8, 1] 7>4, remain same
[7, 4, 8, 1] 4<8, swap position
[7, 8, 4, 1] 4>1, remain same
  • 3rd iteration − Again compare two elements and swap their position if first element is smaller than second element.

[7, 8, 4, 1] 7<8, swap position
[8, 7, 4, 1] 7>4, remain same
[8, 7, 4, 1] 4>1, remain same
So the sorted array in descending order is [8, 7, 4, 1]

Algorithm

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

  • Step 2 − Inside the function, we run nested for-in loop to traverse over each pair of adjacent element in the given.

  • Step 3 − Check if array[y]<array[y+1]. If yes, then swap the position of the elements without each other. If no, then move to next pair.

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

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

  • Step 6 − Print the sorted array.

Example

In the following example, we will create a function named as mBubbleSort(). This function takes an array as input and sort the given array into descending order with the help of bubble sort algorithm. This function uses nested for-in loop to iterate through each pair of adjacent element the given array and swap if the first element is less than the second element. This process continue till the last unsorted element. Here the function modifies the original array with the help of inout parameter. Finally display the sorted array.

import Foundation
import Glibc

// Function to sort array in descending order using bubble sort
func mBubbleSort(_ array: inout [Int]) {
   let size = array.count
    
   for x in 0..<size {
      for y in 0..<size-x-1 {
        
         // Compare two adjacent elements
         if array[y] < array[y+1] {
            // Swap the elements if they are 
            // not in the correct order 
            let temp = array[y]
            array[y] = array[y+1]
            array[y+1] = temp
         }
      }
   }
}

// Array of integer type
var arr = [67, 3, 22, 89, 12, 1, 55, 80, 4]
mBubbleSort(&arr)
print("Sorted array in descending order: \(arr)")

Output

Sorted array in descending order: [89, 80, 67, 55, 22, 12, 4, 3, 1]

Conclusion

So this is how we can sort an array in descending order using bubble sort.This algorithm works well only for small set of elements, it is not suitable for larger number of elements because its average and worst case time complexity is high.

Updated on: 24-Apr-2023

284 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements