PAT 排序

排序

排序题  我用所有基本的算法做了一遍

1.冒泡从这道题看出了它的效率低下

下面是代码

 1 #include <stdio.h>
 2 main()
 3 {
 4     int a[100000];
 5     int n,i,j,temp,pos;
 6     scanf("%d",&n);
 7     for(i=0;i<n;i++)
 8         scanf("%d",&a[i]);
 9     for(i=1;i<n;i++)
10     {
11         for(j=1;j<=n-i;j++)
12         {
13             if(a[j-1]>a[j])
14             {
15                 temp=a[j-1];
16                 a[j-1]=a[j];
17                 a[j]=temp;
18             }
19         }
20     }
21     printf("%d",a[0]);
22     for(i=1;i<n;i++)
23         printf(" %d",a[i]);
24 }

2.选择排序

 效率和冒泡基本一样

  

3.快排

 若pivot取第一个 效率会差很多很多  第五六个测试点过不了

 我的代码用了中间的数作为pivot

 

 1 #include <stdio.h>
 2 #define N 100000
 3
 4 void quicksort(int a[],int left,int right)
 5 {
 6     int i,j,temp,pivot;
 7     i=left;j=right;
 8     if(left<right)
 9     {
10         temp=a[i];
11         a[i]=a[(i+j)/2];
12         a[(i+j)/2]=temp;
13         pivot=a[i];
14         while(i<j)
15         {
16             while( i<j &&a[j]>pivot)
17                 j--;
18             if(i<j)
19                 a[i++]=a[j];
20             while(i<j&&a[i]<pivot)
21                 i++;
22             if(i<j)
23                 a[j--]=a[i]    ;
24         }
25         a[i]=pivot;
26         quicksort(a,left,i-1);
27         quicksort(a,i+1,right);
28     }
29
30
31 }
32 int main()
33 {
34     int i,a[N],n;
35     scanf("%d",&n);
36
37     for(i=0;i<n;i++)
38         scanf("%d",&a[i]);
39     quicksort(a,0,n-1);
40     printf("%d",a[0]);
41     for(i=1;i<n;i++)
42         printf(" %d",a[i]);
43 }

4.堆排

 和快排基本差不多

 

 1 #define N 100002
 2 #include "stdio.h"
 3 void HeapAdjust(int a[],int s,int n)
 4 {
 5     int j,t;
 6     while(2*s<=n)
 7     {
 8         j=2*s;
 9         if(a[j]<a[j+1]&&j+1<=n)
10             j++;
11         if(a[s]<a[j])
12         {
13             t=a[s];
14             a[s]=a[j];
15             a[j]=t;
16             s=j;
17         }
18         else
19             break;
20     }
21 }
22 void HeapSort(int a[],int n)
23 {
24     int i,t;
25     for(i=n/2;i>=1;i--)
26         HeapAdjust(a,i,n);
27     for(i=n;i>=1;i--)
28     {
29         t=a[1];
30         a[1]=a[i];
31         a[i]=t;
32         HeapAdjust(a,1,i-1);
33     }
34 }
35 main()
36 {
37     int i,a[N],n;
38     scanf("%d",&n);
39     a[0]=-1;
40     for(i=1;i<=n;i++)
41         scanf("%d",&a[i]);
42     HeapSort(a,n);
43     printf("%d",a[1]);
44     for(i=2;i<=n;i++)
45         printf(" %d",a[i]);
46 }

5.归并排序

  不知道为什么提交上去 各种段错误  希望大家帮忙看看代码 据说这个要比快排效率高

 

 1 #define N 100002
 2 #include "stdio.h"
 3 void HeapAdjust(int a[],int s,int n)
 4 {
 5     int j,t;
 6     while(2*s<=n)
 7     {
 8         j=2*s;
 9         if(a[j]<a[j+1]&&j+1<=n)
10             j++;
11         if(a[s]<a[j])
12         {
13             t=a[s];
14             a[s]=a[j];
15             a[j]=t;
16             s=j;
17         }
18         else
19             break;
20     }
21 }
22 void HeapSort(int a[],int n)
23 {
24     int i,t;
25     for(i=n/2;i>=1;i--)
26         HeapAdjust(a,i,n);
27     for(i=n;i>=1;i--)
28     {
29         t=a[1];
30         a[1]=a[i];
31         a[i]=t;
32         HeapAdjust(a,1,i-1);
33     }
34 }
35 main()
36 {
37     int i,a[N],n;
38     scanf("%d",&n);
39     a[0]=-1;
40     for(i=1;i<=n;i++)
41         scanf("%d",&a[i]);
42     HeapSort(a,n);
43     printf("%d",a[1]);
44     for(i=2;i<=n;i++)
45         printf(" %d",a[i]);
46 }
时间: 2024-12-26 10:37:57

PAT 排序的相关文章

1025 PAT Ranking[排序][一般]

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 immediat

PAT 甲级1025 PAT Ranking (25 分)(结构体排序,第一次超时了,一次sort即可小技巧优化)

题意: 给定一次PAT测试的成绩,要求输出考生的编号,总排名,考场编号以及考场排名. 分析: 题意很简单嘛,一开始上来就,一组组输入,一组组排序并记录组内排名,然后再来个总排序并算总排名,结果发现最后一个测试点超时. 发现自己一开始太傻太盲目,其实只要一次性全部输进来,记录好考场编号,一次排序就可以了.既然只排了一次,怎么计算考场排名呢,这里我用了三个数组 int g_rank[100];//记录各个考场当前排到的名次 (当前最后一个人的名次) int g_score[100];//记录个考场当

【算法学习记录-排序题】【PAT A1025】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 i

PAT A 1022. Digital Library (30)【结构体排序检索】

https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #include <string> #include <algorithm> using namespace std; struct book //图书结构 { string id; string title; string author; int k; //关键词数量 string key[5

1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise

题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding i

PAT 1088 1089. Insert or Merge (25)(排序啊)

题目链接:http://www.patest.cn/contests/pat-a-practise/1089 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input d

PAT甲题题解-1016. Phone Bills (25)-模拟、排序

博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789229.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 给出一天24小时内,每个小时内,每分钟的通话费用给出n个记录,on-line表示通话的开始,off-line表示通话的结束如果on-line/off-line没有对应的另一个,忽略即可 先按人名排序,名称一样的按时间排序,这里时间统一按分钟来算,即一天有24

1029. Median (25)【排序】——PAT (Advanced Level) Practise

题目信息 1029. Median (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17}

PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!

博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 题意:给出n个数,求拼接后值最小的数是多少. 一开始就简单的把所有字符串先从小到大拍个序,然后拼接起来去掉前导零,结果发现有个问题,比如下面两个32 32321如果按常规字符串比较,32是在32321前面.然而,32321-32才是较小的方案如何有个好的比较