Reverse
Luogu P1908 reverses a positive integer sequence given...
Luogu. P1177
The N number that will be read will be followed by the output from small to large.
The first act is a positive integer N.
The second line contains a positive integer of N-spaces separated a~i~.
Splits the given number of N from small to large output, with spaces separated between numbers, lines unsigned and spaceless.
For 100% data, there is 1 ≤N ≤105 One.I ≤ 109。
Example:
输入
8
9 1 7 6 6 3 2 8
输出
1 2 3 6 6 7 8 9
Quick Sort Main UseThe idea of partition.Time complexityO(nlogn)。
int n, a[100005];
void quicksort(int l, int r){
if(l == r) return;
int i = l - 1, j = r + 1, x = q[(l + r) >> 1];
while(i < j){
do i++; while(q[i] < x); // 向右查找 ≥x 的数
do j--; while(q[j] > x); // 向左查找 ≤x 的数
if(i < j) swap(q[i], q[j]);
}
quicksort(l, j), quicksort(j + 1, r);
}
That is, to the left of the median, to find a number greater than the median m1, find a number smaller than the middle value on the right of the middle valuem2, and when1less than m2It's the time.1and m2, after the exchange has been completed, divide and repeat the above-mentioned operation in the divided area.
STL Achieved
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[100005];
int main(){
cin >> n;
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
for(int i = 0; i < n; i++) printf("%d", &a[i]);
return 0;
}
General
#include <iostream>
using namespace std;
int n, a[100005];
void quicksort(int l, int r){
if(l == r) return;
int i = l - 1, j = r + 1, x = q[(l + r) >> 1];
while(i < j){
do i++; while(q[i] < x); // 向右查找 ≥x 的数
do j--; while(q[j] > x); // 向左查找 ≤x 的数
if(i < j) swap(q[i], q[j]);
}
quicksort(l, j), quicksort(j + 1, r);
}
int main(){
cin >> n;
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
quicksort(0, n - 1);
for(int i = 0; i < n; i++) printf("%d", &a[i]);
return 0;
}
# 231
## 123