XOJ 1004.Sort Ver.2 From XMU

12 篇文章 0 订阅
6 篇文章 0 订阅

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;

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值