给定NN个(长整型范围内的)整数,要求输出从小到大排序后的结果。

本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:

  • 数据1:只有1个元素;
  • 数据2:11个不相同的整数,测试基本正确性;
  • 数据3:103个随机整数;
  • 数据4:104个随机整数;
  • 数据5:105个随机整数;
  • 数据6:105个顺序整数;
  • 数据7:105个逆序整数;
  • 数据8:105个基本有序的整数;
  • 数据9:105个随机正整数,每个数字不超过1000。

    输入格式:

    输入第一行给出正整数NN(\le 10^5≤10​5​​),随后一行给出NN个(长整型范围内的)整数,其间以空格分隔。

    输出格式:

    在一行中输出从小到大排序后的结果,数字间以1个空格分隔,行末不得有多余空格。

    输入样例:

    11
    4 981 10 -17 0 -20 29 50 8 43 -5

    输出样例:

    -20 -17 -5 0 4 8 10 29 43 50 981
  • 下面试验了各种排序算法的的表现
  • 算法/时间复杂度

    10^3的随机整数

    10^4个随机整数

    10^5个随机整数

    10^5个顺序整数

    10^5个逆序整数

    10^5个基本有序的整数

    10^5个随机正整数,每个数不超过1000

    冒泡排序

    4ms

    228ms

    >10s

    88ms

    >10s

    650ms

    >10s

    插入排序

    3ms

    35ms

    4784ms

    82ms

    9206ms

    115ms

    4499ms

    选择排序

    5ms

    332ms

    >10s

    >10s

    >10s

    >10s

    >10s

    归并排序(递归版本)

    4ms

    12ms

    131ms

    82ms

    127ms

    83ms

    75ms

    堆排序

    3ms

    10ms

    103ms

    89ms

    102ms

    126ms

    94ms

    希尔排序

    3ms

    25ms

    128ms

    119ms

    125ms

    117ms

    116ms

    归并排序(循环版本)

    3ms

    24ms

    125ms

    78ms

    99ms

    77ms

    93ms

    快速排序(pivot取中位数)

    3ms

    10ms

    122ms

    76ms

    112ms

    76ms

    69ms

