What are Algorithms
Algorithms are rules or instructions that are formulated in a finite, sequential order to solve problems and get the required result. They give the pseudo code for problems and can be implemented in several languages as they are not language specific.
Writing Algorithms:
Algorithms are generally written as an amalgamation of user understandable language and some common programming languages. They are commonly written down in steps however it is not always necessary to do so. There are no distinct rules to formulate algorithms but you need to keep the following points in mind.
● Figure out what exactly is the problem
● Determine where you need to start
● Determine where you need to stop
● Formulate the intermediate steps
● Review your steps
Example -:
If you have to formulate an algorithm to check if a student has passed the exam or not ,you have to follow the following steps.
•Step-1 : START
•Step-2 : Declare two variables X and Y
•Step-3 : Assign the marks obtained by the student in X
•Step-4 : Store the minimum passing marks in Y
•Step-5 : Check if X > Y or X = Y
If YES then the student has Passed. ELSE , return Fail.
•Step-6 : STOP
N.B. -: The steps of an algorithm can be manipulated according to your preference. You can also cut short many steps to make a single step as well.
Elements of a Good Algorithm:
❖ Steps should be finite, clear and understandable.
❖ Clear and precise description of Input & Output
❖Each and every step must have a defined output that depends only on the input in that step or the preceding steps.
❖ The Algorithm should be flexible
❖ Steps should make use of general programming fundamentals
Classes of Algorithm
1. Divide & Conquer: Divide the problems into sub-parts and solve them separately.
2. Dynamic Programming: Divides the problems into sub-parts, remembers the results of the sub-parts & applies it to similar ones.
3. Greedy Algorithm: Takes the easiest step while solving problems without worrying about the complexity of the future steps.
Tree Traversal Algorithm:
Trees in Python are non-linear data structures having a root & nodes. Tree traversal refers to visiting each node present in the tree exactly once in order to update or check them.
Types of tree traversal-:
➢ In-order Traversal: It refers to visiting the left nodes followed by the root and then the right nodes.
➢ Pre-order Traversal: It refers to visiting the root nodes followed by left nodes and then the right nodes.
➢ Post-order Traversal: It refers to visiting the left nodes followed by the right nodes and then the root.
Sorting Algorithm:
Sorting Algorithms used to sort data into some given order. Sorting algorithms can be classified into 5 type’s i.e.
1)Merge Sort
2)Bubble Sort
3)Insertion Sort
4)Selection Sort
5)Shell Sort
Merge Sort: — It follows the divide & conquer rule. The given list is first divided into smaller lists and compares adjacent lists and then, reorders them in the desired sequence.
Bubble Sort: — It is a comparison algorithm that first compares and then sorts adjacent elements if they are not in the specified order.
Insertion Sort :-Insertion sort picks one element of a given list at a time and places it at the exact spot where it is to be placed.
Selection Sort:-Selection sort algorithm first divides the given list into two halves where the first half is the sorted list and the second half is unsorted list. At first the sorted list is empty and all the elements to be sorted are present in the unsorted list. The selection sort algorithm looks at all the elements present in the unsorted list, picks up an item that is supposed to come first and then places it in the sorted list. It then repeats the searching process and places the next element to the right of the first element in the sorted list.
Shell Sort:-Shell Sort algorithm allows you to sort elements apart from each other. In shell sort, elements at a particular interval are sorted. The interval between the elements is slowly decreased based on the sequence used.
Searching Algorithm:
Searching Algorithms are used to search for or fetch elements present in some given dataset. There are many types of searching algorithms such as Linear search,Binary search, Exponential search, Inter polar search etc.
❏ Linear Search: Linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
❏ Binary Search: Binary search, also known as half-interval search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array.
❏ Exponential Search: An exponential search is an algorithm used for searching sorted, unbounded/infinite lists.Exponential search can also be used to search in bounded lists.
Algorithm Analysis
Algorithms can be analysed both before and after their implementation. These analyses are referred to as, i) Priori Analysis ii) Posteriori Analysis.
❖ Priori Analysis: A priori analysis is also known as theoretical analysis. Here the efficiency of analysis is measured by assuming all the factors are constant and do not affect the implementation of the algorithm.
❖ Posterior Analysis: In a Posteriori Analysis, algorithms after being implemented using some programming language are executed. The actual values like time complexity or execution time of an algorithm, space complexity or the space required by an algorithm in its full life cycle etc. are gathered.
Written By: Priyanka Pattanaik
Get Oretes Academy Certified in Trending Technologies: http://oretesacademy.com/
If you have any query drop a email at helpdesk@oretes.com