Title Source: "Information on an NOI" Example 2.1
Title Description
Input \ (n-\) number, the \ (n-\) number in ascending order of the output ( \ (n-\ Le 1000 \) ).
Input Format
Comprising a first line of input integer \ (n-\ Le 1000 \) , representing a number of elements.
The next line contains the \ (n-\) an integer number within the range of int.
Output Format
Output per line, for indicating \ (n-\) integer result from small to large, with a space between any two.
Sample input
8
3 2 4 5 1 8 6 7
Sample Output
1 2 3 4 5 6 7 8
Topic analysis
This question can we have so far learned of any sort algorithms to solve.
Select Sort Code Example:
#include <bits/stdc++.h>
using namespace std;
int n, a[1001];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i < n; i ++)
for (int j = i+1; j <= n; j ++)
if (a[i] > a[j])
swap(a[i], a[j]);
for (int i = 1; i <= n; i ++) cout << a[i] << " ";
return 0;
}
Bubble Sort Code Example:
#include <bits/stdc++.h>
using namespace std;
int n, a[1001];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i < n; i ++)
for (int j = 1; j <= n-i; j ++)
if (a[j] > a[j+1])
swap(a[j], a[j+1]);
for (int i = 1; i <= n; i ++) cout << a[i] << " ";
return 0;
}
Quicksort (sort function) Code Example:
#include <bits/stdc++.h>
using namespace std;
int n, a[1001];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
sort(a+1, a+1+n);
for (int i = 1; i <= n; i ++) cout << a[i] << " ";
return 0;
}
Note that for arrays, we know a+i
that a[i]
address, and we use sort
function sort of the first two variables are the first address of the array (included) and bit address (not included), that is to say from a[1]
to a[n+1]
a left closed right-open interval, that is a[1]
to a[n]
.