代码:
#include<bits/stdc++.h>
using namespace std;
void quicksort(int arr[], int low, int high) {
if (high <= low)
return;
int i = low;
int j = high + 1;
int key = arr[low];
while (true) {
/*Find the number that bigger than key from left to right*/
while (arr[++i] < key) {
if (i == high) {
break;
}
}
/*Find the number that smaller than key from right to left*/
while (arr[--j] > key) {
if (j == low) {
break;
}
}
if (i >= j) {
break;
}
/*exchange the number of i&j*/
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*exchange the number*/
int temp = arr[low];
arr[low] = arr[j];
arr[j] = temp;
quicksort(arr, low, j - 1);
quicksort(arr, j + 1, high);
}
int main() {
int n;
int a[1000010];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
quicksort(a, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
输入:
5
4 5 3 1 2
输出:
1 2 3 4 5
输出正常,我通过了3个测试点(共5个)。其他两个测试点说我用了太多时间。我的代码仍然不好吗?如何升级?
答案 0 :(得分:0)
为枢轴选择中间值,从而消除了在内部循环中进行边界检查的需要。本示例使用两个函数,但是可以将它们组合为一个函数。逻辑是相同的:
const exampleMiddleware1 = async (ctx, next) => {
return await next();
console.log('Doing something');
};
const exampleMiddleware2 = async (ctx, next) => {
next();
console.log('Doing something');
};
const exampleMiddleware3 = async (ctx, next) => {
return next();
console.log('Doing something');
};