Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

归并排序,JavaScript版的好像是不稳定的 #399

Closed
Colin3191 opened this issue May 20, 2021 · 3 comments
Closed

归并排序,JavaScript版的好像是不稳定的 #399

Colin3191 opened this issue May 20, 2021 · 3 comments

Comments

@Colin3191
Copy link

归并排序,JavaScript版的好像是不稳定的

@Colin3191
Copy link
Author

Colin3191 commented May 20, 2021

function merge(arr1, arr2) {
let arr = [];
while (arr1.length && arr2.length) {
if (arr1[0] < arr2[0]) {
arr.push(arr1.shift());
} else {
arr.push(arr2.shift());
}
}
return [...arr, ...arr1, ...arr2];

}
这里在arr1和arr2都只有一个元素且它们的值相等的情况下,返回的新数组中它们的顺序是交换了的。

@yanglbme
Copy link
Member

应该是 if (arr1[0] <= arr2[0])

@yanglbme
Copy link
Member

@Colin3191 已更正,谢谢反馈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants