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 c7e49af

Browse filesBrowse files
committed
sync
1 parent 29d6a40 commit c7e49af
Copy full SHA for c7e49af

4 files changed

+122-2Lines changed: 122 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+84Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.laidu.learn.algorithm.sort;
2+
3+
/**
4+
* 快排
5+
*
6+
* @author tiancai.zang
7+
* on 2018-10-31 13:05.
8+
*/
9+
public class QuickSort {
10+
11+
public static void main(String[] args) {
12+
13+
int[] input = new int[]{1,2,3,43,12,7,34};
14+
15+
QuickSort quickSort = new QuickSort();
16+
17+
int[] sort = quickSort.sort(input, 0, input.length-1);
18+
19+
System.out.println(sort);
20+
21+
}
22+
23+
/**
24+
* @param array
25+
* @return
26+
*/
27+
public int[] sort(int[] array, int start, int end) {
28+
29+
if ((end - start) < 2 || Math.max(start,end) > array.length-1) {
30+
return array;
31+
}
32+
33+
int pivotIndex = (start + end) / 2;
34+
int pivot = array[pivotIndex];
35+
36+
int i = start;
37+
int j = end;
38+
39+
boolean iWait = false;
40+
boolean jWait = false;
41+
42+
while (i <= j) {
43+
if (array[i] > pivot) {
44+
if (jWait) {
45+
swap(array,i,j);
46+
jWait = false;
47+
}else {
48+
iWait = true;
49+
}
50+
} else {
51+
i++;
52+
}
53+
if (array[j] < pivot) {
54+
if (iWait) {
55+
swap(array,i,j);
56+
iWait = false;
57+
}else {
58+
jWait = true;
59+
}
60+
} else {
61+
j--;
62+
}
63+
}
64+
65+
if (array[Math.min(i,end)] <= pivot) {
66+
swap(array,pivotIndex,Math.min(i,end));
67+
}
68+
69+
sort(array, start, pivotIndex-1);
70+
sort(array, pivotIndex+1, end);
71+
72+
return array ;
73+
}
74+
75+
public void swap(int[] array, int firstIndex, int second){
76+
77+
if (firstIndex == second) {
78+
return;
79+
}
80+
int temp = array[firstIndex];
81+
array[firstIndex] = array[second];
82+
array[second] = temp;
83+
}
84+
}
Collapse file
+22Lines changed: 22 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 快排 算法学习
2+
3+
## 1 场景
4+
> 针对一组数据进行排序
5+
6+
## 2 原理
7+
### 2.1 算法
8+
>
9+
10+
### 2.2 基本步骤
11+
> 将数组S排序的基本算法由以下4步组成:
12+
* 1.如果数组S元素个数为0或者1,则返回
13+
* 2.数组S中任意元素为 __枢纽元(pivot)__ v
14+
* 3.将S中的其余元素划分两个不相交的集合: S1(所有元素均小于v) S2(所有元素均大于v)
15+
* 4.返回{quickSort(S1),v,quickSort(S2)}
16+
17+
## 3 复杂度
18+
### 3.1 时间复杂度
19+
### 3.2 空间复杂度
20+
21+
## 4 适用范围
22+
Collapse file
+4-2Lines changed: 4 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# 选择排序 算法学习
22

3-
## 1 原理
3+
## 1 场景
4+
5+
## 2 原理
46

5-
## 2 实现
67
## 3 复杂度
78
### 3.1 时间复杂度
89
### 3.2 空间复杂度
10+
911
## 4 适用范围
1012

Collapse file
+12Lines changed: 12 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 选择排序 算法学习
2+
3+
## 1 场景
4+
5+
## 2 原理
6+
7+
## 3 复杂度
8+
### 3.1 时间复杂度
9+
### 3.2 空间复杂度
10+
11+
## 4 适用范围
12+

0 commit comments

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