ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

选择排序(C++实现)

2022-07-20 22:37:19  阅读:145  来源: 互联网

标签:minIndex 10 arr include int C++ 选择 ++ 排序


  1. 对选择排序的理解
    每次选择最小的值往前放。
    比如9,3,8排序:每次选择最小的数放在前面,第一次选3放在第一位,第二次选8放在第三位,第三次选择9放在第三位,直到排序结束。
    代码:(举例int型数据排序)
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    void selectionSort(int arr[], int n) {
    
        for (int i = 0; i < n; i++) {
            // 寻找[i, n)区间里的最小值
            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 a[10] = { 10,9,8,7,6,5,4,3,2,1 };
        selectionSort(a, 10);
        for (int i = 0; i < 10; i++)
            cout << a[i] << " ";
        cout << endl;
    
        return 0;
    }

     

  2. 使用模板(泛型)编写选择排序
    Student.h:
    #ifndef INC_02_SELECTION_SORT_USING_TEMPLATE_STUDENT_H
    #define INC_02_SELECTION_SORT_USING_TEMPLATE_STUDENT_H
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    struct Student{
    
        string name;
        int score;
    
        // 重载小于运算法,定义Student之间的比较方式
        // 如果分数相等,则按照名字的字母序排序
        // 如果分数不等,则分数高的靠前
        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 //INC_02_SELECTION_SORT_USING_TEMPLATE_STUDENT_H

     main.cpp:

    #include <iostream>
    #include "Student.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 a[10] = {10,9,8,7,6,5,4,3,2,1};
        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;
    }

     

标签:minIndex,10,arr,include,int,C++,选择,++,排序
来源: https://www.cnblogs.com/mjj1024/p/16500090.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有