sql初学者指南_大O符号初学者指南

sql初学者指南

by Festus K. Yangani

由Festus K.Yangani

O标记法入门指南 (A Beginners Guide to Big O Notation)

Big O Notation is a way to represent how long an algorithm will take to execute. It enables a software Engineer to determine how efficient different approaches to solving a problem are.

大O表示法是表示算法执行所需时间的一种方式。 它使软件工程师能够确定解决问题的不同方法的效率如何。

Here are some common types of time complexities in Big O Notation.

这是大O表示法中一些常见的时间复杂度类型。

  • O(1) - Constant time complexity

    O(1)-恒定的时间复杂度
  • O(n) - Linear time complexity

    O(n)-线性时间复杂度
  • O(log n) - Logarithmic time complexity

    O(log n)-对数时间复杂度
  • O(n^2) - Quadratic time complexity

    O(n ^ 2)-二次时间复杂度

Hopefully by the end of this article you will be able to understand the basics of Big O Notation.

希望到本文结尾时,您将能够理解Big O符号的基础。

O(1)-恒定时间 (O(1) — Constant Time)

Constant time algorithms will always take same amount of time to be executed. The execution time of these algorithm is independent of the size of the input. A good example of O(1) time is accessing a value with an array index.

恒定时间算法将始终花费相同的时间来执行。 这些算法的执行时间与输入的大小无关。 O(1)时间的一个很好的例子是使用数组索引访问一个值。

var arr = [ 1,2,3,4,5];
arr[2]; // => 3

Other examples include: push() and pop() operations on an array.

其他示例包括:数组上的push()和pop()操作。

O(n)-线性时间复杂度 (O(n) - Linear time complexity)

An algorithm has a linear time complexity if the time to execute the algorithm is directly proportional to the input size n. Therefore the time it will take to run the algorithm will increase proportionately as the size of input n increases.

如果执行算法的时间与输入大小n成正比,则算法具有线性时间复杂度。 因此,随着输入n大小的增加,算法运行时间将成比例增加。

A good example is finding a CD in a stack of CDs or reading a book, where n is the number of pages.

一个很好的例子是在成堆的CD中查找CD或阅读一本书,其中n是页数。

Examples in of O(n) is using linear search:

O(n)中的示例正在使用线性搜索:

//if we used for loop to print out the values of the arrays
for (var i = 0; i < array.length; i++) {  console.log(array[i]);}
var arr1 = [orange, apple, banana, lemon]; //=> 4 steps
var arr2 = [apple, htc,samsung, sony, motorola]; //=> 5 steps
O(log n)-对数时间复杂度 (O(log n) - Logarithmic time complexity)

An algorithm has logarithmic time complexity if the time it takes to run the algorithm is proportional to the logarithm of the input size n. An example is binary search, which is often used to search data sets:

如果运行该算法所需的时间与输入大小n的对数成正比,则该算法具有对数时间复杂度。 一个示例是二进制搜索,它通常用于搜索数据集:

//Binary search implementationvar doSearch = function(array, targetValue) {    var minIndex = 0;    var maxIndex = array.length - 1;    var currentIndex;    var currentElement;        while (minIndex <= maxIndex) {        currentIndex = (minIndex + maxIndex) / 2 | 0;        currentElement = array[currentIndex];        if (currentElement < targetValue) {            minIndex = currentIndex + 1;        } else if (currentElement > targetValue) {            maxIndex = currentIndex - 1;        } else {            return currentIndex;        }    }    return -1;  //If the index of the element is not found.};
var numbers = [11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33];
doSearch(numbers, 23) //=>; 6

Other examples of logarithmic time complexity include:

对数时间复杂度的其他示例包括:

Example 1;
for (var i = 1; i < n; i = i * 2)  console.log(i);}
Example 2;
for (i = n; i >= 1; i = i/2) console.log(i);}
O(n ^ 2)-二次时间复杂度 (O(n^2) - Quadratic time complexity)

An algorithm has quadratic time complexity if the time to execution it is proportional to the square of the input size. A good example of this is checking to see whether there are any duplicates in a deck of cards.

如果算法的执行时间与输入大小的平方成正比,则它具有二次时间复杂度。 一个很好的例子是检查卡片组中是否有重复项。

You will encounter quadratic time complexity in algorithms involving nested iterations, such as nested for loops. In fact, the deeper nested loops will result in O(n^3), O(n^4), etc.

在涉及嵌套迭代(例如嵌套for循环)的算法中,您将遇到二次时间复杂性 实际上,更深层的嵌套循环将导致O(n ^ 3),O(n ^ 4)等。

for(var i = 0; i < length; i++) {     //has O(n) time complexity    for(var j = 0; j < length; j++) { //has O(n^2) time complexity      // More loops?    }}

Other examples of quadratic time complexity include bubble sort, selection sort, and insertion sort.

二次时间复杂度的其他示例包括冒泡排序,选择排序和插入排序。

This article only scratches the surface of Big O Notation. If you would like to understand more about Big O Notation, I recommend checking out the Big-O Cheat Sheet.

本文仅涉及Big O符号的表面。 如果您想了解更多有关Big O符号的信息,我建议您查阅Big O备忘单

翻译自: https://www.freecodecamp.org/news/my-first-foray-into-technology-c5b6e83fe8f1/

sql初学者指南

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值