Home Backend Development PHP Tutorial The top ten programming algorithms help programmers on the road to becoming masters

The top ten programming algorithms help programmers on the road to becoming masters

Aug 08, 2016 am 09:27 AM
nbsp partition

Algorithm 1: Quick Sort AlgorithmQuick sort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. In the worst case, O(n2) comparisons are required, but this situation is uncommon. In fact, quicksort is often significantly faster than other Ο(n log n) algorithms because its inner loop can be implemented efficiently on most architectures. Quick sort uses the divide and conquer strategy to divide a sequence (list) into two sub-series (sub-lists). Algorithm steps: 1 Pick an element from the sequence, called "pivot", 2 Reorder the sequence, all elements smaller than the pivot value are placed in front of the pivot, and all elements smaller than the pivot value are placed in front of the pivot The larger one goes behind the base (the same number can go to either side). After this partition exits, the base is in the middle of the sequence. This is called a partition operation. 3 Recursively sort the subarray of elements smaller than the base value and the subarray of elements greater than the base value. The bottom case of recursion is when the size of the array is zero or one, that is, it has always been sorted. Although it keeps recursing, this algorithm will always exit because in each iteration, it will move at least one element to its final position. Algorithm 2: Heap sorting algorithmHeapsort refers to a sorting algorithm designed using the data structure of a heap. Stacking is a structure that approximates a complete binary tree and satisfies the properties of stacking: that is, the key value or index of a child node is always smaller (or larger) than its parent node. The average time complexity of heap sort is Ο(nlogn). Algorithm steps: Create a heap H[0..n-1]Exchange the head (maximum value) and the tail of the heap3. Reduce the size of the heap by 1 and call shift_down(0) , the purpose is to adjust the top data of the new array to the corresponding position4. Repeat step 2 until the size of the heap is 1Algorithm 3: Merge sortMerge sort (Merge sort, Taiwanese translation: merge sort) is an effective sorting algorithm based on merge operations. This algorithm is a very typical application using the divide and conquer method. Algorithm steps: 1. Apply for space so that its size is the sum of the two sorted sequences. This space is used to store the merged sequence2. Set two pointers, and the initial positions are the two already sorted sequences. The starting position of the sorting sequence3. Compare the elements pointed to by the two pointers, select the relatively small element and put it into the merge space, and move the pointer to the next position4. Repeat step 3 until a certain pointer reaches the sequence Tail 5. Copy all the remaining elements of the other sequence directly to the end of the merged sequence Algorithm 4: Binary search algorithm The binary search algorithm is a way to find a specific item in an ordered array Element search algorithm. The search process starts from the middle element of the array. If the middle element happens to be the element to be searched, the search process ends; if a specific element is greater than or less than the middle element, search in the half of the array that is greater than or less than the middle element. , and start the comparison from the middle element just like the beginning. If the array is empty at a certain step, it means it cannot be found. This search algorithm reduces the search range by half with each comparison. Half search reduces the search area by half each time, and the time complexity is Ο(logn). Algorithm 5: BFPRT (Linear Search Algorithm)The problem solved by the BFPRT algorithm is very classic, that is, selecting the k-th largest (k-th smallest) element from a sequence of n elements, through clever analysis , BFPRT can guarantee linear time complexity in the worst case. The idea of ​​this algorithm is similar to that of quick sort. Of course, in order to make the algorithm still achieve the time complexity of o(n) in the worst case, the five algorithm authors have done exquisite processing. Algorithm steps: 1. Divide n elements into groups of 5 and divide them into n/5 (upper bound) groups. 2. Get the median of each group and use any sorting method, such as insertion sort. 3. Recursively call the selection algorithm to find the median of all the medians in the previous step, set it to x, and in the case of an even number of medians, set it to select the smaller one in the middle. 4. Use x to divide the array, let the number less than or equal to x be k, and the number greater than x be n-k. 5. If i==k, return x; if iTermination condition: When n=1, the small element i is returned.

Algorithm 6: DFS (Depth-First Search)

Depth-First-Search is a type of search algorithm. It traverses the nodes of the tree along the depth of the tree, searching the branches of the tree as deep as possible. When all edges of node v have been explored, the search will backtrack to the starting node of the edge where node v was found. This process continues until all nodes reachable from the source node have been discovered. If there are still undiscovered nodes, select one of them as the source node and repeat the above process. The entire process is repeated until all nodes are visited. DFS is a blind search.

Depth-first search is a classic algorithm in graph theory. The depth-first search algorithm can be used to generate the corresponding topological sorting table of the target graph. The topological sorting table can be used to easily solve many related graph theory problems, such as the maximum path problem, etc. Heap data structures are generally used to assist in implementing the DFS algorithm.

Depth-first traversal graph algorithm steps:

1. Visit vertex v;

2. Starting from the unvisited adjacent points of v, perform depth-first traversal of the graph until there is a vertex in the graph that has a path connected to v. All have been visited;

3. If there are still vertices in the graph that have not been visited at this time, start from an unvisited vertex and perform depth-first traversal again until all vertices in the graph have been visited.

The above description may be relatively abstract. For example:

DFS, after visiting a certain starting vertex v in the graph, starts from v and visits any of its adjacent vertices w1; then starting from w1, it visits the adjacent vertex w1 but The vertex w2 that has not been visited yet; then start from w2 and perform similar visits,...and so on until it reaches the vertex u where all adjacent vertices have been visited.

