Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 110;
- int n, a[N];
- int main(){
- cin >> n;
- for (int i = 1; i <= n; i ++) {
- cin >> a[i];
- }
- for (int i = 1, j = n; i < j; i ++, j --) {
- // cout << i << ' ' << j << endl;
- int minIndex = i;
- for (int k = i; k <= j; k ++) {
- if (a[minIndex] > a[k]) {
- minIndex = k;
- }
- }
- swap(a[minIndex], a[i]);
- for (int k = 1; k <= n; k ++) {
- cout << a[k] << ' ';
- }
- cout << endl;
- if (i + 1 == j) break; // 如果 i,j已经相邻
- int maxIndex = j;
- for (int k = j; k > i; k --) {
- if (a[maxIndex] < a[k]) {
- maxIndex = k;
- }
- }
- swap(a[maxIndex], a[j]);
- for (int k = 1; k <= n; k ++) {
- cout << a[k] << ' ';
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement