1 solutions

  • 0
    @ 2026-11-16 10:45:26

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int a[1005];
    void selectionSort(int a[], int n) {
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (a[j] < a[minIndex]) {
                    minIndex = j;
                }
            }
            // 交换当前元素和最小值
            int temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
    }
    
    int main(){
        int n;
        cin >> n;
        for (int i = 0;i<n;i++){
            cin >> a[i];
        }
        
        selectionSort(a,n);
        for (int i = 0;i<n;i++){
            cout << a[i] << " ";
        }
        
        return 0;
    }
    
    
    • 1

    Information

    ID
    224
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    8
    Tags
    # Submissions
    15
    Accepted
    6
    Uploaded By