Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1936cff

Browse filesBrowse files
committed
auto commit
1 parent 9057fb0 commit 1936cff
Copy full SHA for 1936cff

File tree

Expand file treeCollapse file tree

1 file changed

+40
-40
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+40
-40
lines changed

‎notes/算法.md

Copy file name to clipboardExpand all lines: notes/算法.md
+40-40Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -630,18 +630,18 @@ public class MergeSort {
630630
### 2. 自顶向下归并排序
631631

632632
```java
633-
public static void sort(Comparable[] a) {
634-
aux = new Comparable[a.length];
635-
sort(a, 0, a.length - 1);
636-
}
633+
public static void sort(Comparable[] a) {
634+
aux = new Comparable[a.length];
635+
sort(a, 0, a.length - 1);
636+
}
637637

638-
private static void sort(Comparable[] a, int lo, int hi) {
639-
if (hi <= lo) return;
640-
int mid = lo + (hi - lo) / 2;
641-
sort(a, lo, mid);
642-
sort(a, mid + 1, hi);
643-
merge(a, lo, mid, hi);
644-
}
638+
private static void sort(Comparable[] a, int lo, int hi) {
639+
if (hi <= lo) return;
640+
int mid = lo + (hi - lo) / 2;
641+
sort(a, lo, mid);
642+
sort(a, mid + 1, hi);
643+
merge(a, lo, mid, hi);
644+
}
645645
```
646646

647647
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6468a541-3a9a-4008-82b6-03a0fe941d2a.png)
@@ -659,15 +659,15 @@ public class MergeSort {
659659
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c7b9b4c8-83d1-4eb0-8408-ea6576a9ed90.png)
660660

661661
```java
662-
public static void busort(Comparable[] a) {
663-
int N = a.length;
664-
aux = new Comparable[N];
665-
for (int sz = 1; sz < N; sz += sz) {
666-
for (int lo = 0; lo < N - sz; lo += sz + sz) {
667-
merge(a, lo, lo + sz - 1, Math.min(lo + sz + sz - 1, N - 1));
668-
}
662+
public static void busort(Comparable[] a) {
663+
int N = a.length;
664+
aux = new Comparable[N];
665+
for (int sz = 1; sz < N; sz += sz) {
666+
for (int lo = 0; lo < N - sz; lo += sz + sz) {
667+
merge(a, lo, lo + sz - 1, Math.min(lo + sz + sz - 1, N - 1));
669668
}
670669
}
670+
}
671671
```
672672

673673
## 快速排序
@@ -701,18 +701,18 @@ public class QuickSort {
701701
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e198c201-f386-4491-8ad6-f7e433bf992d.png)
702702

703703
```java
704-
private static int partition(Comparable[] a, int lo, int hi) {
705-
int i = lo, j = hi + 1;
706-
Comparable v = a[lo];
707-
while (true) {
708-
while (less(a[++i], v)) if (i == hi) break;
709-
while (less(v, a[--j])) if (j == lo) break;
710-
if (i >= j) break;
711-
exch(a, i, j);
712-
}
713-
exch(a, lo, j);
714-
return j;
715-
}
704+
private static int partition(Comparable[] a, int lo, int hi) {
705+
int i = lo, j = hi + 1;
706+
Comparable v = a[lo];
707+
while (true) {
708+
while (less(a[++i], v)) if (i == hi) break;
709+
while (less(v, a[--j])) if (j == lo) break;
710+
if (i >= j) break;
711+
exch(a, i, j);
712+
}
713+
exch(a, lo, j);
714+
return j;
715+
}
716716
```
717717

718718
### 3. 性能分析
@@ -904,16 +904,16 @@ Java ϵͳ
904904
该算法是线性级别的,因为每次正好将数组二分,那么比较的总次数为 (N+N/2+N/4+..),直到找到第 k 个元素,这个和显然小于 2N。
905905

906906
```java
907-
public static Comparable select(Comparable[] a, int k) {
908-
int lo = 0, hi = a.length - 1;
909-
while (hi > lo) {
910-
int j = partion(a, lo, hi);
911-
if (j == k) return a[k];
912-
else if (j > k) hi = j - 1;
913-
else lo = j + 1;
914-
}
915-
return a[k];
916-
}
907+
public static Comparable select(Comparable[] a, int k) {
908+
int lo = 0, hi = a.length - 1;
909+
while (hi > lo) {
910+
int j = partion(a, lo, hi);
911+
if (j == k) return a[k];
912+
else if (j > k) hi = j - 1;
913+
else lo = j + 1;
914+
}
915+
return a[k];
916+
}
917917
```
918918

919919
# 第三章 查找

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.