c语言40分 后面3个全部超时 请问如何改良
查看原帖
c语言40分 后面3个全部超时 请问如何改良
971954
a1760084203楼主2023/4/14 21:33
#include <stdio.h>
#define MAX_STACK_SIZE 10000
int Partition(int *a, int left, int right)
{
    int pivot = a[left];
    while (left < right)
    {
        while (left < right && a[right] > pivot)
            right--;
        a[left] = a[right];
        while (left < right && a[left] <= pivot)
            left++;
        a[right] = a[left];
    }
    a[left] = pivot;
    return left;
}
void quick(int *a, int left, int right)
{
    int stack[MAX_STACK_SIZE], top = -1;
    stack[++top] = left;
    stack[++top] = right;
    while (top >= 0)
    {
        right = stack[top--];
        left = stack[top--];
        int pivot = Partition(a, left, right);
        if (pivot - 1 > left)
        {
            stack[++top] = left;
            stack[++top] = pivot - 1;
        }
        if (pivot + 1 < right)
        {
            stack[++top] = pivot + 1;
            stack[++top] = right;
        }
    }
}
int main()
{
    int N, a[100000];
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }
    quick(a, 0, N - 1);
    for (int i = 0; i < N; i++)
    {
        printf("%d ", a[i]);
    }
    return 0;
}
2023/4/14 21:33
加载中...