zoukankan      html  css  js  c++  java
  • 选择排序

    #include<iostream>
    using namespace std;
    const int maxn = 5000;
    
    int main()
    {
        void selection_sort(int A[], int n);
        void put(int A[], int n);
        int n, A[maxn];
        cin >> n;
        for(int i = 0; i < n; ++i)
        cin >> A[i];
        selection_sort(A, n);
        put(A, n);
        return 0;
    }
    
    int min(int A[], int beg, int n)
    {//查找最小数并返回
        int min = A[beg], index = beg;
        for(int i = beg+1; i < n; ++i)
            if(A[i] < min)
            {
                min = A[i];
                index = i;
            }
        return index;
    }
    void exchange(int A[], int i, int j)
    {//交换两数
        int t;
        t = A[i];
        A[i] = A[j];
        A[j] = t;
    }
    void selection_sort(int A[], int n)
    {//选择排序
        int index = 0;
        for(int i = 0; i < n; ++i)
        {
            index = min(A, i, n-1);
            exchange(A, i, index);
        }
    }
    
    void put(int A[], int n)
    {//输出
        for(int i = 0; i < n; ++i)
            cout << A[i] << "  ";
        cout << endl;
    }
    
    //时间复杂度:T(n) = Θ(n^2)(最好与最坏情况下都是一样的);
    /*
    8
    3 6 5 7 2 0 1 9
    */
    

      

  • 相关阅读:
    对于 redux 的一些理解-1.基础
    css 优化
    HTML 理解标签
    css 理解盒模型
    json2.js JSON解析程序
    DOM 核心
    居中
    Director.js
    jquery-2.0.3 源码分析 整体架构
    Zookeeper安装
  • 原文地址:https://www.cnblogs.com/sanghai/p/2809329.html
Copyright © 2011-2022 走看看