3.排序-基础排序

今天学习排序,先看很重要的sort函数,有三个参数:

fitst,起始位置(地址哦!);

last,终止位置(地址哦!);排序的时候终止位置不加入排序,大多数集成好的算法都是左闭右开

compare,比较函数,返回的是bool类型,不填则是默认的从小到大升序排序。

而对于排序而言,分为:

内定义数据类型(整型,浮点型,字符串的排序)

自定义数据类型(结构体,类)

1.内定义数据类型排序

第一题:排序

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int MAXN = 100 + 10;

int main(){
    int arr[MAXN];
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i = 0; i < n; ++i){
            scanf("%d",&arr[i]);
        }
        sort(arr,arr + n);
        for(int i = 0;i < n; ++i){
            printf("%d ",arr[i]);
        }
        printf("\n");
    }
    return 0;

}

2.自定义的数据类型进行排序

 1.设计一个比较函数

2.在定义类的时候强行定义大小关系

第二题:成绩排序

1.本题设计了一个compare比较函数,两个参数,比较x,y是否真的按照从小到大的顺序排序的

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

struct Student{
    int number;
    int score;
};

bool Compare(Student x,Student y){//比较x,y是否真的按照从小到大的顺序排序的
     if (x.score == y.score){
        return x.number < y.number;//如果x成绩比y小,则compare返回true,说明前面的比后面的小,就不用改变了
     }
     return x.score < y.score;
}


Student arr[110];//这里注意

int main(){
    int n;
    scanf("%d",&n);
    for(int i = 0;i < n; ++i){
        scanf("%d%d",&arr[i].number,&arr[i].score);
    }
    sort(arr,arr+n,Compare);//compare的参数填啥?

    for(int i = 0;i < n; ++i){
        printf("%d %d\n",arr[i].number,arr[i].score);
    }

    return 0;
}

3.若要你排序的属性本身没有大小之分,此时应该如何处理呢?

第三题:整数奇偶排序

我的想法是弄三个数组,一个判断奇偶的函数,一个输入所有数,一个存放奇数,一个存放偶数。

然后先输出奇数数组,再输出偶数数组,就欧了。好的,这个方法比较笨。

学习新的技巧吧!!!!

1.用比较函数的方式实现这样的排序

2.由于是多组样例输入,而输入的是一个数组,所以这里有个小技巧,输入第一个数!=EOF 用这样的语句写

而自己在设计比较函数时出现了一个错误,请看下面:

bool Compare(int x, int y){

    if(x % 2 == 0 && y % 2 == 0){
        return x < y;
    }else if(x % 2 == 1 && y % 2 == 1){
        return y < x;
    }else if(x % 2 == 1 && y % 2 == 0){
        return x < y;//大错特错哦!!
//这样还是在比较两个数的大小
//而题目要求奇数在前,而此时前面是奇数,所以返回true
    }else{
        return y < x;//所以这里也是错的!!!
    }
}

错误之处再重复一遍:当x为奇数,y为偶数时,他们的相对位置就不受数据大小的控制了,而若返回x < y,则表示一奇一偶按照从小到大的顺序排列。

但题目要求的是无论奇数偶数大小如何,奇数永远在偶数之前,所以,必须是返回 true!!!

正确代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int arr[10];

bool Compare(int x, int y){

    if(x % 2 == 0 && y % 2 == 0){
        return x < y;
    }else if(x % 2 == 1 && y % 2 == 1){
        return y < x;
    }else if(x % 2 == 1 && y % 2 == 0){
        return true;//这里注意了啊!!!!!
    }else{
        return false;//还有这里!!!!
    }
}

int main(){

    while(scanf("%d",&arr[0])!=EOF){//若出现输入多组数组样例,使用这个技巧!!!
        for(int i = 1;i < 10; ++i){
            scanf("%d",&arr[i]);
        }

    sort(arr, arr + 10, Compare);

    for(int i = 0; i < 10; ++i){
        printf("%d ",arr[i]);
    }
    printf("\n");
  }
    return 0;
}

所以,即便是叫你按照奇数偶数特定的属性进行排序,实际上是叫你比较数模2运算后的大小。作为考生,一切的比较,还是得找到其内部的一个比较大小的关系,进行排序,这样才能设计出比较合格的比较函数,最终实现排序

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值