File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change
1
+ package newcode
2
+
3
+ import org.junit.Test
4
+ import java.util.*
5
+
6
+ // 使用优先级队列实现大小根堆
7
+ class Heap {
8
+ fun getMinHeap (): PriorityQueue <Int > {
9
+ return PriorityQueue <Int >()
10
+ }
11
+
12
+ fun getMaxHeap (): PriorityQueue <Int > {
13
+ return PriorityQueue <Int > { o1, o2 -> o2 - o1 }
14
+ }
15
+
16
+ @Test
17
+ fun test () {
18
+ val heap = getMaxHeap()
19
+ heap.add(3 )
20
+ heap.add(2 )
21
+ heap.add(5 )
22
+ heap.add(8 )
23
+ heap.add(234 )
24
+ heap.add(- 12 )
25
+ heap.add(6 )
26
+ while (heap.isNotEmpty()) {
27
+ val node = heap.poll()
28
+ println (node)
29
+ }
30
+ }
31
+ }
Original file line number Diff line number Diff line change 1
- 排序算法稳定性常见排序算法的稳定性 堆排序、快速排序、希尔排序、直接选择排序是不稳定的排序算法, 而冒泡排序、直接插入排序、折半插入排序、归并排序是稳定的排序算法。
1
+
2
+ | | 时间复杂度 | 空间复杂度 | 稳定性 |
3
+ | ------| ----------:| :-------:| -----|
4
+ | 选择排序 | O(N^2) | O(1) | ✖️ |
5
+ | 冒泡排序 | O(N^2) | O(1) | ☑️️ |
6
+ | 插入排序 | O(N^2) | O(1) | ☑️️ |
7
+ | 归并排序 | O(N* logN) | O(N) | ☑️️ |
8
+ | 快速排序 | O(N* logN | O(logN) | ✖️ |
9
+ | 堆排序 | O(N* logN | O(1) | ✖️ |
10
+
11
+ 默认的sort方法,基础类型用快排,自定类型用归并,稳定性考虑
You can’t perform that action at this time.
0 commit comments