/* 冒泡排序
* 1.最好情况 已经有序了O(n)
* 2.最坏情况 逆序 O(n^2)
* 3.平均情况 O(n^2)
*/
void bubble_sort(int a[],int n) {
for (int i = n - ; i > ; i--) {
int flag = ;
for (int j = ; j < i; j++) {
if (a[j] > a[j + ]) {
int temp = a[j];
a[j] = a[j + ];
a[j + ] = temp;
flag = ;
}
}
if (flag == )
break;
}
}
/*
* 插入排序(对于基本有序的数组排序有较好的表现)
* 1.最好情况 有序 O(n)
* 2.最坏情况 逆序 o(n^2)
* 3.平均情况 O(n^2)
*/
void insertion_sort(int a[],int n) {
int i, j;
for (i = ; i < n; i++) {
int temp = a[i]; /* 当前要插入的数 */
for (j = i - ; j >= ; j--) {
if (temp >= a[j])
break;
a[j + ] = a[j];
}
a[j + ] = temp;
}
}
/*
* 希尔排序(不稳定)
* 1.时间复杂度跟取得序列有关系
* 2.取 n/2,n/2^2,...,1序列时 最坏情况下时间复杂度为O(n^2)
* 3.sedgewick序列时 最坏: O(n^3/2) 平均:O(n^5/4)
*/
void shell_sort(int a[],int n) {
int i, j;
int sedgewick[] = { ,,,,,,, };
for (i = ; sedgewick[i] >= n; i++);
for(int d=sedgewick[i];d>;d=sedgewick[++i])
for (int p = d; p < n; p++) {
int temp = a[p];
for (j = p; j >= d&&a[j - d] > temp; j -= d)
a[j] = a[j - d];
a[j] = temp;
}
}
/*
* 选择排序(不稳定 应该是最差的排序算法了吧)
* 最好 最坏 平均 时间复杂度都是O(n^2)
*/
void selection_sort(int a[],int n) {
for (int i = ; i < n-; i++) {
int min = i;
for (int j = i + ; j < n; j++) {
if (a[j] < a[min]) {
min = j;
}
}
if (min != i) {
int temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
}
/*
* 归并排序(递归版本) 在外排序中使用较多
* 1.时间复杂度 最好 最坏 平均 都是O(n*log n).
* 2.空间复杂度 是 O(n).
*/
void merge1(int a[],int temp[],int left,int right,int rightEnd) {
int l = left;
int leftEnd = right -;
int r = right;
int index = rightEnd - left + ;
int x = left;
while (l <= leftEnd && r <= rightEnd) {
if (a[l] <= a[r]) {
temp[x++] = a[l++];
}
else
temp[x++] = a[r++];
}
while (l <= leftEnd)
temp[x++] = a[l++];
while (r <= rightEnd)
temp[x++] = a[r++];
for (int i=rightEnd; index > ; index--,i--)
a[i] = temp[i];
} void mSort1(int a[],int temp[],int left,int right) {
if (left < right) {
int mid = (left + right) / ;
mSort1(a, temp, left, mid);
mSort1(a, temp, mid + , right);
merge1(a, temp, left, mid + , right);
} }
void merge_sort1(int a[],int n) {
int* temp = (int*)malloc(sizeof(int)*n);
if (temp != NULL ) {
mSort1(a, temp, , n-);
free(temp);
}
else {
cout << "内存不足" << endl;
}
}
/*
* 归并排序循环版本
* 1. 时间复杂度 最好 最坏 平均 都为O(n * log n)
* 2. 空间复杂度O(n)
*/
void merge2(int a[], int temp[], int left, int right, int rightEnd) {
int l = left;
int leftEnd = right - ;
int r = right;
int index = rightEnd - left + ;
int x = left;
while (l <= leftEnd && r <= rightEnd) {
if (a[l] <= a[r]) {
temp[x++] = a[l++];
}
else
temp[x++] = a[r++];
}
while (l <= leftEnd)
temp[x++] = a[l++];
while (r <= rightEnd)
temp[x++] = a[r++];
} void mergePass(int a[],int temp[],int n,int length) {
int i;
for (i = ; i + * length <= n; i += * length) {
merge2(a, temp, i, i + length, i + * length - );
}
if (i + length < n) {
merge2(a, temp, i, i + length, n - );
}
else
for (int j = i; j < n; j++)
temp[j] = a[j];
}
void merge_sort2(int a[],int n) {
int* temp = (int*)malloc(n * sizeof(int));
if (temp != NULL) {
int length = ;
while (length < n) {
mergePass(a, temp, n, length);
length *= ;
mergePass(temp, a, n, length);
length *= ;
}
free(temp);
}
else {
cout << "内存不足" << endl;
}
}
/*
* 堆排序(不稳定)
* 最好 最坏 平均 时间复杂度 O(n*log n).
*/
void adjust(int a[],int i, int n) {
int parent, child;
int temp = a[i];
for (parent = i; parent * < n - ; parent = child) {
child = parent * + ; /* 先指向左孩子 */
if (child != n - && a[child+] > a[child]) {
child++; /* 右孩子较大则指向右孩子 */
}
if (temp >= a[child])
break;
else
a[parent] = a[child];
}
a[parent] = temp;
}
void heap_sort(int a[], int n) {
for (int i = (n-) / ; i >= ; i--)
adjust(a,i,n); /* 构建最大堆 */
for (int i = n - ; i > ; i--) {
int temp = a[i];
a[i] = a[];
a[] = temp;
adjust(a, , i);
}
}
/*
* 快速排序 (不稳定)
* 1.最坏情况和主元的选取有一定的关系 如果选首位为主元O(n^2)
* 2.最好 平均 O(n * log n)
* 3.当待排元素较少时 快排效率会急速下降 此时可采用插入排序
*/
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int median3(int a[],int left,int right) {
int center = (left + right) / ;
if (a[left] > a[center]) {
swap(&a[left], &a[center]);
}
if (a[left] > a[right]) {
swap(&a[left], &a[right]);
}
if (a[center] > a[right]) {
swap(&a[center], &a[right]);
}
swap(&a[center], &a[right-]); /* 将基准放在数组右端 */
return a[right-];
}
void qSort(int a[],int left,int right) {
int cutoff = ;
int low, high, pivot;
if (right - left >= cutoff) {
pivot = median3(a, left, right);
low = left;
high = right - ;
while () {
while (a[++low] < pivot);
while (a[--high] > pivot);
if (low < high)
swap(&a[low], &a[high]);
else
break;
}
swap(&a[low], &a[right - ]);
qSort(a, left, low - );
qSort(a, low + , right);
}
else
insertion_sort(a+left,right-left+);
} void quick_sort(int a[],int n) {
qSort(a,,n-);
}

算法/时间复杂度

10^3的随机整数

10^4个随机整数

10^5个随机整数

10^5个顺序整数

10^5个逆序整数

10^5个基本有序的整数

10^5个随机正整数,每个数不超过1000

冒泡排序

4ms

228ms

>10s

88ms

>10s

650ms

>10s

插入排序

3ms

35ms

4784ms

82ms

9206ms

115ms

4499ms

选择排序

5ms

332ms

>10s

>10s

>10s

>10s

>10s

归并排序(递归版本)

4ms

12ms

131ms

82ms

127ms

83ms

75ms

堆排序

3ms

10ms

103ms

89ms

102ms

126ms

94ms

希尔排序

3ms

25ms

128ms

119ms

125ms

117ms

116ms

归并排序(循环版本)

3ms

24ms

125ms

78ms

99ms

77ms

93ms

快速排序(pivot取中位数)

3ms

10ms

122ms

76ms

112ms

76ms

69ms

基数排序

 

PTA 5-12 排序 (25分)的更多相关文章

  1. PTA 09-排序1 排序 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/720 5-12 排序   (25分) 给定NN个(长整型范围内的)整数,要求输出从小到大 ...

  2. PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)

    PTA数据结构与算法题目集(中文)  7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分)   Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...

  3. PTA 树的同构 (25分)

    PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...

  4. PTA 7-1 还原二叉树 (25分)

    PTA 7-1 还原二叉树 (25分) 给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度. 输入格式: 输入首先给出正整数N(≤50),为树中结点总数.下面两行先后给出先序和中序遍历 ...

  5. PTA 07-图6 旅游规划 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/717 5-9 旅游规划   (25分) 有了一张自驾旅游路线图,你会知道城市间的高速公路 ...

  6. PTA 05-树8 File Transfer (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/670 5-8 File Transfer   (25分) We have a netwo ...

  7. PTA 03-树2 List Leaves (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves   (25分) Given a tree, you ...

  8. PTA - - 06-图1 列出连通集 (25分)

    06-图1 列出连通集   (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...

  9. PTA 旅游规划(25 分)

    7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...

随机推荐

  1. 安装ubuntu12.04LTS卡住以及花屏问题

    昨天在XP下用grub4dos安装了ubuntu12.04LTS,总体上还算比较顺利,中途有碰到两个异常问题,解决了记录一下. 问题一:安装过程中读取ISO镜像文件时,卡在"checking ...

  2. 记一个菜鸟在Linux上部署Tomcat的随笔

    以前都只是在园子里找各种资料.文档.各种抱大腿,今天是第一次进园子里来添砖加瓦,实话说,都不知道整些啥东西上来,就把自己在Linux上搭建Tomcat的过程记录下来,人笨,请各位大虾们勿喷. 虽然做开 ...

  3. [HDOJ - 5282] Senior's String 【DP】

    题目链接:BZOJ - 5282 题目分析 LCS 就是用经典的 O(n^2) DP 解决,f[i][j] 表示 x 串前 i 个字符与 y 串前 j 个字符的 LCS 长度. f[i][j] = m ...

  4. (重)POJ 3020Antenna Placement

    http://poj.org/problem?id=3020 呃...这个题不是很会,所以找了大神的博客做了参考,说得很详细 http://blog.csdn.net/lyy289065406/art ...

  5. HTTP Header 入门详解

    什么是HTTP Headers HTTP是"Hypertext Transfer Protocol"的所写,整个www都在使用这种协定,几乎你在流览器里看到的大部分内容都是通过ht ...

  6. ASP.NET MVC 入门2、项目的目录结构与核心的DLL

    我们新建一个ASP.NET MVC的Web Application后,默认的情况下,项目的目录结构如下: App_Data :这个目录跟我们一般的ASP.NET website是一样的,用于存放数据. ...

  7. ASP.NET在主题中添加CSS文件

    ASP.NET在主题中添加CSS文件 在ASP.NET中,可以使用CSS来控制页面上HTML元素和ASP.NET控件的皮肤.如果在主题文件夹中添加了CSS文件,则在页面应用主题时也会自动应用CSS. ...

  8. Qt中如何写一个model(自定义一个RowNode,我没有碰到过)

    在qt中,用到最多就是model/view的结构来表示数据层及表示层的关系.model用于给view提供数据.那如何来实现一个简单的树形model呢. 实现一个自己的model需要重载以下的方法: Q ...

  9. NuGet学习笔记(1)——初识NuGet及快速安装使用

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  10. char*,const char*和string的相互转换

    好久没写东西啦,发表学术文章一篇,hiahia~ 近日和小佳子编程时遇到很多转换问题,很麻烦,在网上查了很多资料. 为了以后查找方便,特此总结如下. 如果有不对的地方或者有更简单的方法,请指出~~ 1 ...