您的位置:首页 > 编程语言 > Go语言

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息