We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
https://www.javascriptc.com/interview-tips/zh_cn/javascript/javascript-array-sort/
题目描述:冒泡排序如何实现,时间复杂度是多少, 还可以如何改进
The text was updated successfully, but these errors were encountered:
冒泡排序:
function bubbleSort(arr) { for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { const temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } console.log(arr); } // 改进冒泡排序 function bubbleSort1(arr) { let i = arr.length - 1; while (i > 0) { let pos = 0; for (let j = 0; j < i; j++) { if (arr[j] > arr[j + 1]) { pos = j; const temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } i = pos; } console.log(arr); }
Sorry, something went wrong.
function bubbleSort2(arr) { let low = 0; let high = arr.length - 1; let temp, j; while (low < high) { // 正排找最大 for (j = low; j < high; ++j) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } --high; // 反排找最小 for (j = high; j > low; --j) { if (arr[j] < arr[j - 1]) { temp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = temp; } } ++low; } console.log(arr); }
👇~~~~ 欢迎在下方评论补充你的答案,一起来学习~:pushpin:
No branches or pull requests
https://www.javascriptc.com/interview-tips/zh_cn/javascript/javascript-array-sort/
题目描述:冒泡排序如何实现,时间复杂度是多少, 还可以如何改进
The text was updated successfully, but these errors were encountered: