杭电 看归并排序和快速排序

shǎ崽 OrOrOrOrz

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3358    Accepted Submission(s): 1060
 
Problem Description
Acmer in HDU-ACM team are ambitious, especially shǎ崽, he can spend time in Internet bar doing problems overnight. So many girls want to meet and Orz him. But Orz him is not that easy.You must solve this problem first. The problem is : Give you a sequence of distinct integers, choose numbers as following : first choose the biggest, then smallest, then second biggest, second smallest etc. Until all the numbers was chosen . For example, give you 1 2 3 4 5, you should output 5 1 4 2 3
 
Input
There are multiple test cases, each case begins with one integer N(1 <= N <= 10000), following N distinct integers.
 
Output
Output a sequence of distinct integers described above.
 
Sample Input
5
1 2 3 4 5
 
Sample Output
5 1 4 2 3
 
Author
WhereIsHeroFrom
#include <stdio.h>

void sqrot(int *a,int l,int r)
{
    int i=l,j=r;
    int x=a[l];
    while(i<j)
    {
        while(i<j&&a[j]>=x)
            j--;
        if(i<j)
            a[i++]=a[j];
        while(i<j&&a[i]<=x)
            i++;
        if(i<j)
            a[j--]=a[i];
    }
    a[i]=x;
    if(l<r)
    {
    sqrot(a,l,i-1);
    sqrot(a,i+1,r);
    }
    
}

int a[10001];
int main()
{
    int n,j;

  while(scanf("%d",&n)!=EOF)
  {
    
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sqrot(a,0,n-1);

        for(j=n-1,i=0;(j-i)>=2;i++,j--)
            printf("%d %d ",a[j],a[i]);
        if(j-i==0)
            printf("%d\n",a[i]);
        if(j-i==1)
            printf("%d %d\n",a[j],a[i]);
    }

return 0;
}
#include<stdio.h>

void mergearry(int *a,int first,int mid,int last,int *temp){

    int i=first,m=mid,j=mid+1,n=last,k=0;
    while(i<=m&&j<=n){
        if(a[i]<=a[j])
            temp[k++]=a[i++];
        else
            temp[k++]=a[j++];
    }
    while(i<=m)
        temp[k++]=a[i++];
    while(j<=n)
        temp[k++]=a[j++];
    for(i=0;i<k;i++)
        a[first+i]=temp[i];

}




void mergesort(int *a,int first,int last,int *temp){
    if(first<last)                                    
    {int mid=(first+last)/2;
    mergesort(a,first,mid,temp);                      
    mergesort(a,mid+1,last,temp);
    mergearry(a,first,mid,last,temp);

    }
}




int main(){
        int n,j;
        while(scanf("%d",&n)!=EOF){
        int a[10001];
        int b[10001];

        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        mergesort(a,0,n-1,b);
        for(j=n-1,i=0;(j-i)>=2;i++,j--)
            printf("%d %d ",a[j],a[i]);
        if(j-i==0)
            printf("%d\n",a[i]);
        if(j-i==1)
            printf("%d %d\n",a[j],a[i]);

        }

}

 

posted @ 2014-02-25 13:30  漏网的鱼  阅读(314)  评论(0编辑  收藏  举报