选择排序
来源:互联网 发布:aspera上传数据至sra 编辑:程序博客网 时间:2024/06/16 06:22
选择排序的基本思想:第一趟从第1个记录arr[0]开始,依次向后查找关键字最小的记录(记为arrr[k]),交换arr[0]和arr[k],第二趟从第2个记录arr[1]开始,依次向后查找关键字最小的记录(记为arrr[k]),交换arr[1]和arr[k],依次类推...
选择排序的C++代码实现如下:
SortTestHelper.h头文件(其中包含一些常用于排序算法的辅助函数)
#include <iostream>#include <cstdlib>#include <ctime> //clock()、CLOCKS_PER_SEC#include <cassert> //包含函数assert()using namespace std;namespace SortTestHelper{ //辅助函数 - 随机产生一个数组 int* generateRandomArray(int n, int RangeL, int RangeR) //返回数组首地址 { //判断RangeL是否<=RangeR assert(RangeL <= RangeR); //参数为表达式,表达式为真时返回true,否则打印错误信息 int *arr = new int[n]; srand(time(0)); for(int i = 0; i < n ; i++) { arr[i] = rand() % (RangeR - RangeL + 1) + RangeL; //使得产生的随机数在RangeL和RangeR之间 } return arr; } //辅助函数 - 打印数组 template<typename T> void printArray(T arr[], int n) { for(int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } //辅助函数 - 判断数组是否有序(升序) template<typename T> bool isSorted(T arr[], int n) { for(int i = 0; i < n; i++) { if(arr[i] > arr[i + 1]) { return false; } } return true; } //辅助函数 - 测试算法的时间 template<typename T> void testSort(string sortname, void(*sort)(T[], int), T arr[], int n) //arr[]和n是函数指针需要的参数 { clock_t starttime = clock(); sort(arr, n); //调用函数sort() clock_t endtime = clock(); //判断排序是否成功 assert(isSorted(arr, n)); //若是数组无序,则assert会自动调用abort()退出程序,不会执行下面的语句 cout << sortname << " needs " << double(endtime - starttime) / CLOCKS_PER_SEC << "s." << endl; }}
Student.h头文件(其中包含一个结构体,用于测试将结构数组作为算法的参数)
#include <iostream>#include <string>//再定义了结构体或者类之后一定要考虑是否需要重载运算符(特别是<<)struct Student{ std::string name; double score; //为了将Student能够进行比较,需要对运算符重载 bool operator<(const Student & otherStudent) const //和类的成员函数类似 { //若将学生的成绩从大到小排列,只需将这里改为>(即将当前学生的成绩大于其他学生成绩定义为当前学生小于其他学生) return score < otherStudent.score; } //重载输出运算符 friend std::ostream & operator<<(std::ostream & os, const Student & student) { os << "Name: " << student.name << std::endl; os << "Score: " << student.score << std::endl; return os; }};
main.cpp文件(其中将算法写作模板函数,即可对任意类型T的数组进行测试,这里的类型T可以是系统内置类型,也可以是用户自定义的类型)
#include <iostream>#include <algorithm> //包含内置函数swap()#include <string>#include "Student.h"#include "SortTestHelper.h"/** * 简单选择排序 - 在每一趟选出最小关键字的值,放在已排序列的最后 */using namespace std;//模板函数template<typename T>void selectionSort(T arr[], int n) //这里的T可以是内置类型(如int、double)或者用户自定义类型(如结构体和类){ int i; for(i = 0; i < n; i++) //共执行n-1趟排序完毕 { //寻找[i, n)区间中关键字最小的数据 int mindex = i; //最小值在数组中的索引,初始化为i的位置 for(int j = i + 1; j < n; j++) { if(arr[j] < arr[mindex]) { mindex = j; } } //交换arr[i]和arr[mindex] swap(arr[i], arr[mindex]); }}int main(){ //编写测试用例 //T - 整型 /*int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; selectionSort(arr, 10); for(int i = 0; i < 10; i++) { cout << arr[i] << " "; } cout << endl; */ int n = 10000; int *arr = SortTestHelper::generateRandomArray(n, 0, n); selectionSort(arr, n); /*for(int i = 0; i < n; i++) { cout << arr[i] << " "; } */ SortTestHelper::printArray(arr, n); //测试算法的时间性能 SortTestHelper::testSort("selectionSort", selectionSort, arr, n); //千万不要忘记delete delete[] arr; cout << endl; //T - 浮点型 float b[4] = {4.4, 3.3, 2.2, 1.1}; selectionSort(b, 4); for(int i = 0; i < 4; i++) { cout << b[i] << " "; } cout << endl; //T - 字符串型 string s[4] = {"D", "C", "B", "A"}; selectionSort(s, 4); for(int i = 0; i < 4; i++) { cout << s[i] << " "; } cout << endl; //T - 结构体 Student stu[4] = { {"liqian", 76}, {"tianxinyu", 89}, {"wumin", 90}, {"angle", 78} }; selectionSort(stu, 4); for(int i = 0; i < 4; i++) { cout << stu[i] << " "; } cout << endl; return 0;}
阅读全文
1 0
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序:
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 选择排序
- 输入流转换字符串(解析)
- Java异常处理
- Spring学习笔记
- Ubuntu14.04上配置Swift3.0环境
- Spark统计词汇量/排行榜/中位数
- 选择排序
- Fast Compressive Tracking (高速压缩跟踪)
- SAS统计分析学习笔记(七)——相关和简单回归分析
- Machine Learning---8--模型评估与分类性能度量
- Eigen: 二维Vector怎么转为矩阵
- machine learning学习笔记<二>
- 二项分布,binomial(n,k,p)
- HDOJ HDU 1026 Ignatius and the Princess I
- 丢失的相机照片怎么找回来的使用方法分享