zoukankan      html  css  js  c++  java
  • 数据结构与算法(C++)之selectsort

    选择排序:

    #include <iostream>
    //从当前未排序的正数中找一个最小的整数,将他放在已排好的整数列表中的最后。
    //要点:选择排序选择最小的,往左边选。
    using namespace std;

    void selectsort(int *a,const int n);

    int main()
    {
    int x[]={1,3,5,9,7,2,4,6,8,0};
    selectsort(x,10);
    for(int k=0;k<10;k++)
    {
    cout<<x[k]<<endl;
    }
    return 0;
    }
    void selectsort(int *list,const int n)
    { //i<n也可以
    //第一个循环定义扫描的次数
    for(int i=0;i<n-1;i++)
    {
    int m=i;//m就是毛巾,毛巾是数组的下标。开始的时候假设第一个是最小的
    for(int j=i+1;j<n;j++)
    {

    if(list[j]<list[m])
    {//在扫描的过程中,如果发现更小的,就把它记录下来,把毛巾丢下。
    m=j;
    }
    //第一边扫描结束以后,来交换一次
    std::swap(list[i],list[m]);
    }
    }
    }

    no one and you
  • 相关阅读:
    spring源码怎么解决循环依赖?
    观察者模式
    单例模式
    Python 列表(List)
    python字符串(str)
    内置函数
    python运算符
    函数名的应用 闭包 迭代器
    生成器,推导式
    python的起源
  • 原文地址:https://www.cnblogs.com/wslQAQ/p/12325004.html
Copyright © 2011-2022 走看看