排序算法
为什么要学习O(n^2)的排序算法?
基础
- 编码简单,易于实现,是一些简单情景的首选
- 在一些特殊情况下,简单的排序算法更有效
- 简单的排序算法思想衍生出复杂的排序算法
- 作为子过程,改进更复杂的排序算法
选择排序——Selection Sort
依次在剩下的部分(当前还没有排序的部分)找出最小的元素,与第一个调换位置。
#include <iostream>
using namespace std;
void selectionSort(int arr[] ,int n){
for(int i=0;i<n;i++){
//寻找[i,n)区间里的最小值
int mid=i;
//找出未排序部分的最小值,将他的下标值赋给mid,将最小值的索引存在mid中
for(int j=i+1;j<n;j++){
if(arr[mid]>arr[j]){
mid=j;//更新mid
}
}
//找出最小值,此时mid的值就是其下标,交换
//交换函数swap(),c++标准库中内置的函数,在命名空间std中
swap(arr[i],arr[mid]);
}
}
int main() {
int a[10]={5,8,9,7,3,0,2,1,4,6};
selectionSort(a,10);
for(int i=0;i<10;i++){
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
只能对整形进行排序——改进:
main.cpp
#include <iostream>
#include "Student.h"
using namespace std;
//要排序的内容很多,所以使用模板函数(泛型)
template <typename T>
//出入的数组为T类型
void selectionSort(T arr[] ,int n){
for(int i=0;i<n;i++){
//寻找[i,n)区间里的最小值
int mid=i;
//找出未排序部分的最小值,将他的下标值赋给mid,将最小值的索引存在mid中
for(int j=i+1;j<n;j++){
if(arr[j]<arr[mid]){
mid=j;//更新mid
}
}
//找出最小值,此时mid的值就是其下标,交换
//交换函数swap(),c++标准库中内置的函数,在命名空间std中
swap(arr[i],arr[mid]);
}
}
int main() {
//传入整型数组
int a[10]={5,8,9,7,3,0,2,1,4,6};
//调用选择排序函数
selectionSort(a,10);
//遍历打印数组
for(int i=0;i<10;i++){
cout<<a[i]<<" ";
}
cout<<endl;
// 测试模板函数,传入浮点数数组
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;
// 测试模板函数,传入字符串数组
string c[4] = {"D","C","B","A"};
selectionSort(c,4);
for( int i = 0 ; i < 4 ; i ++ )
cout<<c[i]<<" ";
cout<<endl;
// 测试模板函数,传入自定义结构体Student数组
Student d[4] = { {"D",90} , {"C",100} , {"B",95} , {"A",95} };
selectionSort(d,4);
for( int i = 0 ; i < 4 ; i ++ )
cout<<d[i];
cout<<endl;
return 0;
}
Student.h
//宏定义
#ifndef SELECTIONSORT_STUDENT_H
#define SELECTIONSORT_STUDENT_H
//再此写声明和实现,开源项目.h文件不对外隐藏,.cpp编译后对外隐藏
#include <iostream>
#include <string>
using namespace std;
struct Student{
string name;
int score;
//运算符重载 <
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;
}
};
#endif //SELECTIONSORT_STUDENT_H
用代码生成随机用例:
SortTestHelper.h
//
// Created by administrator on 2019/2/2.
//
#ifndef SELECTIONSORT_SORTTESTHELPER_H
#define SELECTIONSORT_SORTTESTHELPER_H
#include <iostream>
#include <ctime>
#include <cassert>
using namespace std;
namespace SortTestHelper {
// 生成有n个元素的随机数组,每个元素的随机范围为[rangeL, rangeR]
int *generateRandomArray(int n, int rangeL, int rangeR) {
assert(rangeL <= rangeR);
int *arr = new int[n];
srand(time(NULL));
for (int i = 0; i < n; i++)
arr[i] = rand() % (rangeR - rangeL + 1) + rangeL;
return arr;
}
template<typename T>
void printArray(T arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return;
}
};
#endif //SELECTIONSORT_SORTTESTHELPER_H
main.cpp
#include <iostream>
#include "SortTestHelper.h"
using namespace std;
template<typename T>
void selectionSort(T arr[], int n){
for(int i = 0 ; i < n ; i ++){
int minIndex = i;
for( int j = i + 1 ; j < n ; j ++ )
if( arr[j] < arr[minIndex] )
minIndex = j;
swap( arr[i] , arr[minIndex] );
}
}
int main() {
// 测试排序算法辅助函数
int N = 10000;
int *arr = SortTestHelper::generateRandomArray(N,0,100000);
selectionSort(arr,N);
SortTestHelper::printArray(arr,N);
delete[] arr;
return 0;
}