蠻力寫算法
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 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.
算法是一種有效的方法,可以在有限的空間和時間范圍內并以定義明確的形式語言表示,以計算函數。 從初始狀態和初始輸入(可能為空)開始,指令描述了一種計算,該計算在執行時會經過有限數量的定義明確的連續狀態,最終產生“輸出”并終止于最終的結束狀態。 從一種狀態過渡到另一種狀態不一定是確定性的; 一些算法(稱為隨機算法)包含隨機輸入。
There are certain requirements that an algorithm must abide by:
算法必須滿足某些要求:
- Definiteness: Each step in the process is precisely stated. 確定性:過程中的每個步驟均已明確說明。
- Effective Computability: Each step in the process can be carried out by a computer. 有效的可計算性:該過程的每個步驟都可以由計算機執行。
- 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: Quick Sort
沒有排序討論,沒有快速排序就可以結束。 這是基本概念: 快速排序
合并排序 (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
排序算法依賴于如何將排序數組合并為一個排序數組的概念。 在此處了解更多信息: 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的課程非常強調創建算法。 這是因為學習算法是練習編程技能的好方法。 面試官最常在開發人員工作面試中測試算法候選人。
有關JavaScript算法的書籍: (Books about algorithms in JavaScript:)
Data Structures in JavaScript
JavaScript中的數據結構
- Free book which covers Data Structures in JavaScript 涵蓋JavaScript中數據結構的免費書籍
GitBook
GitBook
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 涵蓋面向對象的編程,原型繼承,排序和搜索算法,快速排序,合并排序,二進制搜索樹和高級算法概念
Amazon
亞馬孫
- 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. 涵蓋遞歸,排序和搜索算法,鏈表和二進制搜索樹。
Amazon
亞馬孫
- ISBN-13: 978-1449364939 ISBN-13:978-1449364939
翻譯自: https://www.freecodecamp.org/news/brute-force-algorithms-explained/
蠻力寫算法