#include <iostream>
using namespace std;

void selectionSort(int arr[], int n) {
    int count = 0;
    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] > arr[minIndex]) {
                minIndex = j;
            }
        }
        if (minIndex != i) {
            swap(arr[minIndex], arr[i]);
            count++;
            for (int k = 0; k < n; k++) {
                cout << arr[k] << " ";
            }
            cout << endl;
        }
    }
    cout << count << endl;
}

int main() {
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    selectionSort(arr, n);
    return 0;
}

样例输入 5 5 3 8 2 9

样例输出 9 3 8 2 5 9 8 3 2 5 9 8 5 2 3 9 8 5 3 2

选择排序过程输出Description【试题描述】给出n个数输出每次交换后数组的状态并记录共交换了多少次。从大到小排序。【输入要求】两行第一行为一个正整数n第二行为n个数【输出要求】若干行每行为一次交换后数组的状态每两个元素间用空格隔开。最后一行为交换的次数。c++

原文地址: https://www.cveoy.top/t/topic/h20k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录