求助大佬 自己写的快速排序,MLE和偶尔的TLE怎么破
查看原帖
求助大佬 自己写的快速排序,MLE和偶尔的TLE怎么破
639202
Abin_Plus楼主2022/1/26 12:09
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int fast(int *p,int m,int n)
{
    if(m<n)
    {
      int i,j,a=n,b=m,temp,counter;
      srand(time(0));
      i=rand()%n+m;
      do
      {
        counter=0;
        j=a;
        while(*(p+i)<=*(p+j)&&i<j)
        {
         j--;
        }
       if(i<j)
       {
         temp=*(p+i);
         *(p+i)=*(p+j);
         *(p+j)=temp;
         i=j;
         a=j;
         counter++;
       }
       j=b;
       while(*(p+i)>=*(p+j)&&i>j)
       {
         j++;
       }
       if(i>j )
       {
         temp=*(p+i);
         *(p+i)=*(p+j);
         *(p+j)=temp;
         i=j;
         b=j;
         counter++;
       }
     }while(counter>0);
    fast(p,m,i-1);
    fast(p,i+1,n);
    }
    return 0;
}
int main()
{
    int n,i=0,*p;
    scanf("%d",&n);
    getchar();
    p=(int *)malloc(n*sizeof(int));
    do
    {
        scanf("%d",p+i);
        i++;
    }while(getchar()==' ');
    fast(p,0,n-1);
    for(i=0;i<n-1;i++)
    {
        printf("%d ",*(p+i));
    }
    printf("%d\n",*(p+i));
    free(p);
    return 0;
}```
2022/1/26 12:09
加载中...