#C7636. Find the Smallest n Numbers
Find the Smallest n Numbers
Find the Smallest n Numbers
You are given an array of integers and an integer n. Your task is to write a program that finds the smallest n numbers in the array and prints them in ascending order. You must implement your own sorting algorithm (e.g., bubble sort) and are not allowed to use built-in sorting functions.
Note: In case n is greater than or equal to the length of the array, simply sort the whole array and output all the elements.
Input/Output Method: Read input from stdin
and write output to stdout
.
The mathematical condition for the problem can be summarized as follows:
Given an array \(A = [a_1, a_2, \dots, a_m]\) and an integer \(n\), find a sorted array \(B = [b_1, b_2, \dots, b_k]\) such that \(k = \min(n, m)\) and \(b_1 \leq b_2 \leq \dots \leq b_k\), where \(\{b_1, b_2, \dots, b_k\} \subseteq A\).
inputFormat
The input consists of two lines:
- The first line contains a series of integers separated by spaces representing the array. This line may be empty, indicating an empty array.
- The second line contains a single integer n which indicates how many smallest numbers to extract.
outputFormat
Output the smallest n numbers in ascending order on a single line, separated by a single space. If no numbers are to be output (e.g., when the array is empty or n is 0), output an empty line.
## sample4 10 2 8 6 7 1 5
3
1 2 4