如何用库函数sort进行自定义排序 用法详解 C语言入门

欢迎关注笔者,你的支持是持续更博的最大动力

目录

  • sort排序
      • 用法一
      • 用法二
      • 用法三
  • 相关内容
  • 其他

在标准模版库(Standard Template Library)中,包含一些常用的算法和数据结构可供调用。

使用前提:#include

sort排序

sort就是STL中已经包含的排序算法,是用快速排序实现的,时间复杂度 O(nlogn)。

语法:sort (数组名+n1,数组名+n2,排序规则名( ) );

sort函数可以:

  • 基础数据类型的数组进行默认排序;
  • 基础数据类型的数组进行自定义排序;
  • 任意数据类型的数组进行自定义排序。

下面将举例说明这三大用法:

用法一

对基本类型的数组进行默认排序(从小到大),默认排序则不用写第三个参数 (排序规则名):
sort (数组名+n1,数组名+n2);

意思是,将数组中下标范围为 [n1, n2) 的元素从小到大排序。

举例

对整个数组排序,[0, 7):
int a[ ] = {15, 4, 3, 9, 7, 2, 6};
sort(a, a+7);
结果:{2,3,4,6,7,9,15}

对前三个元素排序,[0, 3):
int a[ ] = {15, 4, 3, 9, 7, 2, 6};
sort(a, a+7);
结果:{3, 4, 15, 9, 7, 2, 6}

对第3个到第5个元素排序,[2, 5):
int a[ ] = {15, 4, 3, 9, 7, 2, 6};
sort(a+2, a+5);
结果:{15, 4, 3, 7, 9, 2, 6}

用法二

对基本类型数组从大到小排序,规则名greater,需要写明被排序的元素类型T:
sort (数组名+n1,数组名+n2,greater<T>() );

举例

对第2个元素到第4个元素排序,[1, 4):
int a[ ] = {15, 4, 3, 9, 7, 2, 6};
sort(a+1, a+4, greater<int>() );
结果:{15, 9, 4, 3, 7, 2, 6}

用法三

自定义的排序规则,对任何类型T的数组排序:
sort (数组名+n1,数组名+n2,排序规则结构名() );

排序规则结构的定义方式:

struct 结构名 {
	bool operator() (const T & a1, const T & a2) const {
		//若a1应该在a2前面,返回true
		//否则,返回false,相等也是false
	}
};

下面将举例:

  1. int 数据类型的数组按2个规则排序
  2. 给自定义的 Student 数据类型按学生姓名、学号、绩点分别排序

举例1:int 数据类型

//创建按从大到小排序规则
struct Rule1 {
    bool operator()(const int & a1, const int & a2) const{
        return a1 > a2;
    }
};

//创建按个位数从小到大排序规则
struct Rule2 {
    bool operator()(const int & a1, const int & a2) const{
        return a1%10 < a2%10;
    }
};

//创建Print函数,把常用的输出语句放进来,每次输出调用即可,方便输出内容
void Print(int a[], int size){
    for (int i = 0; i < size; ++i)
        cout << a[i] << ",";
    cout << endl;
}

int main (){
    int a[] = {12, 45, 3, 98, 21, 7};
    int n = sizeof(a) / sizeof(int);  // 24/4 = 6 数组大小/单个元素大小=元素个数,共有6个元素
    
    //从小到大
    sort(a, a+n);
    cout << "1) "; Print(a, n);       // 1) 3,7,12,21,45,98,
    
    //从大到小
    sort(a, a+n, Rule1());
    cout << "2) "; Print(a, n);       //2) 98,45,21,12,7,3,
    
    //按个位数从小到大
    sort(a, a+n, Rule2());
    cout << "3) "; Print(a, n);       //3) 21,12,3,45,7,98,
    
	return 0;
}

举例2:Student 自定义数据类型

//用"struct"关键字定义新的数据类型:学生 Student
struct Student {
    char name[20];
    int id;
    double gpa;
};

//初始化一个学生类型的数组,里面有四个学生的信息:姓名、学号、绩点
Student students [] = {
    {"Jack", 1013, 4.7},
    {"Marry", 1011, 3.4},
    {"John", 1010, 3.7},
    {"Alex", 1012, 4.2}
};

//创建按学生姓名排序规则
struct StudentRule1 {
    bool operator()(const Student & s1, const Student & s2) const{
        if (strcmp(s1.name, s2.name) < 0)      //比较string compare,比较字符串字典序
            return true;
        return s1.name < s2.name;
    }
};

//创建按学生绩点排序规则
struct StudentRule2 {
    bool operator()(const Student & s1, const Student & s2) const{
        return s1.gpa > s2.gpa;
    }
};

//创建按学生学号排序规则
struct StudentRule3 {
    bool operator()(const Student & s1, const Student & s2) const{
        return s1.id < s2.id;
    }
};

//创建PrintStudent函数,每次输出调用即可,方便输出学生信息
void PrintStudent(Student s[], int size){
    for (int i = 0; i < size; ++i)
        cout << "(" << s[i].name << "," << s[i].id << "," << s[i].gpa << ") ";
    cout << endl;
}

int main (){
    int n = sizeof(students)/ sizeof(Student);
    
    //按学生姓名排序
    sort(students, students+n, StudentRule1());
    PrintStudent(students, n);
    
    //按学生绩点排序
    sort(students, students+n, StudentRule2());
    PrintStudent(students, n);
    
    //按学生学号排序
    sort(students, students+n, StudentRule3());
    PrintStudent(students, n);
    
    return 0;
}

相关内容

  • 选择排序
  • 插入排序
  • 冒泡排序

其他

日常vlog: 点这里去B站~


你可能感兴趣的:(C入门,排序算法,数据结构,算法,c语言)