Swift Coding Test Course, Bubble Sort Program 2

Hello! In this course, we will delve deeper into algorithms for preparing coding tests using Swift, specifically focusing on Bubble Sort. The problem we will solve is sorting an array, and we will implement this using the Bubble Sort algorithm. This course will include the following topics:

  • Explanation of the principles and workings of Bubble Sort
  • Implementation of the code in Swift
  • Examples of code execution
  • Suggestions for optimization and improvement
  • Analysis of the time complexity of Bubble Sort

1. What is Bubble Sort?

Bubble Sort is a very simple and intuitive sorting algorithm. It works by comparing two adjacent elements in an array and sorting them in order. This process can be visualized as the largest value “bubbling” up to the end of the array.

The Working Principle of Bubble Sort

  1. Start from the beginning of the array and compare two adjacent elements.
  2. If the first element is greater than the second, swap the two elements.
  3. Repeat this process until the end of the array.
  4. After completing one pass, the largest value moves to the end of the array.
  5. Repeat this process until the last element remains sorted.

2. Problem Definition

Given an integer array, sort this array in ascending order using Bubble Sort.

Input: [64, 34, 25, 12, 22, 11, 90]
Output: [11, 12, 22, 25, 34, 64, 90]

3. Implementation of Swift Code

Now, let’s write the code to solve this problem using Swift.

func bubbleSort(arr: [Int]) -> [Int] {
    var sortedArray = arr
    let n = sortedArray.count
    
    for i in 0.. sortedArray[j+1] {
                // Swap
                let temp = sortedArray[j]
                sortedArray[j] = sortedArray[j+1]
                sortedArray[j+1] = temp
            }
        }
    }
    return sortedArray
}

// Example execution
let array = [64, 34, 25, 12, 22, 11, 90]
let sortedArray = bubbleSort(arr: array)
print(sortedArray)  // Output: [11, 12, 22, 25, 34, 64, 90]

4. Code Execution Example

When we execute the above Swift code, the given array is successfully sorted. In this code, we use two nested for loops to compare all the elements of the array. This can be time-consuming, but the true advantage of Bubble Sort is its intuitive nature.

5. Optimization and Improvement Methods

Bubble Sort has a time complexity of O(n^2), which is inefficient. However, a few simple optimization techniques can be applied:

  • Early termination: If no swaps occur during a pass, the array is already sorted, so no further comparisons are necessary.
  • Reducing the range of comparisons: Elements at both ends that have already been passed don’t need to be compared in subsequent passes.

The code with these optimizations can be modified as follows:

func optimizedBubbleSort(arr: [Int]) -> [Int] {
    var sortedArray = arr
    let n = sortedArray.count
    var swapped: Bool
    
    for i in 0.. sortedArray[j+1] {
                // Swap
                let temp = sortedArray[j]
                sortedArray[j] = sortedArray[j+1]
                sortedArray[j+1] = temp
                swapped = true
            }
        }
        // If no swaps occurred, sorting is complete, so exit
        if !swapped {
            break
        }
    }
    return sortedArray
}

6. Time Complexity of Bubble Sort

The time complexity of Bubble Sort is O(n^2) in both average and worst-case scenarios. The best case (when the array is already sorted) can be O(n), making it more efficient. For this reason, Bubble Sort may be effective on small data sets, but in real applications, it is common to use more efficient sorting algorithms.

Conclusion

In this course, we had the opportunity to implement Bubble Sort using Swift and learn the basics of algorithm sorting. By solving basic problems like sorting an array, we were able to understand the workings and time complexity of algorithms. In the next course, we will cover a more efficient sorting algorithm called Quick Sort. Thank you for being with us!