选择排序 Selection Sort

1)在数组中找最小的数与第一个位置上的数交换;

2)找第二小的数与第二个位置上的数交换;

3)以此类推

template<typename T>      //泛型
void selectionSort(T arr[], int n){
//数组arr 个数n
for(int i=;i<n;i++){
//寻找[i,n)区间里的最小值
int minIndex = i;
for(int j = i+;j<n;j++){
if(arr[j] < arr[minIndex])
//minIndex 中储存最小元素的下标
minIndex = j;
swap(arr[i], arr[minIndex]);
}
}
}

完整代码:

#include <iostream>

#include "Student.h"

using namespace std;

template<typename T>

void selectionSort(T arr[], int n){

    for(int i =  ; i < n ; i ++){

        int minIndex = i;

        for( int j = i +  ; j < n ; j ++ )

            if( arr[j] < arr[minIndex] )

                minIndex = j;

        swap( arr[i] , arr[minIndex] );

    }

}

int main() {

    // 测试模板函数,传入整型数组

    int a[] = {,,,,,,,,,};

    selectionSort( a ,  );

    for( int i =  ; i <  ; i ++ )

        cout<<a[i]<<" ";

    cout<<endl;

    // 测试模板函数,传入浮点数数组

    float b[] = {4.4,3.3,2.2,1.1};

    selectionSort(b,);

    for( int i =  ; i <  ; i ++ )

        cout<<b[i]<<" ";

    cout<<endl;

    // 测试模板函数,传入字符串数组

    string c[] = {"D","C","B","A"};

    selectionSort(c,);

    for( int i =  ; i <  ; i ++ )

        cout<<c[i]<<" ";

    cout<<endl;

    // 测试模板函数,传入自定义结构体Student数组

    Student d[] = { {"D",} , {"C",} , {"B",} , {"A",} };

    selectionSort(d,);

    for( int i =  ; i <  ; i ++ )

        cout<<d[i];

    cout<<endl;

    return ;

}

相应头文件:Student.h

#include <iostream>

#include <string>

using namespace std;

struct Student{

    string name;

    int score;

    // 重载小于运算法,定义Student之间的比较方式

    // 如果分数相等,则按照名字的字母序排序

    // 如果分数不等,则分数高的靠前

    bool operator<(const Student& otherStudent){

        return score != otherStudent.score ?

               score > otherStudent.score : name < otherStudent.name;

    }

    friend ostream& operator<<(ostream &os, const Student &student){

        os<<"Student: "<<student.name<<" "<<student.score<<endl;

        return os;

    }

};

选择排序 Selection Sort的更多相关文章

  1. 排序算法 - 选择排序(selection sort)

    选择排序(Selection sort)跟插入排序一样,也是O(n^2)的复杂度,这个排序方式也可以用我们的扑克牌来解释. 概念 桌面上有一堆牌,也是杂乱无章的,现在我们想将牌由小到大排序,如果使用选 ...

  2. 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort

    选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...

  3. 排序算法--选择排序(Selection Sort)_C#程序实现

    排序算法--选择排序(Selection Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来 ...

  4. 跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort)

    跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort) 选择排序(selection sort) 算法原理:有一筐苹果,先挑出最大的一个放在最后,然后 ...

  5. [算法] 选择排序 Selection sort

    选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然 ...

  6. 【排序算法】选择排序(Selection sort)

    0. 说明 选择排序(Selection sort)是一种简单直观的排序算法. 它的工作原理如下. 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最 ...

  7. 排序--选择排序Selection Sort Java实现

    基本原理 选择排序的简单原理:选择排序算法通过从未排序部分重复查找最小元素(考虑升序)并将其放在开头来对数组进行排序. 将数组两个子数组: 已排序子数组 未排序子数组 选择排序中每次循环都会从未排序子 ...

  8. 选择排序——Selection Sort

    基本思想: 在长度为N的无序数组中,第一次遍历n-1个数,找到最小的数值与第一个元素交换:第二次遍历n-2个数,找到最小的数值与第二个元素交换:...第n-1次遍历,找到最小的数值与第n-1个元素交换 ...

  9. 选择排序Selection sort

    顾名思意,就是直接从待排序数组里选择一个最小(或最大)的数字,每次都拿一个最小数字出来, 顺序放入新数组,直到全部拿完 再简单点,对着一群数组说,你们谁最小出列,站到最后边 然后继续对剩余的无序数组说 ...

随机推荐

  1. mybatis内部类映射写法

    直接上代码:   mybatis内部类映射使用$而不是.

  2. 360 安全卫士 for Linux 使用结果

    测试了一把,结果显示360基本对Linux社区规范和安全常识不give a fuck. 胡乱打包 首先,这个deb包就是胡乱打包,依赖关系就没弄好: $ dpkg-deb -I 360safeforl ...

  3. Spring Boot 启动失败,描述/Description: Cannot determine embedded database driver class for database type NONE

    异常截图====> 快速解决方式==> 在SpringBoot的启动类上面添加注解:@EnableAutoConfiguration(exclude={DataSourceAutoConf ...

  4. Selenium家谱

    自动化测试一词也算是整个测试行业里面比较热门的一个词儿,工资高,前景好,有实力,有态度等等,很多企业的管理者也在不断的扩大自己的队伍和职能,这也是导致自动化测试比较流行的原因之一.但是很多企业做自动化 ...

  5. 二度Xml<2>

    一下介绍xml的基本操作,添加xml新节点: 其他方法在前一篇日记中有详细讲解,请详见:http://www.cnblogs.com/fjsnail/archive/2012/10/20/273212 ...

  6. Instruments Tutorial for iOS: How To Debug Memory Leaks【转】

    If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter. Thanks for vis ...

  7. C# Path类常用方法

    Path 类 对包含文件或目录路径信息的 String 实例执行操作. 1.Path.GetExtension 方法 —— 返回指定的路径字符串的扩展名. public static string G ...

  8. vs2015编译ffmpeg 出现错误rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 ___iob_func

    vs2015编译ffmpeg(版本3.0.2)引用外部库文件librtmp出现以下错误: rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 __imp__st ...

  9. C#ADO.NET基础二

    DataAdapter的使用,批量增删改 1.使用DataAdapter查询 private void Select2() { try { using (SQLiteConnection conn = ...

  10. 【转载】C# DataGridView 通过代码设置样式

    // 表格上下左右自适应 dataGridView.Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | An ...