 # An Introduction to Algorithms to New Programmers

The data structure is undeniably the most elementary block of any programming language. For a programming enthusiast or perhaps someone aspiring to dive into the world of coding, sound knowledge of data structure is a must. Besides getting brush up with data structures, understanding the different algorithms and picking the right one is also a part of the game. In a nutshell, a programmer must be picky when choosing the suitable algorithm – binary search or linear search algorithm, to get the job done efficiently with complete accuracy.

## What is an Algorithm?

An algorithm is a step-by-step approach used to solve a problem by breaking it down into steps. Starting till the end, an algorithm follows a sequential step, and often the present step is the roadmap to what’s next.

Algorithms must have a well-defined input, be simple, and be easy to understand. Finiteness, clarity, and flexibility, to name a few, are the basic elements to consider when writing an algorithm.

## Top Considerations

Whether you are writing an algorithm for a complex project or merely to create a small application, you must keep the following points in mind:

Know your expectation of the algorithm and define the problem in writing.

### Know the starting point

Knowing where to start will help you in organizing the sequential steps.

### Know the ending point

Similar to step-2, it is also essential to know where you want the algorithm to end.

### Don’t miss the intermediate steps

Between the starting and the ending points, take special care between the lines.

Review, Refine, Revise the algorithm.

## Mostly used algorithms

Choosing the correct algorithm can be tricky with many existing algorithms, especially if you have little experience or perhaps just started. No worries. Here we have got the top three algorithms every programmer must know, and let’s understand each one of them in detail.

### Divide and Conquer Algorithm

This class of algorithms is one of the most used algorithms that involves breaking down the problem into parts. The idea behind dividing is to call the breakdown parts explicitly using recursive function until we get a desirable solution.

The three steps followed in this technique are:

#### Divide:

As a first step, we divide the problem into smaller parts.

#### Conquer:

With the help of the recursive function, we solve the sub-parts.

#### Combine:

At the final step, we merge the sub-problems to reach a final solution to the entire problem.

### Decrease and Conquer Algorithm

Similar to the divide and conquer algorithm, we have a decrease and conquer algorithm. The main idea behind this algorithmic approach is to reduce the original problem by partitioning a given problem into multiple sub-problems of smaller size.

The three steps followed in this technique are:

#### Decrease:

Reduce the original problem into smaller sub-problems and extend the solution.

#### Conquer:

We conquer the problem by solving smaller sub-problems in this step.

#### Extend:

At the final step, we extend the solution of smaller sub-problems to get the final solution of the original problem.

### Linear Search

Linear search is one of the popular searching algorithms used to find elements within a list. The algorithm searches and checks each value in the list until it finds the right match.

```def search(my_List, num):

i = 0

while i<len(my_List):

if my_List[i] == num:

return True

i +=1

return False

my_List = [1, 2, 3, 4, 5, 6,]

num = 3

if search(my_List, num):

print("Element Found: ", num)

else:

print("Element Found: ", num)```

### Binary Search

A binary search algorithm searches an element by comparing the middle part in the sequence. Define the lower bound and upper bound. If the searched value is smaller than the mid-value, change the upper bound, and the mid-value will be the upper bound. However, if the searched value is larger than the mid-value, change the lower bound, and the mid-value will be the lower bound.

Let us have a look at Python binary search using the following example:

```pos = -1

def search(my_List, num):

lb = 0

ub = 0

len(my_List)-1

while lb <= ub:

mid_value = (lb + ub) // 2

if my_List[mid_value] == num:

globals() ['pos'] = mid_value

return True

else:

if my_List[mid_value] < num:

lb = mid_value

else:

up = mid_value

my_List = [100, 200, 300, 400, 500, 600]

num = 400

if search(my_List, num):

print("Element Found at: ", pos+1 )

else:

### The Final Verdict

Algorithms are not language-oriented but logic-oriented. That means no matter which programming language you are comfortable with, logic works everywhere. For instance, an algorithm written in Python is applicable in other programming languages like C, C++, Java, Swift, etc. Just focus on the code without worrying about the programming language you choose.