selectionSort
来源:互联网 发布:网络犯罪的客观原因有 编辑:程序博客网 时间:2024/06/04 19:15
推荐慕课网,刘宇波老师《算法与数据结构》链接:http://coding.imooc.com/class/71.htmlselectionSort 课堂笔记/* main.cpp */#include <iostream>using namespace std;#include<stdio.h>#include"Student.h"template<typename T>void SelectionSort(T arr[],int n){ for(int i=0;i<n;i++) { int index = i; for(int j = i+1;j<n;j++) { if(arr[j] < arr[index]) index = j; } if(index != i) swap(arr[i],arr[index]); }}int main(){ int arr[] = {10,9,8,7,6,5,4,3,2,1}; int n = sizeof(arr)/sizeof(arr[0]); SelectionSort(arr,n); for(int i=0;i<n;i++) printf("%d ",arr[i]); printf("\n"); double arr2[] = {10.1,9.5,3.3}; int n2 = sizeof(arr2)/sizeof(arr2[0]); SelectionSort(arr2,n2); for(int i=0;i<n2;i++) printf("%.2f ",arr2[i]); printf("\n"); string c[] = {"D","A","C","B"}; int n3 = sizeof(c)/sizeof(c[0]); SelectionSort(c,n3); for(int i=0;i<n3;i++) cout<<c[i]<<" "; printf("\n"); Student st[3] = {{"B",80},{"C",95},{"A",90}}; SelectionSort(st,3); for(int i=0;i<3;i++) cout<<st[i]; cout<<endl; return 0;}/* Student.h *///C头文件格式,参考:http://blog.sina.com.cn/s/blog_966f8e8501010wlv.html#ifndef SELECTIONSORT_STUDENT_H#define SELECTIONSORY_STUDENT_H#include<iostream>#include<string>using namespace std;struct Student{ string name; int score; bool operator<(const Student &otherStudent){ return score<otherStudent.score; } friend ostream& operator<<(ostream &os,const Student &student){ os<<"Student: "<<student.name<<" "<<student.score<<endl; return os; }};#endif
阅读全文
0 0
- SelectionSort
- selectionSort
- selectionSort
- selectionSort
- selectionSort
- SelectionSort
- selectionSort
- Golang - SelectionSort
- 选择排序SelectionSort
- 快速排序(SelectionSort)
- 选择排序-SelectionSort
- 选择排序 - SelectionSort
- 算法基础复习-SelectionSort
- 选择排序SelectionSort
- 【代码积累】SelectionSort
- bubblesort and selectionsort
- 选择排序(1)SelectionSort
- SelectionSort -- 选择排序(C++)
- Android studio微信支付官网demo
- 如何关闭电脑的自启软件?
- C#基础-040 设计User类,该类有用户名,密码,存款金额。该类用户有存款,取款的方法;设计 CreditUser,该类新增信用额度。用户每次提升信用额度后,每刷卡刷爆三次,信用额度提升1000
- 存储管理
- swift本地存储
- selectionSort
- 演讲、演讲人、听众
- ubuntu修改静态IP地址
- String.Format()
- LRU算法 C++实现
- DNS
- cocos2d中去掉微信呢称中的表情字符(ios与android)
- 颓废了?看看这些!
- 天天学Linux命令11--cat命令