Java Coding Test Course, 022 Sort an Array 3

Problem Description

The problem of sorting a given sequence is a common type of algorithmic problem.
This problem, titled “Sorting III”, requires you to sort an array of integers in ascending order.
You are given the size N of the input array (1 ≤ N ≤ 1,000,000) and the elements A[i] of the array (0 ≤ A[i] ≤ 1,000,000).
If A[i] is already in sorted form, you can simply output the array without any additional operations.

Input Format

        The first line contains the integer N.
        The second line contains N integers A[i].
    

Output Format

Output the sorted integer array. Each integer should be separated by a space.

Example Problem

        Input:
        5
        5 4 3 2 1

        Output:
        1 2 3 4 5
    

Approach

To solve this problem, you first need to sort the received array.
In Java, you can easily sort it using the Arrays.sort() method, but understanding how sorting algorithms work is also important.
Some sorting algorithms you should learn include Selection Sort, Insertion Sort, Quick Sort, and Merge Sort.

Here, we will use Quick Sort to solve the problem.
Quick Sort has an average time complexity of O(N log N) and is generally known as one of the most efficient methods for sorting large datasets.

Algorithm Description

  1. Generally, Quick Sort works by selecting a pivot and positioning elements smaller than the pivot to the left and those larger to the right.
  2. It sorts by dividing the array and essentially operates through recursive calls.
  3. This process is repeated until all elements are sorted.

Code Implementation

        
            import java.util.Arrays;

            public class Main {
                public static void main(String[] args) {
                    Scanner scanner = new Scanner(System.in);
                    int n = scanner.nextInt();
                    int[] arr = new int[n];

                    for (int i = 0; i < n; i++) {
                        arr[i] = scanner.nextInt();
                    }

                    // Sort the array
                    Arrays.sort(arr);

                    // Output the sorted array
                    for (int num : arr) {
                        System.out.print(num + " ");
                    }
                }
            }
        
    

Optimization Methods

Depending on the case, if the range of array elements is small or there is a specific pattern,
counting sort may be considered.
Counting sort has a complexity of O(N + K), where K is the maximum value of the array elements.
If the distribution of elements is narrow, counting sort can be used to maximize performance.

For example, if the range of numbers is limited to 0 to 1000,
you can count how many times each value appears between 0 and 1000 and sort based on that.

Counting Sort Code Implementation

        
            import java.util.Scanner;

            public class CountingSort {
                public static void main(String[] args) {
                    Scanner scanner = new Scanner(System.in);
                    int n = scanner.nextInt();
                    int[] arr = new int[n];
                    int max = 1000000; // Maximum value as per problem conditions

                    int[] count = new int[max + 1]; // Count array with size max + 1

                    // Receiving input
                    for (int i = 0; i < n; i++) {
                        arr[i] = scanner.nextInt();
                        count[arr[i]]++;
                    }

                    // Sort and output
                    for (int i = 0; i <= max; i++) {
                        while (count[i] > 0) {
                            System.out.print(i + " ");
                            count[i]--;
                        }
                    }
                }
            }
        
    

Conclusion

This “Sorting III” problem provides a good opportunity to practice basic understanding of sorting algorithms and array manipulation.
It is possible to learn various approaches to sorting algorithms through Quick Sort and Counting Sort.
To solve more complex problems in the future, it’s important to well understand and utilize the basics learned from this problem.

Additional Problems

It would also be good practice to think of variations of this problem.
For example, creating problems like the following can be beneficial:

  • A problem to track how many times each number appears after randomly inputting numbers from 1 to 1000
  • A problem of sorting a sequence that can only be sorted at specific intervals
  • A problem to implement another sorting algorithm, Merge Sort