多元线性回归解释_线性搜索解释

多元线性回归解释

Suppose you are given a list or an array of items. You are searching for a particular item. How do you do that?

假设给您一个列表或项目数组。 您正在搜索特定项目。 你是怎样做的?

Find the number 13 in the given list.

在给定列表中找到数字13。

You just look at the list and there it is!

您只要看一下清单就可以了!

Now, how do you tell a computer to find it?

现在,您如何告诉计算机找到它?

A computer cannot look at more than the value at a given instant of time. So it takes one item from the array and checks if it is the same as what you are looking for.

在给定的时间,计算机所看到的值不能超过该值。 因此,它从数组中取出一项,并检查它是否与您要查找的项相同。

The first item did not match. So move onto the next one.

第一项不匹配。 因此,移至下一个。

And so on…

等等…

This is done till a match is found or until all the items have been checked.

完成此操作,直到找到匹配项或检查了所有项目。

In this algorithm, you can stop when the item is found and then there is no need to look further.

在此算法中,您可以在找到项目后停止,然后无需进一步查找。

So how long would it take to do the linear search operation? In the best case, you could get lucky and the item you are looking at maybe at the first position in the array!

那么线性搜索操作需要多长时间? 在最好的情况下,您可能会很幸运,并且您正在寻找的物品可能在阵列的第一位置!

But in the worst case, you would have to look at each and every item before you find the item at the last place or before you realize that the item is not in the array.

但是在最坏的情况下,您必须先查看每个项目,然后才能找到该项目的最后一位,或者您意识到该项目不在数组中。

The complexity of linear search is therefore O(n).

因此,线性搜索的复杂度为O(n)。

If the element to be searched lived on the the first memory block then the complexity would be: O(1).

如果要搜索的元素位于第一个存储块上,则复杂度为:O(1)。

The code for a linear search function in JavaScript is shown below. This function returns the position of the item we are looking for in the array. If the item is not present in the array, the function will return null.

JavaScript中的线性搜索功能代码如下所示。 此函数返回我们要查找的项目在数组中的位置。 如果数组中没有该项,则该函数将返回null。

JavaScript范例 (Example in Javascript)

function linearSearch(arr, item) {
  // Go through all the elements of arr to look for item.
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === item) { // Found it!
      return i;
    }
  }
  
  // Item not found in the array.
  return null;
}

Ruby中的示例 (Example in Ruby)

def linear_search(target, array)
  counter = 0

  while counter < array.length
    if array[counter] == target
      return counter
    else
      counter += 1
    end
  end
  return nil
end

C ++中的示例 (Example in C++)

int linear_search(int arr[],int n,int num)
{
	for(int i=0;i<n;i++){
		if(arr[i]==num)
			return i;
   }
   // Item not found in the array
   return -1; 
}

Python范例 (Example in Python)

def linear_search(array, num):
	for i in range(len(array)):
		if (array[i]==num):
			return i
	return -1

What if you are searching the multiple occurrences of an element? For example you want to see how many 5’s are in an array.

如果要搜索一个元素的多次出现怎么办? 例如,您想查看数组中有5个数字。

Target = 5

目标= 5

Array = [ 1, 2, 3, 4, 5, 6, 5, 7, 8, 9, 5]

数组= [1、2、3、4、5、6、5、7、8、9、5]

This array has 3 occurrences of 5s and we want to return the indexes (where they are in the array) of all of them.

该数组有3次出现的5s,我们要返回所有索引的索引(它们在数组中的位置)。

This is called global linear search and you will need to adjust your code to return an array of the index points at which it finds your target element.

这称为全局线性搜索,您将需要调整代码以返回找到目标元素的索引点数组。

When you find an index element that matches your target, the index point (counter) will be added in the results array. If it doesn’t match, the code will continue to move on to the next element in the array by adding 1 to the counter.

当找到与目标匹配的索引元素时,索引点(计数器)将添加到结果数组中。 如果不匹配,则代码将通过向计数器加1继续移动到数组中的下一个元素。

def global_linear_search(target, array)
  counter = 0
  results = []

  while counter < array.length
    if array[counter] == target
      results << counter
      counter += 1
    else
      counter += 1
    end
  end

  if results.empty?
    return nil
  else
    return results
  end
end

为什么线性搜索效率不高 (Why linear search is not efficient)

There is no doubt that linear search is simple. But because it compares each element one by one, it is time consuming and therefore not very efficient. If we have to find a number from, say, 1,000,000 numbers and that number is at the last position, a linear search technique would become quite tedious.

毫无疑问,线性搜索很简单。 但是由于它会逐个比较每个元素,因此很耗时,因此效率不高。 如果必须从1,000,000个数字中找到一个数字,并且该数字位于最后一个位置,则线性搜索技术将变得非常乏味。

So you should also learn about bubble sort, quick sort and other more efficient algorithms.

因此,您还应该了解气泡排序,快速排序和其他更有效的算法。

其他搜索算法: (Other search algorithms:)

翻译自: https://www.freecodecamp.org/news/linear-search/

多元线性回归解释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值