Replies: 11 comments 6 replies
-
线性找第k大的数,可以把代码给一下吗 |
Beta Was this translation helpful? Give feedback.
-
犹豫再三,还是想说建议改善一下 朴素快排的代码
|
Beta Was this translation helpful? Give feedback.
-
补充一句,通过划分找第 k 大的数显然是不稳定的,不同于查找第一个第k大的数,这点需要注意 |
Beta Was this translation helpful? Give feedback.
-
那个快排代码为什么写成那样我也不是很清楚😂,可能是有些历史原因吧,应该是直接用了 这里的代码 …… 我个人认为您的建议很合理哈~ |
Beta Was this translation helpful? Give feedback.
-
线性找顺序统计量(也就是第k大)存在最坏O(n)的算法,在算法导论中有,不过意义不大,可以考虑加进来 |
Beta Was this translation helpful? Give feedback.
-
median-of-medians? |
Beta Was this translation helpful? Give feedback.
-
最后面好像有一个括号漏了 |
Beta Was this translation helpful? Give feedback.
-
三路快排是稳定的对吧,相同的元素相对顺序没有改变 |
Beta Was this translation helpful? Give feedback.
-
java递归快速排序: import java.util.*;
public class Main {
public static void main(String... args){
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] arr = new int[N];
for(int i = 0; i < N; i++){
arr[i] = in.nextInt();
}
quickSort(arr, 0, N - 1);
for(int element : arr){
System.out.print(element + " ");
}
}
public static void quickSort(int[] arr, int start, int end){
if(start < end) {
int pivot = partition(arr, start, end);
quickSort(arr, start, pivot - 1);
quickSort(arr, pivot + 1, end);
}
}
public static int partition(int[] arr, int start, int end){
int pivot = arr[start];
while (start < end) {
while (start < end && pivot <= arr[end]) --end;
arr[start] =arr[end];
while (start < end && arr[start] <= pivot) ++start;
arr[end] = arr[start];
}
arr[end] = pivot;
return start;
}
} |
Beta Was this translation helpful? Give feedback.
-
快速排序被卡成 |
Beta Was this translation helpful? Give feedback.
-
https://oi-wiki.org/basic/quick-sort/
OI Wiki 是一个编程竞赛知识整合站点,提供有趣又实用的编程竞赛知识以及其他有帮助的内容,帮助广大编程竞赛爱好者更快更深入地学习编程竞赛
Beta Was this translation helpful? Give feedback.
All reactions