Sorting numbers in coding tests is a very basic yet important problem. In this article, we will provide an in-depth explanation of how to sort numbers using C++, guiding you through the process of solving the algorithm problem.
Problem Description
The task is to sort the given numbers and output them. The specific input and output conditions are as follows.
- The first line contains the number of elements to sort, N. (1 ≤ N ≤ 1,000,000)
- The second line contains N integers A[1], A[2], …, A[N]. (−1,000,000,000 ≤ A[i] ≤ 1,000,000,000)
The output must print N integers sorted in ascending order, each on a new line.
Example Input
5 5 4 3 2 1
Example Output
1 2 3 4 5
Solution Process
There are various methods to solve the problem, but here we will use the sorting algorithm utilizing C++’s STL (Standard Template Library). The sort()
function of STL has a fast and efficient sorting algorithm, typically with a time complexity of O(N log N).
1. Including Necessary Header Files
We include the necessary header files to use the STL sort function and vector.
#include <iostream> #include <vector> #include <algorithm> using namespace std;
2. Writing the Main Function
We will write the main function that processes the input and outputs the results after sorting.
int main() { int N; cin >> N; // Input the number of elements vector<int> A(N); // Declare a vector A of size N // Input N integers for(int i = 0; i < N; i++) { cin >> A[i]; } // Sort vector A sort(A.begin(), A.end()); // Output the sorted result for(const int &num : A) { cout << num << endl; } return 0; }
3. Explanation of the Code
The above code operates in the following steps:
- It takes an integer N as input to determine the number of numbers to sort.
- It creates a vector A of size N and inputs N integers.
- It uses the STL’s
sort()
function to sort vector A. - It outputs the contents of the sorted vector.
Time Complexity Analysis
The time complexity of the algorithm to solve this problem is O(N log N). This increases in proportion to the number of elements N being input, but since it is based on the most commonly used sorting algorithm, quicksort, it achieves very fast performance in practice.
Space Complexity Analysis
The space complexity is O(N). The vector A allocates space for N numbers to store all inputted values. Using STL allows efficient space management, and by using a vector instead of an array, memory management becomes easier.
Conclusion
In this post, we experienced a basic number sorting problem using C++. Through the number sorting problem, which is frequently asked in coding tests, we learned how to use sorting algorithms with STL and analyzed time and space complexity. Understanding such basic problems will greatly help in solving more complex problems in the future.
Additional Practice Problems
For more practice, try the following variations:
- Write a program that removes duplicate numbers from the input and outputs the sorted result.
- Output the sorted numbers in descending order.
- Write a program that outputs the frequency of each number. (Count how many times each number appears)
Through such additional practice, you can gain a deeper experience.