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 }}
microwind
algorithms
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithms
/
algorithmic-thinking
/
random-algorithm
/
shuffle
/
shuffle.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
51 lines (46 loc) · 1.63 KB
main
Breadcrumbs
algorithms
/
algorithmic-thinking
/
random-algorithm
/
shuffle
/
shuffle.js
Copy path
Top
File metadata and controls
Code
Blame
51 lines (46 loc) · 1.63 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
/*
* 洗牌算法(Shuffle)- JavaScript 版本
*
* 实现 Fisher–Yates 洗牌算法,保证每一种排列出现的概率相等。
* 这里提供两种形式:
* 1. 原地洗牌(in-place):直接修改原数组
* 2. 生成新数组的洗牌:返回一个被打乱的新数组,原数组不变
*
* 时间复杂度:O(n)
* 空间复杂度:O(1) 原地版本;O(n) 新数组版本
*/
/**
* 原地洗牌:直接修改传入的数组
* @param {any[]} arr - 待打乱的数组
* @returns {any[]} 同一个数组(已经被打乱)
*/
function shuffleInPlace(arr) {
// 从数组末尾开始,逐个与前面某个随机位置交换
for (let i = arr.length - 1; i > 0; i--) {
// 在 [0, i] 范围内生成一个随机整数 j
const j = Math.floor(Math.random() * (i + 1));
// 交换 arr[i] 与 arr[j]
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
/**
* 非原地洗牌:返回一个新数组,原数组不变
* @param {any[]} arr - 待打乱的数组
* @returns {any[]} 新数组(打乱后的结果)
*/
function shuffleNewArray(arr) {
// 先复制一份数组,再在副本上做原地洗牌
const copy = arr.slice();
return shuffleInPlace(copy);
}
// 简单演示
if (require.main === module) {
console.log("========== Shuffle (JavaScript) ==========");
const original = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log("原数组:", original);
console.log("原地洗牌结果:", shuffleInPlace([...original]));
console.log("新数组洗牌结果:", shuffleNewArray(original));
console.log("原数组保持不变:", original);
}
module.exports = { shuffleInPlace, shuffleNewArray };
You can’t perform that action at this time.