Skip to content

Commit 7ab28ce

Browse files
committed
update readme
1 parent abae230 commit 7ab28ce

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/main/kotlin/newcode/Heap.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

src/main/kotlin/sort/Readme.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
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方法,基础类型用快排,自定类型用归并,稳定性考虑

0 commit comments

Comments
 (0)