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

    /*
        选择排序实例
        Wirtten by: nick
        Date: 2012-10-16 12:13
    */
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    
    void selectionSort(int list[], int last);
    void exchangeSmallest(int list[], int current, int last);
    
    int main()
    {
        int a[10] = {3,4,6,3,2,8,4,0,5,7};
    
        selectionSort(a, 9);
    
        for(int i=0; i<10; i++)
            cout<< setw(5) << a[i];
    
        return 0;
    }
    
    void selectionSort(int list[], int last)
    {
        for(int current=0; current<last; current++)
        {
            exchangeSmallest(list, current, last);
        }
    }
    
    void exchangeSmallest(int list[], int current, int last)
    {
        int smallest = current;
        for(int walker=current+1; walker<=last; walker++)
        {
            if(list[smallest] > list[walker])
            {
                smallest = walker;
            }
        }
        int tmp = list[current];
        list[current] = list[smallest];
        list[smallest] = tmp;
    }
  • 相关阅读:
    CentOS
    Docker
    Chart的简单使用
    DataGridView中间插入数据行
    获取每个月的固定的第n个星期几
    设置只能开启一个程序实例
    DataContext与实体类
    Attribute
    Delegate
    Event
  • 原文地址:https://www.cnblogs.com/wouldguan/p/2725749.html
Copyright © 2011-2022 走看看