蛮力写算法_蛮力算法解释
蠻力寫算法
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.
蠻力算法聽起來確實像是–解決問題的直接方法,該方法依賴于純粹的計算能力,并嘗試各種可能性而不是先進(jìn)的技術(shù)來提高效率。
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.
例如,假設(shè)您有一個帶有4位數(shù)字的小掛鎖,每個數(shù)字從0-9。 您忘記了密碼,但是您不想再購買一個掛鎖。 由于您不記得任何數(shù)字,因此必須使用蠻力方法來打開鎖。
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.
因此,您將所有數(shù)字都設(shè)置回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?
計算機(jī)科學(xué)中的經(jīng)典示例是旅行商問題(TSP)。 假設(shè)業(yè)務(wù)員需要訪問全國10個城市。 如何確定應(yīng)該訪問這些城市的順序,以使旅行的總距離最小化?
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.
蠻力的時間復(fù)雜度為O(m n ) ,有時寫為O(n * m) 。 因此,如果要使用蠻力在“ m”個字符字符串中搜索一個“ n”個字符字符串,則需要進(jìn)行n * m次嘗試。
有關(guān)算法的更多信息 (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.
在計算機(jī)科學(xué)中,算法只是解決特定問題的一組逐步步驟。 可以將算法設(shè)計為執(zhí)行計算,處理數(shù)據(jù)或執(zhí)行自動推理任務(wù)。
Here's how Wikipedia 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.算法是一種有效的方法,可以在有限的空間和時間范圍內(nèi)并以定義明確的形式語言表示,以計算函數(shù)。 從初始狀態(tài)和初始輸入(可能為空)開始,指令描述了一種計算,該計算在執(zhí)行時會經(jīng)過有限數(shù)量的定義明確的連續(xù)狀態(tài),最終產(chǎn)生“輸出”并終止于最終的結(jié)束狀態(tài)。 從一種狀態(tài)過渡到另一種狀態(tài)不一定是確定性的; 一些算法(稱為隨機(jī)算法)包含隨機(jī)輸入。There are certain requirements that an algorithm must abide by:
算法必須滿足某些要求:
Some common types of algorithms include:
一些常見的算法類型包括:
- sorting algorithms 排序算法
- search algorithms 搜索算法
- compression algorithms. 壓縮算法。
Classes of algorithms include
算法類別包括
- Graph 圖形
- Dynamic Programming 動態(tài)編程
- Sorting 排序
- Searching 正在搜尋
- Strings 弦樂
- Math 數(shù)學(xué)
- Computational Geometry 計算幾何
- Optimization 優(yōu)化
- Miscellaneous. 雜。
Although technically not a class of algorithms, Data Structures are often grouped with them.
盡管從技術(shù)上講不是一類算法,但是數(shù)據(jù)結(jié)構(gòu)經(jīng)常與它們組合在一起。
效率 (Efficiency)
Algorithms are most commonly judged by their efficiency and the amount of computing resources they require to complete their task.
最常根據(jù)算法的效率和完成任務(wù)所需的計算資源量來判斷算法。
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.
評估算法的常見方法是查看其時間復(fù)雜度。 這顯示了算法的運行時間如何隨著輸入大小的增長而增長。 由于當(dāng)今的算法必須在大數(shù)據(jù)輸入上運行,因此對于我們的算法而言,具有相當(dāng)快的運行時間至關(guān)重要。
排序算法 (Sorting Algorithms)
Sorting algorithms come in various flavors depending on your necessity. Some, very common and widely used are:
排序算法根據(jù)您的需要而有不同的風(fēng)格。 一些非常普遍和廣泛使用的是:
快速排序 (Quicksort)
There is no sorting discussion which can finish without quick sort. Here is the basic concept: Quick Sort
沒有排序討論,沒有快速排序就可以結(jié)束。 這是基本概念: 快速排序
合并排序 (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: Mergesort
排序算法依賴于如何將排序數(shù)組合并為一個排序數(shù)組的概念。 在此處了解更多信息: Mergesort
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的課程非常強(qiáng)調(diào)創(chuàng)建算法。 這是因為學(xué)習(xí)算法是練習(xí)編程技能的好方法。 面試官最常在開發(fā)人員工作面試中測試算法候選人。
有關(guān)JavaScript算法的書籍: (Books about algorithms in JavaScript:)
Data Structures in JavaScript
JavaScript中的數(shù)據(jù)結(jié)構(gòu)
- Free book which covers Data Structures in JavaScript 涵蓋JavaScript中數(shù)據(jù)結(jié)構(gòu)的免費書籍
GitBook
GitBook
Learning JavaScript Data Structures and Algorithms - Second Edition
學(xué)習(xí)JavaScript數(shù)據(jù)結(jié)構(gòu)和算法-第二版
- Covers object oriented programming, prototypal inheritance, sorting & searching algorithms, quicksort, mergesort, binary search trees and advanced algorithm concepts 涵蓋面向?qū)ο蟮木幊?#xff0c;原型繼承,排序和搜索算法,快速排序,合并排序,二進(jìn)制搜索樹和高級算法概念
Amazon
亞馬孫
- ISBN-13: 978-1785285493 ISBN-13:978-1785285493
Data Structures and Algorithms with JavaScript: Bringing classic computing approaches to the Web
JavaScript的數(shù)據(jù)結(jié)構(gòu)和算法:將經(jīng)典的計算方法引入Web
- Covers recursion, sorting and searching algorithms, linked lists and binary search trees. 涵蓋遞歸,排序和搜索算法,鏈表和二進(jìn)制搜索樹。
Amazon
亞馬孫
- ISBN-13: 978-1449364939 ISBN-13:978-1449364939
翻譯自: https://www.freecodecamp.org/news/brute-force-algorithms-explained/
蠻力寫算法
總結(jié)
以上是生活随笔為你收集整理的蛮力写算法_蛮力算法解释的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到了前男友什么预兆
- 下一篇: git push命令_Git Push命