How to be Better at Algorithms? | Examples for Beginners
Tags: algorithms, webdev, programming
Published: 2022-11-01 12:28:52

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3>What is a Computer Algorithm?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>An algorithm is a list of instructions that a computer can use to solve a certain problem. Algorithms are used in all fields of computing, and they are designed to solve problems in an efficient way.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The design of an algorithm depends on the complexity of the problem it needs to solve. For simple problems, a brute force might be viable. However, for more complex issues, more complicated algorithms are needed.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3>Computer Algorithms are Everywhere</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Algorithms are the backbone of all of our digital lives. They help us make decisions faster and more efficiently.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The examples of algorithms that are used in everyday life, such as Google search engine, Amazon's recommendation system and Netflix's movie recommendation system, so algorithms are everywhere!</p>
<!-- /wp:paragraph -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>Algorithm Efficiency and the Big O(n):</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The optimization process is also dependent on the type and complexity of the algorithm. In some cases, it can take days or weeks to optimize an algorithm while in others it can take only hours or minutes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The time and space variables are the most important when it comes to Big O(n). Algorithms are an important part of software engineering and computing resources. One of the components that is crucial for their performance is the big O(n) notation. The letter O(n) in this phrase qualifies how many operations are being performed by a particular algorithm, where a lower number means faster execution times. Also depend of how many (n)s we talking about. Big O focuses on the worst-case scenario.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The prominence of an operation is its efficiency in finding desired results. The size of an operation is its dependence on space to retrieve desired outcomes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The more input on your operation the slower it will become. There are a few different common types of 'Big O' notation which represent how input affects the efficiency of an operation. These include O(n), O(1) which is the most efficient algorithm, and O(log n) and more…</p>
<!-- /wp:paragraph -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center"} -->
<figure class="wp-block-image aligncenter"><img src="https://qph.cf2.quoracdn.net/main-qimg-0475c0d183bac2e1333f066f5371af09-pjlq" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>How Do Algorithms Work?</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Algorithms are used to sort lists instructions and find the best possible solution or optimal solution for a given problem. The sorting algorithms are the most common algorithms used in data processing. They help organize data points by keeping it sorted, so that it can be easily accessed.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The list of different methods for sorting algorithms is not complete without mentioning the linear sort, which is not really an algorithm but a technique that can be used with any of the other sorting methods.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Can be divided into simple and complex:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><li>Simple sorting algorithms are called "bubble sort" and "insertion sort."</li><li>Complex sorting algorithms include "quick sort," "merge sort," and "heap sort."</li></ul>
<!-- /wp:list -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>The Complexity of an Algorithm</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The algorithm can be measured in two ways:</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Time Complexity for an Algorithms</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A time complexity is the upper bound on the time taken by an algorithm to run. It is expressed in terms of a function, asymptotic notation, and a constant value. This may be written in the form of an equation, graph or table to show the function's dependence on the independent variable. A time complexity of O(n) means that the algorithm will complete in a series of logical steps correlated to n number.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Space Complexity for an Algorithms</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Space complexity is a measure of how much space is required to execute an algorithm. Space Complexity for algorithms can be measured in a number of ways, including: number of iterations, number of bits processed by each operation, or number of words processed for each iteration. The O(n) algorithm is linear in the input size. This means that it takes time correlated to the size of the input, up to some maximum amount of work.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>Multiple Techniques to Approach a Problem?</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>There are a variety of approaches to solve a problem, which can be classified using different criteria. Please note that this is just my opinion to classify the type of problem I'm trying to solve.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The most common types are:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"fontSize":"medium"} -->
<ol class="has-medium-font-size"><li><strong>Divide and Conquer: </strong>are a clever way to tackle a problem. First, subdivide the problem into smaller smaller subproblems, solve those smaller problems and combine all the solutions into the problem’s final solution.</li><li><strong>Brute force: </strong>involve trying all possible solutions until a satisfactory solution is found.</li><li><strong>Randomized:</strong> use a random number during the computation to find a solution to the problem.</li><li><strong>Greedy: </strong>looking for an efficient solution on smaller part, then expand this optimum solution to the rest of the parts</li><li><strong>Recursive: </strong>are used to tackle larger and more difficult versions of a problem by solving smaller, simpler variations.</li><li><strong>Backtracking: </strong>is computer programming technique that divide a problem into sub problems, then takes each to an attempted solution. If the desired solution is not reached it will work backwards by finding a path that moves it forward in the problem.</li><li><strong>Dynamic Programming:</strong> focuses on solving complex problems in a series of simpler problems that are solved only one time rather than re-computing for each single problem. After at least computing it for once then you need to store it somewhere to re call it when necessary</li><li><strong>Pointer Traversal or Pathfinding:</strong> when searching a graph or network, it's important to use a proven search algorithm. A pointer traversal is a type of search algorithm which can be used to find the shortest route from one node of the graph to another. For instance, if we want to find the shortest route from Node 1 to Node 2 using pointer traversal then we would start by looking for the nearest neighbor of Node 1 and then, if there is none, looking for the nearest neighbor of the node's parent</li><li><strong>Hash Table</strong>: Hash table algorithms are used for a variety of purposes, such as collision detection and pathfinding. Collision detection is when you want to ensure that two objects don't intersect with each other, while pathfinding is the process of calculating the shortest distance between two or more points.</li></ol>
<!-- /wp:list -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3>We Got a Problem</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>How to Solve it in Plain English?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The first thing to do is read the problem and make sure that you understand what the instructions are asking you to do.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next, identify possible values for all of the variables given in the problem, and try to come up with a logical solution for each variable.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Finally, try to write out an algorithm for, start with words not code, write what every programmer knows it’s called “Pseudo code“</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>What is a Pseudo Code?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When we think about programming, pseudo code is often seen. It is a descriptive words with your language used by programmers to design an algorithm or another system's functions.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This programming language can use the structural conventions of normal languages, yet it is meant for human reading and not machine reading.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>Algorithm Example</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here is an example of how we think when we face a problem and how we solving it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Of course this is just my opinion, but I hope you were able to take something from it. I’ll get this problem from <a target="_blank" rel="noreferrer noopener" href="https://leetcode.com/problems/group-anagrams/">leetcode</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This problem about Anagrams, An anagram is a word created by rearranging the letters of another word. The new word will always have all the original letters from the original word, but not in the same sequence.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Example 1</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Input:</strong> strs = ["eat","tea","tan","ate","nat","bat"]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Output:</strong> [["bat"],["nat","tan"],["ate","eat","tea"]]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You’ve read the description, seen the example and done your research on Google. All of this should prepare you in advance for what is requested and what to include in your code lines.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Sure thing you're still trying to figure out how to put this on code, but now that you have a sense for the solution, you can work on it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>My thoughts step-by-step procedure would be as follow:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>1. I could get one instance of each word from the input data.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>2. This one instance should make it the default one, and it should be sorted.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>3. Again, this default instance would be the key value that collects other instances with.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>4. This collection is an array that has these values coming from the sorted key value.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>5. If I couldn't find multiple instances then I will just return whatever instance I got.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now let's break it down even further by imagining that we write some lines of code. But this code is going to be a pseudo code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So we need a kind of table that can store a key-value pair. The key would be the default sorted instance of a word and the values would be an array of this multiple instance values within the same characters of same word. If we couldn't find any other instances of same word, then we put it in array alone.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So, </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>loop through the input array</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>sort each string to make it the default key that we talked about above</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Is the key exists in the hashTable then put the unsorted version into it’s values</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If not then create an array with the current value in the loop</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Source Code in Javascript:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>function groupAnagrams(strs) {

//creating the Hash Table

const hashTable = {}

  for (let i = 0; i &lt; strs.length; i++){ //Loop through the input list
  
  // Creating the default key by sorting out the value from each string
  
  // To use the Sort method in JS, you need to use it on an array.
  
  // By using the Split() method, you create an array from this current value that you standing on.
  
  // By using the join() method, you recreate the string from the group of characters you created with the split() method above.
  
    const defKey = strs&#91;i].split('').sort().join('')
  
    if (hashTable&#91;defKey]){ 

  //Checking if the hash table has this value if yes, push the current value which is in that case going to be  different instance from the sorted default instance.
  
    hashTable&#91;defKey].push(strs&#91;i])
  
    } else {
  
    // if not just initialize it with an array
  
    hashTable&#91;defKey] = &#91;strs&#91;i]]
  
    }
  
  }

//Another great method in JS to help you flat all arrays created in the values of hash Table into one array

return Object.values(hashTable)

};</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Source Code in Python:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def groupAnagrams(strs):
  hashTable = {}
  for s in strs:
    defKey = ''.join(sorted(s))
    if defKey in hashTable:
      hashTable&#91;defKey].append(s)
    else:
      hashTable&#91;defKey] = &#91;s]

  return hashTable.values()
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->