Skip to content

Commit 7ac792d

Browse files
author
Botao Xiao
committed
[Function add]
1. Add leetcode solutions. 2. Add conclusion about quick sort.
1 parent 262a8d2 commit 7ac792d

File tree

6 files changed

+114
-26
lines changed

6 files changed

+114
-26
lines changed

DataStructrue/Sort/QuickSort.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## QuickSort 快速排序
2+
快速排序较为稳定,时间复杂度为O(NLogN)。
3+
4+
### 实现逻辑
5+
1. 找到一个pivot(基准点),用来比较大小。
6+
2. 从左向右遍历,找到第一个大于pivot的元素。
7+
3. 从右向左遍历,找到第一个小于pivot的元素。
8+
4. 交换2和3中找到的两个元素。
9+
4. 交换pivot和index的位置,保证左边的数都小于pivot,右边的数都大于pivot。
10+
11+
### 实现代码
12+
```Java
13+
public void sortColors(int[] nums) {
14+
if(null == nums) return;
15+
quickSort(nums, 0, nums.length - 1);
16+
}
17+
private void quickSort(int[] nums, int low, int high){
18+
if(low > high) return;
19+
int i = partition(nums, low, high);
20+
quickSort(nums, low, i - 1); //递归调用
21+
quickSort(nums, i + 1, high);
22+
}
23+
private int partition(int[] nums, int low, int high){
24+
int i = low, j = high + 1;
25+
int cmp = nums[low];
26+
while(true){
27+
while(nums[++i] <= cmp) if(i == high) break; //找到第一个大于pivot的元素
28+
while(nums[--j] > cmp) if(j == low) break; //找到第一个小于pivot的元素
29+
if(i >= j) break;
30+
swap(nums, i, j); //交换这两个数字的位置。
31+
}
32+
swap(nums, low, j); //将pivot和index换位置,此时左边的的所有数都小于pivot,右边的数都大于pivot。
33+
return j; //返回当前的index位置。
34+
}
35+
private static void swap(int[] nums, int i, int j){
36+
int temp = nums[i];
37+
nums[i] = nums[j];
38+
nums[j] = temp;
39+
}
40+
```

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,12 @@
141141

142142
[71. Simplify Path](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/71.%20Simplify%20Path.md)
143143

144-
<<<<<<< HEAD
145144
[72. Edit Distance](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/72.%20Edit%20Distance.md)
146145

147146
[73. Set Matrix Zeroes](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/73.%20Set%20Matrix%20Zeroes.md)
148147

149148
[74. Search a 2D Matrix](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/74.%20Search%20a%202D%20Matrix.md)
150149

151-
=======
152-
>>>>>>> 5ddac4c8fb078caea256a647f91903353b2ab074
153150
## Algorithm(4th_Edition)
154151
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
155152
All java realization codes are placed in different packages.
@@ -158,7 +155,7 @@ All java realization codes are placed in different packages.
158155
* [Insertion](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/InsertionSort.md)
159156
* [Shell]()
160157
* [Merge](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/MergeSort.md)
161-
* [QuickSort]()
158+
* [QuickSort](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/QuickSort.md)
162159
* [Priority Queue]()
163160
* [BucketSort](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/BucketSort.md)
164161

leetcode/69. Sqrt(x).md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,4 @@ class Solution {
6565
}
6666
}
6767
}
68-
<<<<<<< HEAD
6968
```
70-
=======
71-
```
72-
>>>>>>> 5ddac4c8fb078caea256a647f91903353b2ab074

leetcode/70. Climbing Stairs.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,3 @@ class Solution {
8181
return dp[n];
8282
}
8383
}
84-
<<<<<<< HEAD
85-
```
86-
=======
87-
```
88-
>>>>>>> 5ddac4c8fb078caea256a647f91903353b2ab074

leetcode/71. Simplify Path.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
## 71. Simplify Path
22

