XOJ 1004.Sort Ver.2 From XMU
2012-12-23 22:20
246 查看
Description
Give a set of numbers, output them after sort. You may use any algorithm you like to solve it.Input
Each input file contains only one case.
Each test case begins with an integer N(0<N<=1,000,000), the size of the set.
The Next line contains N numbers, represent the elements of the set. Each number range in [0..2147483647]
Output
Output the set in one line after sort.
Sample Input
4
4 15 8 5
Sample Output
4 5 8 15
Hint
O(n2) algorithm may get Time Limit Exceed. Try O(nlogn) to avoid TLE.
Source
xmu
Solution
#include<stdio.h> #include<stdlib.h> #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 //SqList typedef struct{ int *elem; int length; int listsize; }SqList; void InitSqList(SqList &L,int length){ L.elem=(int *)malloc((length+1)*sizeof(int)); if(!L.elem) exit(1); L.length=length; L.listsize=LIST_INIT_SIZE; } void HeapAdjust(SqList &L,int s,int m){ int rc,j; rc=L.elem[s]; for(j=2*s;j<=m;j*=2){ if(j<m&&(L.elem[j]<L.elem[j+1])) j++; if(rc>=L.elem[j]) break; L.elem[s]=L.elem[j]; s=j; } L.elem[s]=rc; } void HeapSort(SqList &L,SqList &OP){ int i; for(i=L.length/2;i>0;--i) HeapAdjust(L,i,L.length); for(i=L.length;i>0;--i){ OP.elem[i]=L.elem[1]; L.elem[1]=L.elem[i]; HeapAdjust(L,1,i-1); } } int main(){ int N,i; SqList L,OP; scanf("%d",&N); InitSqList(L,N); InitSqList(OP,N); for(i=1;i<=N;i++) scanf("%d",&L.elem[i]); HeapSort(L,OP); for(i=1;i<=N;i++) printf("%d ",OP.elem[i]); return 0; }
相关文章推荐
- hdu Problem-1004(map+sort)
- ACM解题报告 XOJ 1004
- (TOJ1506)Sort ZOJ7
- HDU 1425 sort (排序)
- JavaScript中sort()排序方法使用
- 对二维字符数组排序(2SORT和1QSORT)
- 15HD_OJ——sort
- STL模板之vector与sort的使用
- 在MongoDB中一起使用$or和sort()时,查询性能差的一种解决方案
- InsertSort
- coderforce--277SwapSort
- lucene 排序 (Sort SortField 构造函数)
- Internal Sorting: Cocktail-shaker sort: Sorting by Exchanging
- 选择排序(Select_Sort)
- BZOJ 1004([HNOI2008]Cards-Polya计数+k背包)
- 贪心/数学 2015百度之星资格赛 1004 放盘子
- JS V8引擎中 sort的源码
- 编程经验:List Sort by Reflection
- js中数组(Array)的排序(sort)注意事项
- Insertion or Heap Sort PAT甲级真题(堆排序)