博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
蛮力写算法_蛮力算法解释
阅读量:2519 次
发布时间:2019-05-11

本文共 5911 字,大约阅读时间需要 19 分钟。

蛮力写算法

Brute Force Algorithms are exactly what they sound like – straightforward methods of solving a problem that rely on sheer computing power and trying every possibility rather than advanced techniques to improve efficiency.

蛮力算法听起来确实像是–解决问题的直接方法,该方法依赖于纯粹的计算能力,并尝试各种可能性而不是先进的技术来提高效率。

For example, imagine you have a small padlock with 4 digits, each from 0-9. You forgot your combination, but you don't want to buy another padlock. Since you can't remember any of the digits, you have to use a brute force method to open the lock.

例如,假设您有一个带有4位数字的小挂锁,每个数字从0-9。 您忘记了密码,但是您不想再购买一个挂锁。 由于您不记得任何数字,因此必须使用蛮力方法来打开锁。

So you set all the numbers back to 0 and try them one by one: 0001, 0002, 0003, and so on until it opens. In the worst case scenario, it would take 104, or 10,000 tries to find your combination.

因此,您将所有数字都设置回0,然后一一尝试:0001、0002、0003,依此类推,直到打开为止。 在最坏的情况下,将需要10 4或10,000次尝试来找到您的组合。

A classic example in computer science is the traveling salesman problem (TSP). Suppose a salesman needs to visit 10 cities across the country. How does one determine the order in which those cities should be visited such that the total distance traveled is minimized?

计算机科学中的经典示例是旅行商问题(TSP)。 假设业务员需要访问全国10个城市。 如何确定应该访问这些城市的顺序,以使旅行的总距离最小化?

The brute force solution is simply to calculate the total distance for every possible route and then select the shortest one. This is not particularly efficient because it is possible to eliminate many possible routes through clever algorithms.

蛮力解决方案仅是计算每种可能路线的总距离,然后选择最短的路线。 这不是特别有效,因为可以通过巧妙的算法消除许多可能的路线。

The time complexity of brute force is O(mn), which is sometimes written as O(n*m) . So, if we were to search for a string of "n" characters in a string of "m" characters using brute force, it would take us n * m tries.

蛮力的时间复杂度为O(m n ) ,有时写为O(n * m) 。 因此,如果要使用蛮力在“ m”个字符字符串中搜索一个“ n”个字符字符串,则需要进行n * m次尝试。

有关算法的更多信息 (More information about algorithms)

In computer science, an algorithm is simply a set of step by step procedure to solve a given problem. Algorithms can be designed to perform calculations, process data, or perform automated reasoning tasks.

在计算机科学中,算法只是解决特定问题的一组逐步步骤。 可以将算法设计为执行计算,处理数据或执行自动推理任务。

Here's how defines them:

如何定义它们:

An algorithm is an effective method that can be expressed within a finite amount of space and time and in a well-defined formal language for calculating a function. Starting from an initial state and initial input (perhaps empty), the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing “output” and terminating at a final ending state. The transition from one state to the next is not necessarily deterministic; some algorithms, known as randomized algorithms, incorporate random input.
算法是一种有效的方法,可以在有限的空间和时间范围内并以定义明确的形式语言表示,以计算函数。 从初始状态和初始输入(可能为空)开始,指令描述了一种计算,该计算在执行时会经过有限数量的定义明确的连续状态,最终产生“输出”并终止于最终的结束状态。 从一种状态过渡到另一种状态不一定是确定性的; 一些算法(称为随机算法)包含随机输入。

There are certain requirements that an algorithm must abide by:

算法必须满足某些要求:

  1. Definiteness: Each step in the process is precisely stated.

    确定性:过程中的每个步骤均已明确说明。
  2. Effective Computability: Each step in the process can be carried out by a computer.

    有效的可计算性:该过程的每个步骤都可以由计算机执行。
  3. Finiteness: The program will eventually successfully terminate.

    有限:程序最终将成功终止。

Some common types of algorithms include:

一些常见的算法类型包括:

  • sorting algorithms

    排序算法
  • search algorithms

    搜索算法
  • compression algorithms.

    压缩算法。

Classes of algorithms include

