Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
RecYan
Data-Structure-and-Algorithm
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Data-Structure-and-Algorithm
/
src
/
Algorithm
/
QuickSort.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
63 lines (54 loc) · 1.64 KB
master
Breadcrumbs
Data-Structure-and-Algorithm
/
src
/
Algorithm
/
QuickSort.java
Copy path
Top
File metadata and controls
Code
Blame
63 lines (54 loc) · 1.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package
Algorithm
;
import
java
.
util
.
Arrays
;
/**
Created by Yan_Jiang
荷兰国旗改进经典快排
经典快排 --> 改进快排 --> 随机快排
时间复杂度O(N*logN)
*/
public
class
QuickSort
{
public
static
void
quickSort
(
int
[]
arr
) {
if
(
arr
==
null
||
arr
.
length
<
2
) {
return
;
}
quickSort
(
arr
,
0
,
arr
.
length
-
1
);
}
public
static
void
quickSort
(
int
[]
arr
,
int
l
,
int
r
) {
if
(
l
<
r
) {
//l + (int) (Math.random() * (r - l + 1)) --> 随机选取数组中的一个元素
swap
(
arr
,
l
+ (
int
) (
Math
.
random
() * (
r
-
l
+
1
)),
r
);
int
[]
p
=
partition
(
arr
,
l
,
r
);
quickSort
(
arr
,
l
,
p
[
0
] -
1
);
quickSort
(
arr
,
p
[
1
] +
1
,
r
);
}
}
/**
改进经典快排 --> 随机快排
随机选取数组中一个元素与数组中最后一个元素进行交换
选取该元素值作为比较对象
划分less区域,more区域{**区别:包括数组最后一个元素**}
*/
public
static
int
[]
partition
(
int
[]
arr
,
int
l
,
int
r
) {
int
less
=
l
-
1
;
//初始的小于区域
int
more
=
r
;
//包括数组最后一个元素 --> 初始化大于区域
/**
如果遍历下标 遇到比带比较数小的数 则将该数与小于区域的后一个交换
*/
while
(
l
<
more
) {
if
(
arr
[
l
] <
arr
[
r
]) {
swap
(
arr
, ++
less
,
l
++);
}
else
if
(
arr
[
l
] >
arr
[
r
]) {
swap
(
arr
, --
more
,
l
);
}
else
{
l
++;
}
}
swap
(
arr
,
more
,
r
);
//将数组最后一个元素与大于区域的第一个元素交换 还原数组
return
new
int
[] {
less
+
1
,
more
};
//返回等于区域数组边界元素
}
public
static
void
swap
(
int
[]
arr
,
int
i
,
int
j
) {
int
tmp
=
arr
[
i
];
arr
[
i
] =
arr
[
j
];
arr
[
j
] =
tmp
;
}
}
You can’t perform that action at this time.