Swift Coding Test Course, 022 Sorting Numbers 3

Hello! This time, let’s solve a coding test problem based on Swift called “Sorting Numbers 3”. This problem may seem simple as it involves sorting numbers, but it has specific conditions and constraints, making it a good practice for coding tests. In this article, we will explore the problem definition, input and output formats, algorithmic approaches, and optimization methods in detail.

Problem Description

The requirement of the problem is to sort the given numbers, but the range of numbers to be sorted is limited. Specifically, the range of numbers is integers between 1 and 100,000, and the objective is to sort these integers in ascending order and output them.

For example, let’s assume the following numbers are given:

  • 5
  • 3
  • 8
  • 1
  • 2

In this case, the output should be displayed as follows:

  • 1
  • 2
  • 3
  • 5
  • 8

Input Format

The input is provided in the following format:

  1. The first line will contain the number of integers N. (1 ≤ N ≤ 100,000)
  2. The second line contains N integers separated by spaces.

Output Format

The output should print each sorted number on a new line.

Solution Approach

This problem involves sorting numbers, so we can use the most basic sorting algorithms. However, since the maximum value of N is 100,000, we cannot use inefficient algorithms like Bubble Sort or Selection Sort that have a time complexity of O(N^2).

We will use the Counting Sort algorithm to solve this problem. Counting Sort is a method that sorts efficiently when the range of given numbers is limited. This algorithm involves the following steps:

  1. Create an array corresponding to the range of input numbers.
  2. Record the count of each input number at the respective index.
  3. Finally, output the sorted numbers based on their counts.

Code Implementation

Now let’s write code to solve the problem in Swift. Since Swift allows the use of arrays as fields, implementing Counting Sort is very straightforward. Below is an example of the implementation:


    import Foundation

    func countingSort(numbers: [Int]) -> [Int] {
        // Since the range of numbers is 1 to 100,000, initialize an array of size 100,001
        var counts = Array(repeating: 0, count: 100001)

        // Count the occurrences of each number
        for number in numbers {
            counts[number] += 1
        }

        var sortedNumbers: [Int] = []
        
        // Generate sorted numbers based on the counts
        for (number, count) in counts.enumerated() {
            for _ in 0..

Example Execution

Let's use the code above to provide input. For instance, suppose we provide the following input:


    5
    5 3 8 1 2
    

The output result should be as follows:


    1
    2
    3
    5
    8
    

Complexity Analysis

The time complexity of this algorithm is O(N + K). Here, N is the number of input integers, and K is the range of numbers. In this case, K is fixed at 100,000, making the algorithm highly efficient. The space complexity also requires O(K), taking up O(100,000) space.

Conclusion

In this article, we examined the problem "Sorting Numbers 3" and implemented a solution using Counting Sort. Problems like these enhance understanding of basic algorithms and are frequently encountered in actual coding tests, so be sure to practice them. In the next tutorial, we will tackle more challenging problems. Thank you!