排序

排序

归并排序

void merge(int a[],int l1,int r1,int l2,int r2)//合并两个区间
{
    int i=l1;
    int j=l2;
    int temp[100];
    int index=0;
    while(i<=r1&&j<=r2)//分别从两端开始
    {
        if(a[i]<=a[j]) temp[index++]=a[i++];
        else temp[index++]=a[j++];
    }
    while(i<=r1) temp[index++]=a[i++];
    while(j<=r2) temp[index++]=a[j++];
    for(int i=0;i<index;i++)
    {
        a[l1+i]=temp[i];
    }
}
void mergesort(int a[],int left,int right)
{
    if(left<right) 
    {
        int mid=(left+right)/2;
        mergesort(a,left,mid);//递归二分
        mergesort(a,mid+1,right);
        merge(a,left,mid,mid+1,right);//合并
    }
}

快速排序

int position(int  a[],int left,int right)//寻找主元
{
int temp=a[left];//把最左边的元素给临时变量
while(left<right)//temp左边元素小于等于temp,右边元素大于temp
{
while(left<right&&temp<a[right]) right--;
a[left]=a[right];
while(left<right&&temp>=a[left]) left++;
a[right]=a[left];
}
a[left]=temp;
return left;返回left位置
}
void quicksort(int a[],int left,int right)
{
if(left<right)
{
int pos=postion(a,left,right);
quicksort(a,left,pos-1);//对左子区间递归排序
quicksort(a,pos+1,right);
}
}

快速排序题目-PAT

著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边。 给定划分后的 N 个互不相同的正整数的排列,请问有多少个元素可能是划分前选取的主元?

例如给定 N = 5 N = 5 N=5, 排列是1、3、2、4、5。则:

1 的左边没有元素,右边的元素都比它大,所以它可能是主元;
尽管 3 的左边元素都比它小,但其右边的 2 比它小,所以它不能是主元;
尽管 2 的右边元素都比它大,但其左边的 3 比它大,所以它不能是主元;
类似原因,4 和 5 都可能是主元。
因此,有 3 个元素可能是主元。

输入格式:

输入在第 1 行中给出一个正整数 N(≤10
​5
​​ ); 第 2 行是空格分隔的 N 个不同的正整数,每个数不超过 10
​9
​​ 。

输出格式:

在第 1 行中输出有可能是主元的元素个数;在第 2 行中按递增顺序输出这些元素,其间以 1 个空格分隔,行首尾不得有多余空格。

输入样例:

5
1 3 2 4 5

输出样例:

3
1 4 5

代码:

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int N;
    int judge[100000][2]={0,0};//数组一维存数,二维判断
    vector<int>v;
    int min=0;
    int max=1000000001;
    scanf("%d",&N);
    for(int i=0;i<N;i++)
    {
        scanf("%d",&judge[i][0]);
        if(judge[i][0]>min)//若大于左边的数,二维++
        {
            min=judge[i][0];
            judge[i][1]++;
        }
    }
    for(int i=N-1;i>=0;i--)
    {
        if(judge[i][0]<max)//若小于右边的数,二维++
        {
            max=judge[i][0];
            judge[i][1]++;
        }
        if(judge[i][1]==2)//若满足条件,将此数入vector
        v.push_back(judge[i][0]);
    }
    sort(v.begin(),v.end());
    printf("%d\n",v.size());
    for(int i=0;i<v.size();i++)
    {
            printf("%d%c",v[i],i==v.size()-1?'\n':' ');
    }
    if(!v.size())
    printf("\n");
    return 0;
}

冒泡排序

1.	#include <cstdio>  
2.	void BubbleSort(int a[], int n)  
3.	{  
4.	    for (int i = 0; i < n - 1; i++)  
5.	    {  
6.	        for (int j = 0; j < n - i - 1; j++)  
7.	        {  
8.	            if (a[j] > a[j + 1])  
9.	            {  
10.	                int temp = a[j];  
11.	                a[j] = a[j + 1];  
12.	                a[j + 1] = temp;  
13.	            }  
14.	        }  
15.	    }  
16.	    return;  
17.	}  
18.	int main()  
19.	{  
20.	    int n;  
21.	    scanf_s("%d", &n);  
22.	    int a[100];  
23.	    for (int i = 0; i < n; i++)  
24.	    {  
25.	        scanf_s("%d", &a[i]);  
26.	    }  
27.	    BubbleSort(a, n);  
28.	    for (int i = 0; i < n; i++)  
29.	    {  
30.	        printf("%d\n", a[i]);  
31.	    }  
32.	    return 0;  
33.	}  

选择排序

1.	#include <cstdio>  
2.	void SelectSort(int a[], int n)  
3.	{  
4.	    int k;  
5.	    for (int i = 0; i < n ; i++)  
6.	    {  
7.	        k = i;  
8.	        for (int j = i; j < n; j++)  
9.	        {  
10.	            if (a[j] < a[k])  
11.	            {  
12.	                k = j;  
13.	            }  
14.	        }  
15.	                int temp = a[i];  
16.	                a[i] = a[k];  
17.	                a[k] = temp;  
18.	    }  
19.	    return;  
20.	}  
21.	int main()  
22.	{  
23.	    int n;  
24.	    scanf_s("%d", &n);  
25.	    int a[100];  
26.	    for (int i = 0; i < n; i++)  
27.	    {  
28.	        scanf_s("%d", &a[i]);  
29.	    }  
30.	    SelectSort(a, n);  
31.	    for (int i = 0; i < n; i++)  
32.	    {  
33.	        printf("%d\n", a[i]);  
34.	    }  
35.	    return 0;  
36.	}  

插入排序

1.	#include <cstdio>  
2.	void InsertSort(int a[], int n)  
3.	{  
4.	    int j; int temp;  
5.	    for (int i = 1; i < n ; i++)  
6.	    {  
7.	        j = i; temp = a[j];  
8.	        while (j > 0 && temp< a[j - 1])  
9.	        {  
10.	            a[j] = a[j - 1];  
11.	            j--;  
12.	        }  
13.	        a[j] = temp;  
14.	    }  
15.	    return;  
16.	}  
17.	int main()  
18.	{  
19.	    int n;  
20.	    scanf_s("%d", &n);  
21.	    int a[100];  
22.	    for (int i = 0; i < n; i++)  
23.	    {  
24.	        scanf_s("%d", &a[i]);  
25.	    }  
26.	    InsertSort(a, n);  
27.	    for (int i = 0; i < n; i++)  
28.	    {  
29.	        printf("%d\n", a[i]);  
30.	    }  
31.	    return 0;  
32.	}  

STL标准模板库

#include <algorithm>
using namespace std;
bool cmp();
sort();

成绩排名

读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:

每个测试输入包含 1 个测试用例,格式为

第 1 行:正整数 n
第 2 行:第 1 个学生的姓名 学号 成绩
第 3 行:第 2 个学生的姓名 学号 成绩
… … …
第 n+1 行:第 n 个学生的姓名 学号 成绩
其中姓名和学号均为不超过 10 个字符的字符串,成绩为 0 到 100 之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

输出格式:

对每个测试用例输出 2 行,第 1 行是成绩最高学生的姓名和学号,第 2 行是成绩最低学生的姓名和学号,字符串间有 1 空格。

输入样例:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

输出样例:

Mike CS991301
Joe Math990112

#include <cstdio>
struct student
{
    char name[11];
    char id[11];
    int score;
}stu[100],min,max;
int main()
{
    int n;
    max.score=-1;
    min.score=101;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        getchar();
        scanf("%s %s %d",stu[i].name,stu[i].id,&stu[i].score);
        if(stu[i].score<min.score) min=stu[i];
        if(stu[i].score>max.score) max=stu[i];
    }
    printf("%s %s\n%s %s",max.name,max.id,min.name,min.id);
    return 0;
}

德才论

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤10
​5
​​ ),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct student
{
    int id;
    int de;
    int cai;
    int flag;
    int sum;
}stu[100010];
bool cmp(student a,student b)
{
    if(a.flag!=b.flag) return a.flag<b.flag;
    else if(a.sum!=b.sum) return a.sum>b.sum;
    else if(a.de!=b.de) return a.de>b.de;
    else return a.id<b.id;
}
int main()
{
    int n,l,h;
    scanf("%d%d%d",&n,&l,&h);
    int m=n;
    for(int i=0;i<n;i++)
    {
        scanf("%d %d %d",&stu[i].id,&stu[i].de,&stu[i].cai);
        stu[i].sum=stu[i].de+stu[i].cai;
        if(stu[i].de<l||stu[i].cai<l) 
        {
            stu[i].flag=5;
            m--;
        }
        else if(stu[i].de>=h&&stu[i].cai>=h) stu[i].flag=1;
        else if(stu[i].de>=h&&stu[i].cai<h) stu[i].flag=2;
        else if(stu[i].de>=stu[i].cai) stu[i].flag=3;
        else stu[i].flag=4;
    }
    sort(stu,stu+n,cmp);
    printf("%d\n",m);
    for(int i=0;i<m;i++)
    {
        printf("%d %d %d\n",stu[i].id,stu[i].de,stu[i].cai);
    }
    return 0;
}

PAT Ranking

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank
The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct student
{
    char name[20];
    int score,locrank,rank,loc;
}stu[60000];
bool cmp(student a,student b)
{
    if(a.score!=b.score) return a.score>b.score;
    else return strcmp(a.name,b.name)<0;
}
int main()
{
    int n;
    scanf("%d",&n);
    int m;
    int num=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&m);
        for(int j=0;j<m;j++)
        {
            scanf("%s%d",stu[num].name,&stu[num].score);
            stu[num].loc=i;
            num++;
        }
        sort(stu+num-m,stu+num,cmp);
        stu[num-m].locrank=1;
        for(int j=num-m+1;j<num;j++)
        {
            if(stu[j].score==stu[j-1].score)
            stu[j].locrank=stu[j-1].locrank;
            else stu[j].locrank=j+1-(num-m);
        }
    }
    printf("%d\n",num);
    sort(stu,stu+num,cmp);
    int r=1;
    for(int i=0;i<num;i++)
    {
        printf("%s ",stu[i].name);
        if(i>0&&stu[i].score!=stu[i-1].score)
        r=i+1;
        stu[i].rank=r;
        printf("%d %d %d\n",stu[i].rank,stu[i].loc,stu[i].locrank);
    }
    return 0;
}

注:上篇代码来自

版权声明:本文为CSDN博主「负负」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37377504/article/details/77898150

其中多数代码来自胡凡大神-《算法笔记》,抱着学习的态度总结排序,如有介意,联系删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值