• 选择排序(C++实现)


    1. 对选择排序的理解
      每次选择最小的值往前放。
      比如9,3,8排序:每次选择最小的数放在前面,第一次选3放在第一位,第二次选8放在第三位,第三次选择9放在第三位,直到排序结束。
      代码:(举例int型数据排序)
      1. #include
      2. #include
      3. using namespace std;
      4. void selectionSort(int arr[], int n) {
      5. for (int i = 0; i < n; i++) {
      6. // 寻找[i, n)区间里的最小值
      7. int minIndex = i;
      8. for (int j = i + 1; j < n; j++)
      9. if (arr[j] < arr[minIndex])
      10. minIndex = j;
      11. swap(arr[i], arr[minIndex]);
      12. }
      13. }
      14. int main() {
      15. int a[10] = { 10,9,8,7,6,5,4,3,2,1 };
      16. selectionSort(a, 10);
      17. for (int i = 0; i < 10; i++)
      18. cout << a[i] << " ";
      19. cout << endl;
      20. return 0;
      21. }
    2. 使用模板(泛型)编写选择排序
      Student.h:
      1. #ifndef INC_02_SELECTION_SORT_USING_TEMPLATE_STUDENT_H
      2. #define INC_02_SELECTION_SORT_USING_TEMPLATE_STUDENT_H
      3. #include
      4. #include
      5. using namespace std;
      6. struct Student{
      7. string name;
      8. int score;
      9. // 重载小于运算法,定义Student之间的比较方式
      10. // 如果分数相等,则按照名字的字母序排序
      11. // 如果分数不等,则分数高的靠前
      12. bool operator<(const Student& otherStudent){
      13. return score != otherStudent.score ?
      14. score > otherStudent.score : name < otherStudent.name;
      15. }
      16. friend ostream& operator<<(ostream &os, const Student &student){
      17. os<<"Student: "<" "<
      18. return os;
      19. }
      20. };
      21. #endif //INC_02_SELECTION_SORT_USING_TEMPLATE_STUDENT_H

      main.cpp:
       

      1. #include
      2. #include "Student.h"
      3. using namespace std;
      4. template<typename T>
      5. void selectionSort(T arr[], int n){
      6. for(int i = 0 ; i < n ; i ++){
      7. int minIndex = i;
      8. for( int j = i + 1 ; j < n ; j ++ )
      9. if( arr[j] < arr[minIndex] )
      10. minIndex = j;
      11. swap( arr[i] , arr[minIndex] );
      12. }
      13. }
      14. int main() {
      15. // 测试模板函数,传入整型数组
      16. int a[10] = {10,9,8,7,6,5,4,3,2,1};
      17. selectionSort( a , 10 );
      18. for( int i = 0 ; i < 10 ; i ++ )
      19. cout<" ";
      20. cout<
      21. // 测试模板函数,传入浮点数数组
      22. float b[4] = {4.4,3.3,2.2,1.1};
      23. selectionSort(b,4);
      24. for( int i = 0 ; i < 4 ; i ++ )
      25. cout<" ";
      26. cout<
      27. // 测试模板函数,传入字符串数组
      28. string c[4] = {"D","C","B","A"};
      29. selectionSort(c,4);
      30. for( int i = 0 ; i < 4 ; i ++ )
      31. cout<" ";
      32. cout<
      33. // 测试模板函数,传入自定义结构体Student数组
      34. Student d[4] = { {"D",90} , {"C",100} , {"B",95} , {"A",95} };
      35. selectionSort(d,4);
      36. for( int i = 0 ; i < 4 ; i ++ )
      37. cout<
      38. cout<
      39. return 0;
      40. }

  • 相关阅读:
    【Java】泛型 之 super通配符
    ReactNative 井字游戏 实战
    Java中的代码重构:技巧、优秀实践与方法
    力扣每日一题:最接近目标价格的甜点成【dfs 暴力搜索】
    每日五问(java)
    JavaScript之运算符相关知识
    GC 垃圾回收机制
    洛谷千题详解 | P1016 [NOIP1999 提高组] 旅行家的预算【C++、Java、Python语言】
    BeautifulSoup4库
    机器学习教程
  • 原文地址:https://blog.csdn.net/mjj1024/article/details/125905474