Then, take a step back to the vertex that was just visited last time to see if there are any other adjacent vertices that have not been visited. If there is, visit this vertex, and then proceed from this vertex to perform a visit similar to the above; if not, go back one step and search. Repeat the above process until all vertices in the connected graph have been visited.

Algorithm 7: BFS (Breadth-First Search)

Breadth-First-Search is a graph search algorithm. Simply put, BFS starts from the root node and traverses the nodes of the tree (graph) along the width of the tree (graph). If all nodes are visited, the algorithm aborts. BFS is also a blind search. Queue data structures are generally used to assist in implementing the BFS algorithm.

Algorithm steps:

1. First put the root node into the queue.

2. Take the first node from the queue and check whether it is the target.

If the target is found, the search ends and the results are returned.

Otherwise, add all its direct child nodes that have not yet been checked to the queue.

3. If the queue is empty, it means that the entire picture has been checked - that is, there is no target to be searched for in the picture. End the search and return "Target not found".

4. Repeat step 2.

Algorithm 8: Dijkstra’s algorithm

Dijkstra’s algorithm was proposed by Dutch computer scientist Edsger Dijkstra. Dijkstra's algorithm uses breadth-first search to solve the single-source shortest path problem of non-negative weighted directed graphs. The algorithm finally obtains a shortest path tree. This algorithm is often used in routing algorithms or as a submodule of other graph algorithms.

The input of this algorithm contains a weighted directed graph G and a source vertex S in G. We use V to represent the set of all vertices in G. Every edge in a graph is an ordered pair of elements formed by two vertices. (u, v) means there is a path connecting from vertex u to v. We use E to represent the set of all edges in G, and the weight of the edge is defined by the weight function w: E → [0, ∞]. Therefore, w(u, v) is the non-negative weight from vertex u to vertex v. The weight of an edge can be thought of as the distance between two vertices. The weight of a path between any two points is the sum of the weights of all edges on the path. It is known that there are vertices s and t in V, Dijkstra's algorithm can find the lowest weight path (for example, the shortest path) from s to t. This algorithm can also find the shortest path from a vertex s to any other vertex in a graph. For directed graphs without negative weights, Dijkstra's algorithm is currently the fastest known single-source shortest path algorithm.

Algorithm steps:

1. Initial command S={V0},T={remaining vertices}, the distance value corresponding to the vertex in T

If there is , d(V0,Vi) is < ;v0,vi>The weight on the arc does not exist, d(V0,Vi) is ∞2. Select a vertex W whose distance value is the smallest from T and is not in S , add S3. Modify the distance values ​​of the remaining vertices in T: If W is added as an intermediate vertex, the distance value from V0 to Vi is shortened, then modify this distance value and repeat the above steps 2 and 3, Until all vertices are included in S, that is, W=ViAlgorithm 9: Dynamic programming algorithmDynamic programming is a method used in mathematics, computer science and economics to solve complex problems by decomposing the original problem into relatively simple sub-problems. Dynamic programming is often suitable for problems with overlapping subproblems and optimal substructure properties. Dynamic programming methods often take much less time than naive solutions. The basic idea behind dynamic programming is very simple. Roughly speaking, to solve a given problem, we need to solve its different parts (i.e., subproblems) and then combine the solutions to the subproblems to arrive at a solution to the original problem. Often many subproblems are very similar, so dynamic programming attempts to solve each subproblem only once, thereby reducing the amount of computation: once the solution to a given subproblem has been calculated, it is memorized and stored in case the same subproblem is needed next time When solving the problem, look up the table directly. This approach is particularly useful when the number of repeated subproblems grows exponentially with the size of the input. The most classic problem about dynamic programming is undoubtedly the knapsack problem. Algorithm steps: 1. Optimal substructure properties. If the optimal solution of a problem contains solutions to sub-problems that are also optimal, we say that the problem has optimal substructure properties (that is, it satisfies the optimization principle). The optimal substructure properties provide important clues for dynamic programming algorithms to solve problems. 2. Overlapping nature of sub-problems. The overlapping nature of subproblems means that when a recursive algorithm is used to solve a problem from top to bottom, the subproblems generated each time are not always new problems, and some subproblems will be repeatedly calculated multiple times. The dynamic programming algorithm takes advantage of the overlapping nature of this sub-problem. It only calculates each sub-problem once, and then saves its calculation results in a table. When it is necessary to calculate the calculated sub-problem again, it only needs to calculate it in the table. Simply review the results and gain greater efficiency. Algorithm 10: Naive Bayes classification algorithm The Naive Bayes classification algorithm is a simple probability classification algorithm based on Bayes’ theorem. The basis of Bayesian classification is probabilistic reasoning, which is how to complete reasoning and decision-making tasks when the existence of various conditions is uncertain and only the probability of their occurrence is known. Probabilistic reasoning is the counterpart to deterministic reasoning. The Naive Bayes classifier is based on the independence assumption, that is, it is assumed that each feature of the sample is not related to other features. The Naive Bayes classifier relies on an accurate natural probability model and can achieve very good classification results in the sample set of supervised learning. In many practical applications, Naive Bayes model parameter estimation uses maximum likelihood estimation methods. In other words, Naive Bayes models can work without using Bayesian probability or any Bayesian model. Thank you for following the websites blog!

The above has introduced the top ten programming algorithms to help programmers become masters, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles