选择排序--算法笔记

简单选择排序:

  • 令i从1到n进行枚举,进行n趟操作。[1,i-1]为有序区间,[i-1,n]为无序区间。
  • 每次从无序区间找到最小的元素,与i元素进行交换,这样[1,i]就变成有序区间了,这样一点点加。

算法实现:

void seleteSort(){
    for(int i = 0;i<n;i++){
        int k = i;
        for(int j = i;j<n;j++){
            if(a[j]<a[k]){//选出[i,n]中最小的元素,下标为k
                k=j;
            }
        }
        int temp=a[i];
        a[i] = a[k];
        a[k] = temp;
    }
}

插入排序:

  • 插入排序和键盘选择排序类似,都是分为前部分已排序和后部分未排序。
  • 从第二个位置开始于前边元素相比,按照插入的方式插入到前面已排序的数组中。

算法实现:

int a[maxn],n;//n为元素个数,数组下标为1-n
void insertSort(){
    for(int i = 2;i<=n;i++){//进行n-1趟排序
        int temp = a[i],j = i;//temp临时存放a[i],j从i开始往前枚举
        while(j>1&&temp<a[j-1]){//只要temp小于前一个元素a[j-1]
            a[j] = a[j-1];//把a[j-1]后移一位至a[j]
            j--;
        }
        a[j] = temp;
    }
}

Sort函数:

  • Sort函数必须加上头文件#include<algorithm>和using namespace std;
  • Sort()函数有3个,有2个是必填的。sort(首元素地址,尾元素的下一个地址,比较函数(非必填));
  • 结构体数组的排序(cmp函数的编写)
//结构体定义
struct node{
    int x,y;
}ssd[10];
//若按照x从小到大来排序
bool cmp(node a,node b){
    return a.x<b.x;
}
//若进行二级排序,即X相等时按照y的大小排序
bool cmp(node a,node b){
    if(a.x!=b.x) return a.x<b.x;
    else return a.y<b.y;
}
  •  在STL中,只有vector,string,deque是可以使用sort的,set,map这种容器是使用红黑树实现的,元素本身有序,所以不允许使用sort排序。

PAT 1025 PAT Ranking (25分)

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<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct stu{
    long long int reg;//¿¼ºÅ
    int score,finrank,loca,locarank;//·ÖÊý,×îÖÕÅÅÃû£¬¿¼³¡£¬¿¼³¡ÅÅÃû
};
bool cmp(stu a,stu b){
    return a.score==b.score?a.reg<b.reg:a.score>b.score;
}

int main(){
    int N;
    cin>>N;
    vector<stu> fin;
    for(int i = 1;i<=N;i++){
        int m;
        cin>>m;
        vector<stu> v(m);
        for(int j = 0;j<m;j++){
            cin>>v[j].reg>>v[j].score;
            v[j].loca = i;
        }
        sort(v.begin(),v.end(),cmp);
        v[0].locarank = 1;
        fin.push_back(v[0]);
        for(int j = 1;j<m;j++){//Õâ¶ù²»ÒªÐ´µ½m
            v[j].locarank = (v[j].score==v[j-1].score)?v[j-1].locarank:(j+1);
            fin.push_back(v[j]);
        }
    }
    sort(fin.begin(),fin.end(),cmp);
    fin[0].finrank = 1;
    for(int j =1;j < fin.size();j++){
        fin[j].finrank = (fin[j].score==fin[j-1].score)?(fin[j-1].finrank):(j+1);
    }
    cout<<fin.size()<<endl;
    for(int i = 0;i<fin.size();i++){
        printf("%013lld %d %d\n",fin[i].reg,fin[i].finrank,fin[i].loca,fin[i].locarank);
    }
    return 0;
}
发布了98 篇原创文章 · 获赞 2 · 访问量 3701

猜你喜欢

转载自blog.csdn.net/qq_30377869/article/details/105021854
今日推荐