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 }}
pointer-li
arithmetic-php
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
arithmetic-php
/
package
/
Sort
/
ShellSort.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
38 lines (35 loc) · 1.45 KB
develop
Breadcrumbs
arithmetic-php
/
package
/
Sort
/
ShellSort.php
Copy path
Top
File metadata and controls
Code
Blame
38 lines (35 loc) · 1.45 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
<?php
/**
* @example 希尔排序
* @author ShaoWei Pu <pushaowei0727@gmail.com>
* @date 2017/7/22
* @license Mozilla
* -------------------------------------------------------------
* 思路分析:希尔排序是基于插入排序的,区别在于插入排序是相邻的一个个比较(类似于希尔中h=1的情形),而希尔排序是距离h的比较和替换。
* -------------------------------------------------------------
* 希尔排序中一个常数因子n,原数组被分成各个小组,每个小组由h个元素组成,很可能会有多余的元素。
* 当然每次循环的时候,h也是递减的(h=h/n)。第一次循环就是从下标为h开始。希尔排序的一个思想就是,分成小组去排序。
*/
/**
* @param array $container
* @return array
*/
function
ShellSort
(
array
$
container
)
{
$
count
=
count
(
$
container
);
for
(
$
increment
=
intval
(
$
count
/
2
);
$
increment
>
0
;
$
increment
=
intval
(
$
increment
/
2
) ) {
for
(
$
i
=
$
increment
;
$
i
<
$
count
;
$
i
++ ) {
$
temp
=
$
container
[
$
i
];
for
(
$
j
=
$
i
;
$
j
>=
$
increment
;
$
j
-=
$
increment
) {
if
(
$
temp
<
$
container
[
$
j
-
$
increment
] ) {
$
container
[
$
j
] =
$
container
[
$
j
-
$
increment
];
}
else
{
break
;
}
}
$
container
[
$
j
] =
$
temp
;
}
}
return
$
container
;
}
var_dump
(
ShellSort
([
6
,
13
,
21
,
99
,
18
,
2
,
25
,
33
,
19
,
84
]));
You can’t perform that action at this time.