Basic Approach To Algorithm and Data Structure
Tags: problemsolving, algorithms, datastructures, software
Published: 2022-01-08 15:53:43

---
title: Basic Approach To Algorithm and Data Structure
published: true
description: Basic Approach to solving computerized problems. Algorithm for starters
tags: problemsolving, algorithms, datastructures, Software 
cover_image: https://cdn.hashnode.com/res/hashnode/image/upload/v1630853678953/ok4MoRDM8.png
---

<p>Understanding how to solve an algorithm problem becomes easier when we know the underlying factors to consider when approaching a problem. These basic factors are what this article helps to understand.</p> <p>First, Let’s start with what an algorithm is.</p> <p>What is an Algorithm?</p> <p>Algorithms are step-by-step instructions on how to solve a problem. It identifies what is to be done and(the instructions) and the order in which they should be done. It can be represented using pseudocode or flowchart.</p> <p>For example:</p> <p>The algorithm for making a cup of tea might look something like this:</p> <ul> <li><p>Fill the Electric kettle with water.</p> </li> <li><p>Bring to boil.</p> </li> <li><p>Pour water into a cup.</p> </li> <li><p>Put the teabag in the cup.</p> </li> <li><p>Steep for about 3 minutes.</p> </li> <li><p>Remove Tea Bag.</p> </li> </ul> <p>This can be eventually translated to computer instructions using programming languages</p> <p>Given a more definite example like finding the maximum value in the list of numbers say; 27, 31, 42, 26, 10, 44, 35, 19, 33, 14.</p> <p>Mere scanning through this set of numbers, you can immediately see the largest value but a computer can not scan-search as humans do. Even humans will not be able to come up with the answer when the data is many.</p> <p>A computer can only compare two things at a time, ie, the algorithm must be expressed in terms of binary comparison.</p> <p>So then, the linear approach a computer will take to look for the largest value might look something like this:</p> <ul> <li><p>Read first item and store value as max</p> </li> <li><p>Look at each other item in the list, If it is greater, then the value becomes the new max</p> </li> <li><p>After going through the entire list, the current max is the largest value on the list.</p> </li> </ul> <p>For a better understanding of how data structure come to play, let’s look at another example,</p> <p>eg, Determining whether a list contains a given value, say 33 in the previous list of numbers.</p> <p>Let come up with an algorithm to solve this, which might look something like this:</p> <ul> <li><p>Keep the given value as the target,</p> </li> <li><p>Look at each value in the list,</p> </li> <li><p>If one is equal to the target, then we have found the value and we can stop looking.</p> </li> <li><p>If we go through the entire list and have not found the target, then it is not on the list.</p> </li> </ul> <p>This seems effective right?, but if the list is very long, it can take the computer a very long time to look through the entire list(this is called execution time). How much execution time it will take is what is referred to as Algorithm Complexity</p> <p>Complexity is a way of expressing the number of steps or operations in an algorithm. It gives us an idea of how long it will take for an algorithm to execute.</p> <p>We naturally expect an algorithm to take longer as input increases, but how much longer?</p> <p>Complexity is therefore expressed as a function of the number of elements in the input data.</p> <p>So, when we analyze algorithms,</p> <p>We consider the number of operation that needs to be performed We also consider complexity in the worst case, so we can see the changes in operations when the input size increases For example;</p> <p>We could stop when we find the target in the example above, but what happens when we have to look through every item on the list? That means if the number of items in the list increases, then we have to do more comparison through the entire list in cases where the target is not there, say 30. This is the worst case of these algorithms.</p> <p>Well, what can we do better, What if the item on the list were ordered?</p> <p>Consider our previous example: 27, 31, 42, 26, 10, 44, 35, 19, 33, 14.</p> <p>In the ordered version, searching for 33 becomes faster</p> <p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1624319028164/7kZWJBCuo.gif" alt="1581341951198.gif" /></p> <p>With this structure of data, searching for an item that is not in the list, say 30, becomes even easier. The computer would not have to search through the entire list, we can just stop when the next comparison value is greater than the target we are looking for. It can stop looking immediately it gets to 31 because it is greater than our target 30.</p> <p>This leads us to the Data structure.</p> <p>A data structure is a data organization management and storage format that enables efficient access and modification. It provides a means to manage large amounts of data efficiently. It is the way of organizing data in memory such that it is easy to access.</p> <p>There are many ways to store data in software engineering. Some ways are significantly better than the other depending on the requirements, say less memory, faster access or ease of modification.</p> <p>The following are some of the available data structures:</p> <p>Array</p> <p>List</p> <p>LinkedList</p> <p>ArrayList</p> <p>HashTables</p> <p>Dictionary</p> <p>Generic Collections</p> <p>Stack</p> <p>Queue</p> <p>Tree</p> <p>Graphs</p> <p>This should now give you a clearer view of algorithms and data structure even if this is the first time you heard of it.</p> <p>In our subsequent article, we will look at some of these data structures, and also a better algorithm to look for our given value in our earlier example.</p> <p>We searched through the list using a Linear approach. A linear algorithm is one in which the number of operations increases linearly as the increase in the input size. We shall also look at a better and faster approach called the Binary Search Algorithm.</p>