《图解算法》学习笔记之选择排序
选择排序 Selection Sort
遍历从前到后先找到最小的元素,并记录其索引,然后与下一个元素对比,如果大或小则记录其索引,并对应的交换索引对应的两个元素值。
------
Student.h
//
// Created by 天人合一 on 2020/6/15.
//
#ifndef CLIONBUFFER_STUDENT_H
#define CLIONBUFFER_STUDENT_H
#include <iostream>
#include <string>
using namespace std;
//少用,软件工程中认为容易产生namespace污染
struct Student{
string name;
int score;
// <运算符重载
//这种情况A,B的分数相同,按之前给定的顺序
// bool operator < (const Student& otherStudent){
// return score > otherStudent.score;//从小到大
// }
//如果分类相同,则按name的比较排序 ***?***:三目运算符
bool operator<(const Student& otherStudent){
return score != otherStudent.score ?
score < otherStudent.score : name < otherStudent.name; //从大到小,只比较name ----> name < otherStudent.name
// score > otherStudent.score : name < otherStudent.name;//从小到大
}
friend ostream& operator<<(ostream &os, const Student &student){
os<<"Student: "<<student.name<<" "<<student.score<<endl;
return os;
}
};
#endif //CLIONBUFFER_STUDENT_H
操作符《》重载必须是友元函数
main.cpp
#include <iostream>
#include <algorithm>
#include "Student.h"
using namespace std;
void selectionSort1(int array[], int n)
{
int arrtemp = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if(array[i] < array[j])
{
arrtemp = array[i];
array[i] = array[j];
array[j] = arrtemp;
}
}
}
}
//只能接受整数类型的数据,可以通过模板函数改进
void selectionSort2(int array[], int n)
{
for (int i = 0; i < n; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if(array[j] < array[minIndex])
{
minIndex = j;
}
swap( array[i], array[minIndex]);
}
}
}
void selectionSort3(int array[], int n)
{
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if(array[i] < array[j])
{
swap( array[i], array[j]);
}
}
}
}
//修改为模板函数
template<typename T>
void selectionSort4(T array[], int n)
{
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if(array[i] < array[j])
{
swap( array[i], array[j]);
}
}
}
}
template <typename T>
void print_array(T array[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << array[i] << " ";
cout << endl;
}
}
int main() {
// 1 整形数组测试
int intarr[10] = {7, 10, 9, 8, 1, 2, 3, 4, 5, 6};
printf("----------------int: sort before ----------------\n");
print_array(intarr, 10);
selectionSort4(intarr, 10);
printf("----------------int: sort after ----------------\n");
print_array(intarr, 10);
// 2 浮点数组测试
float farr[10] = { 2.45, 10, 9.8, 8, 7.89, 1, 3, 4, 5, 6};
cout << "----------------float: sort before ----------------" << endl;
print_array(farr, 10);
selectionSort4(farr, 10);
cout << "----------------float: sort after ----------------" << endl;
print_array(farr, 10);
float b[4] = {4.4,3.3,2.2,1.1};
selectionSort4(b,4);
for( int i = 0 ; i < 4 ; i ++ )
cout<<b[i]<<" ";
cout<<endl;
// 3 字符数组测试
char strarr[10] = {'d', 'a', 'c', 'f', 'g', 'h'};
cout << "----------------float: char before ----------------" << endl;
print_array(strarr, 6);
selectionSort4(strarr, 6);
cout << "----------------float: char after ----------------" << endl;
print_array(strarr, 6);
// 4 测试模板函数,传入字符串数组
string c[4] = {"D","C","B","A"};
selectionSort4(c,4);
for( int i = 0 ; i < 4 ; i ++ )
cout<<c[i]<<" ";
cout<<endl;
// 5 测试模板函数,传入自定义结构体Student数组
Student d[4] = { {"D",90} , {"C",100} , {"B",95} , {"A",95} };
selectionSort4(d,4);
for( int i = 0 ; i < 4 ; i ++ )
cout<<d[i];
cout<<endl;
return 0;
}