#C4349. Stable Integer Sort
Stable Integer Sort
Stable Integer Sort
You are given a sequence of integers separated by spaces. Your task is to sort these integers in non-decreasing order, while preserving the relative order of elements that have equal values (i.e. using a stable sort).
Note: A sorting algorithm is said to be stable if it maintains the relative order of elements that compare equal.
The input is provided as a single line of space‐separated integers, and the output should also be a single line of space‐separated integers in sorted order.
Mathematical Explanation: Given an array \(a = [a_1, a_2, \dots, a_n]\), produce an output array \(b = [b_1, b_2, \dots, b_n]\) such that
[ b_1 \le b_2 \le \dots \le b_n, ]
and if \(a_i = a_j\) with \(i < j\), then in \(b\) the element corresponding to \(a_i\) appears before the element corresponding to \(a_j\).
inputFormat
The input consists of a single line containing space-separated integers. There may be zero or more integers in the input.
Example: 4 5 2 3 3 1 4 5
outputFormat
Output a single line with the space-separated sorted integers in non-decreasing order, preserving the relative order of duplicate elements.
Example: If the input is 4 5 2 3 3 1 4 5
, the output should be 1 2 3 3 4 4 5 5
.
4 5 2 3 3 1 4 5
1 2 3 3 4 4 5 5