算法类别包括

  • Graph

    图形
  • Dynamic Programming

    动态编程
  • Sorting

    排序
  • Searching

    正在搜寻
  • Strings

    弦乐
  • Math

    数学
  • Computational Geometry

    计算几何
  • Optimization

    优化
  • Miscellaneous.

    杂。

Although technically not a class of algorithms, Data Structures are often grouped with them.

尽管从技术上讲不是一类算法,但是数据结构经常与它们组合在一起。

效率 (Efficiency)

Algorithms are most commonly judged by their efficiency and the amount of computing resources they require to complete their task.

最常根据算法的效率和完成任务所需的计算资源量来判断算法。

A common way to evaluate an algorithm is to look at its time complexity. This shows how the running time of the algorithm grows as the input size grows. Since the algorithms today have to operate on large data inputs, it is essential for our algorithms to have a reasonably fast running time.

评估算法的常见方法是查看其时间复杂度。 这显示了算法的运行时间如何随着输入大小的增长而增长。 由于当今的算法必须在大数据输入上运行,因此对于我们的算法而言,具有相当快的运行时间至关重要。

排序算法 (Sorting Algorithms)

Sorting algorithms come in various flavors depending on your necessity. Some, very common and widely used are:

排序算法根据您的需要而有不同的风格。 一些非常普遍和广泛使用的是:

快速排序 (Quicksort)

There is no sorting discussion which can finish without quick sort. Here is the basic concept:

没有排序讨论,没有快速排序就可以结束。 这是基本概念:

合并排序 (Mergesort)

A sorting algorithm which relies on the concept how to sorted arrays are merged to give one sorted arrays. Read more about it here:

排序算法依赖于如何将排序数组合并为一个排序数组的概念。 在此处了解更多信息:

freeCodeCamp’s curriculum heavily emphasizes creating algorithms. This is because learning algorithms is a good way to practice programming skills. Interviewers most commonly test candidates on algorithms during developer job interviews.

freeCodeCamp的课程非常强调创建算法。 这是因为学习算法是练习编程技能的好方法。 面试官最常在开发人员工作面试中测试算法候选人。

有关JavaScript算法的书籍: (Books about algorithms in JavaScript:)

Data Structures in JavaScript

JavaScript中的数据结构

  • Free book which covers Data Structures in JavaScript

    涵盖JavaScript中数据结构的免费书籍

Learning JavaScript Data Structures and Algorithms - Second Edition

学习JavaScript数据结构和算法-第二版

  • Covers object oriented programming, prototypal inheritance, sorting & searching algorithms, quicksort, mergesort, binary search trees and advanced algorithm concepts

    涵盖面向对象的编程,原型继承,排序和搜索算法,快速排序,合并排序,二进制搜索树和高级算法概念
  • ISBN-13: 978-1785285493

    ISBN-13:978-1785285493

Data Structures and Algorithms with JavaScript: Bringing classic computing approaches to the Web

JavaScript的数据结构和算法:将经典的计算方法引入Web

  • Covers recursion, sorting and searching algorithms, linked lists and binary search trees.

    涵盖递归,排序和搜索算法,链表和二进制搜索树。
  • ISBN-13: 978-1449364939

    ISBN-13:978-1449364939

翻译自:

蛮力写算法

转载地址:http://jqrwd.baihongyu.com/

你可能感兴趣的文章
如何在网页端启动WinForm 程序
查看>>
[转载] Java并发编程:Lock
查看>>
MySQL之索引
查看>>
JAVA设计模式之单例模式
查看>>
优秀博客
查看>>
词法分析程序
查看>>
Java反射
查看>>
[ACM_模拟][ACM_数学] LA 2995 Image Is Everything [由6个视图计算立方体最大体积]
查看>>
1040 有几个PAT
查看>>
BZOJ 1412 [ZJOI2009]狼和羊的故事 | 网络流
查看>>
原型模式
查看>>
Hadoop RPC源码阅读-交互协议
查看>>
WASAPI、DirectSound/DS、WaveOut、Kernel Streaming/KS
查看>>
Perl按行分割文件
查看>>
根据现有表操作基于active record的model
查看>>
NotMapped属性特性
查看>>
Count and Say
查看>>
GridView数据导入Excel/Excel数据读入GridView
查看>>
566. Reshape the Matrix
查看>>
python数据结构与算法之搜索
查看>>