arrays - 单循环排序的复杂性
问题描述
function sortArrays(arr)
{
// Sorting using a single loop
for (let j = 0; j < arr.length - 1; j++) {
// Checking the condition for two
// simultaneous elements of the array
if (arr[j] > arr[j + 1]) {
// Swapping the elements.
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
// updating the value of j = -1
// so after getting updated for j++
// in the loop it becomes 0 and
// the loop begins from the start.
j = -1;
}
}
return arr;
}
我对这部分感到困惑,这个循环的复杂性是什么。只有当条件为真时,循环才会被反转。所以我认为复杂性介于 O(n) 和 O'Log(n) 之间。
解决方案
推荐阅读
- angular - 观察反应角度形式的原始状态时的问题
- django - 显示可能不存在的对象的页面?
- reactjs - 如何从 Firebase 获取一份文档以及如何传递 id 来删除它?
- c++ - 在 C++ 中,使用参数包作为初始化列表是不好的做法吗?
- javascript - 仅模拟模块中的一项功能,但保留原始功能
- rest - Apache Camel Route 没有设置正文
- ios - 您可以自定义 SwiftUI 上下文菜单的激活突出显示颜色吗?
- xcode - 在 Xcode 上构建失败
- python - 如何在 django 中获取唯一值
- javascript - javascript中的 Date(d.getTime()) 和 new Date(d.getTime()) 有什么区别?