暑期算法心得--排序

今天上午主要学习了两种排序算法,即插入排序和选择排序。

  • 所谓插入排序,就是对于一个数组A[1,2,....N]而言,令i从到N枚举,进行n糖操作,每趟操作从待排序部分[i,n]中选择最小的元素,令其与待排序部分的第一个元素A[i]进行交换,这样一来总能确保数组A[1,2,....,i]是有序的。
  • 所谓选择排序,也是确保数组A[1,2,....,N]的前i个元素是有序的,不同之处在于每次将数组右半部分的元素插入到数组的左半部分当中,需要一个临时变量记录待插入元素的值和序号,然后向左开始遍历,直至找到可插入元素的位置为止。

在用代码书写选择排序算法的时候,每次需要记录哨兵的下标值以及有可能进行交换的元素的值。

在用代码书写插入排序算法的时候,每次需要确定右半部分数组首元素的下标值i,然后就是在左半部分数组汇总去寻找A[I]的安身之处。 

今天上午还完后曾了一道PAT的排序题,这道题难度不大,但却做了将近一个小时,主要还是对排序算法不熟,手感生疏。

一下就是这道PAT的题目: 

1025 PAT Ranking (25)(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

时间限制: 200ms

内存限制: 64MB

这里总结一下收获,然后再亮出代码。

  • 对于个题信息而言,最后能设置一个结构体记录一下每个个体的信息
  • 掌握对于结构体数组排序的方法:一般来说,可以使用C++的sort函数进行排序,也可以使用上面提到的两种方法进行排序,这道题目是一道典型的排序题,包括局部排序和全局排序。至于中间排序的一些细节,只可意会不可言传,每隔2-3题就要注意提醒自己。
  • #include <cstdio>
    #include <string.h>
    
    using namespace std;
    
    typedef struct Student
    {
        char id[14];
        int score;
        int final_rank;
        int location_number;
        int location_rank;
    };
    
    int N,K;
    Student student[10000];
    
    //使用选择排序实现排序算法
    void rank_sort(Student* stu,int start,int en)
    {
        int i,j,k;
        int r = 0;
        for(i=start;i<en;i++)
        {
            k=i;
            for(j=i;j<en;j++)
            {
                if(stu[j].score>stu[k].score||((stu[j].score==stu[k].score)&&strcmp(stu[j].id,stu[k].id)<0))
                {
                    k=j;
                }
            }
            Student temp = stu[i];
            stu[i] = stu[k];
            stu[k] = temp;
        }
        stu[start].location_rank=1;
        int cnt=1;
        for(i=start+1;i<en;i++)
        {
            cnt++;
            Student tmep=stu[i];
            Student tmep0=stu[i-1];
            if(stu[i].score!=stu[i-1].score)
            {
                stu[i].location_rank=cnt;
            }
            else
            {
                stu[i].location_rank=stu[i-1].location_rank;
            }
        }
    }
    
    void global_sort(Student* stu,int start,int en)
    {
        int i,j,k;
        int r = 0;
        for(i=start;i<en;i++)
        {
            k=i;
            for(j=i;j<en;j++)
            {
                if(stu[j].score>stu[k].score||((stu[j].score==stu[k].score)&&strcmp(stu[j].id,stu[k].id)<0))
                {
                    k=j;
                }
            }
            Student temp = stu[i];
            stu[i] = stu[k];
            stu[k] = temp;
        }
        stu[start].final_rank=1;
        int cnt=1;
        for(i=start+1;i<en;i++)
        {
            cnt++;
            if(stu[i].score!=stu[i-1].score)
            {
                stu[i].final_rank=cnt;//这里要特别注意了,不能简单以为就是stu[i-1].final_rank+1
            }
            else
            {
                stu[i].final_rank=stu[i-1].final_rank;
            }
        }
    }
    
    
    int main()
    {
        int location_num=0;
        int total_num=0;
        int local_start=0;
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            scanf("%d",&K);
            location_num++;
            local_start=total_num;
            for(int j=0;j<K;j++)
            {
                scanf("%s %d",student[total_num].id,&student[total_num].score);
                student[total_num].location_number=location_num;
                total_num++;
            }
            rank_sort(student,local_start,local_start+K);
        }
        global_sort(student,0,total_num);
        printf("%d\n",total_num);
        for(int i=0;i<total_num;i++)
        {
            printf("%s %d %d %d\n",student[i].id,student[i].final_rank,student[i].location_number,student[i].location_rank);
        }
        return 0;
    }
    
    

评测结果:

猜你喜欢

转载自blog.csdn.net/chengsilin666/article/details/81111810