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 }}
lianqiangjava
AlgorithmDataStructure
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmDataStructure
/
src
/
sort
/
SelectionSort.kt
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
48 lines (42 loc) · 1.59 KB
master
Breadcrumbs
AlgorithmDataStructure
/
src
/
sort
/
SelectionSort.kt
Copy path
Top
File metadata and controls
Code
Blame
48 lines (42 loc) · 1.59 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
package
sort
/*
选择排序
选择排序也是一种简单直观的排序算法。它的工作原理很容易理解:
初始时在序列中找到最小(大)元素,放到序列的起始位置作为已排序序列;
然后,再从剩余未排序元素中继续寻找最小(大)元素,放到已排序序列的末尾。
以此类推,直到所有元素均排序完毕。
注意选择排序与冒泡排序的区别:冒泡排序通过依次交换相邻两个顺序不合法的元素位置,
从而将当前最小(大)元素放到合适的位置;而选择排序每遍历一次都记住了当前最小(大)元素的位置,
最后仅需一次交换操作即可将其放到合适的位置。
*/
/*
分类:内部比较排序
数据结构:数组
最差时间复杂度:O(n^2)
最优时间复杂度:O(n^2)
平均时间复杂度:O(n^2)
所需辅助空间:O(1)
稳定性:不稳定
*/
fun
selectionSort
(
a
:
IntArray
,
n
:
Int
){
for
(i
in
0
until n
-
1
){
//
i为已排序序列的末尾
var
min
:
Int
=
i
for
(j
in
i
+
1
until n){
//
未排序序列
if
(a[j]
<
a[min]){
//
找出未排序序列中的最小值
min
=
j
}
}
if
(min
!=
i){
swap(a,min,i)
//
放到已排序序列末尾,该操作很可能把稳定性打乱,所以是不稳定算法
}
}
}
fun
main
(
args
:
Array
<
String
>) {
val
array
:
IntArray
=
intArrayOf(
3
,
5
,
1
,
8
,
6
,
6
,
9
,
8
,
7
,
6
,
2
)
val
n
:
Int
=
array.size
selectionSort(array,n)
println
(
"
选择排序结果:
"
)
for
(i
in
0
until n){
print
(array[i].toString()
+
"
,
"
)
}
}
You can’t perform that action at this time.