33
### Question
4-
<<<<<<< HEAD
54
Given an absolute path for a file (Unix-style), simplify it.
65

76
```
8-
=======
9-
Given an absolute path for a file (Unix-style), simplify it.
10-
11-
>>>>>>> 5ddac4c8fb078caea256a647f91903353b2ab074
127
For example,
138
path = "/home/", => "/home"
149
path = "/a/./b/../../c/", => "/c"
1510
path = "/a/../../b/../c//.//", => "/c"
1611
path = "/a//b////c/d//././/..", => "/a/b/c"
1712
1813
In a UNIX-style file system, a period ('.') refers to the current directory, so it can be ignored in a simplified path. Additionally, a double period ("..") moves up a directory, so it cancels out whatever the last directory was. For more information, look here: https://en.wikipedia.org/wiki/Path_(computing)#Unix_style
19-
<<<<<<< HEAD
2014
```
21-
=======
2215

23-
>>>>>>> 5ddac4c8fb078caea256a647f91903353b2ab074
2416
Corner Cases:
2517
* Did you consider the case where path = "/../"?
2618
* In this case, you should return "/".
@@ -55,8 +47,4 @@ class Solution {
5547
return sb.toString();
5648
}
5749
}
58-
<<<<<<< HEAD
59-
```
60-
=======
6150
```
62-
>>>>>>> 5ddac4c8fb078caea256a647f91903353b2ab074

leetcode/75. Sort Colors.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
## 75. Sort Colors
2+
3+
### Question
4+
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
5+
6+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
7+
8+
Note: You are not suppose to use the library's sort function for this problem.
9+
10+
```
11+
Example:
12+
13+
Input: [2,0,2,1,1,0]
14+
Output: [0,0,1,1,2,2]
15+
```
16+
17+
Follow up:
18+
* A rather straight forward solution is a two-pass algorithm using counting sort.
19+
* First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
20+
* Could you come up with a one-pass algorithm using only constant space?
21+
22+
223
### Thinking:
324
* Method1:
425
* 排序算法
@@ -34,4 +55,55 @@ class Solution {
3455
nums[high] = temp;
3556
}
3657
}
37-
```
58+
```
59+
60+
### 二刷, 借助二刷的机会,尽量复习多种排序方法。
61+
1. 冒泡法
62+
```Java
63+
public void sortColors(int[] nums) {
64+
int len = nums.length;
65+
for(int i = 0; i < len - 1; i++){
66+
for(int j = i + 1; j < len; j++){
67+
//如果遍历到的位置的值小于外部遍历的值,就交换两个值
68+
if(nums[j] < nums[i]){
69+
int temp = nums[i];
70+
nums[i] = nums[j];
71+
nums[j] = temp;
72+
}
73+
}
74+
}
75+
}
76+
```
77+
78+
2. 快速排序
79+
```Java
80+
class Solution {
81+
public void sortColors(int[] nums) {
82+
if(null == nums) return;
83+
quickSort(nums, 0, nums.length - 1);
84+
}
85+
private void quickSort(int[] nums, int low, int high){
86+
if(low > high) return;
87+
int i = partition(nums, low, high);
88+
quickSort(nums, low, i - 1);
89+
quickSort(nums, i + 1, high);
90+
}
91+
private int partition(int[] nums, int low, int high){
92+
int i = low, j = high + 1;
93+
int cmp = nums[low];
94+
while(true){
95+
while(nums[++i] <= cmp) if(i == high) break;
96+
while(nums[--j] > cmp) if(j == low) break;
97+
if(i >= j) break;
98+
swap(nums, i, j);
99+
}
100+
swap(nums, low, j);
101+
return j;
102+
}
103+
private static void swap(int[] nums, int i, int j){
104+
int temp = nums[i];
105+
nums[i] = nums[j];
106+
nums[j] = temp;
107+
}
108+
}
109+
```

0 commit comments

Comments
 (0)