Posts

Showing posts from January, 2019

Sorting Algorithms

Image
A Sorting Algorithm is used to rearrange a given array or list elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure. The most frequently used orders are numerical order and lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product). Efficient sorting is important for optimizing the efficiency of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists. Sorting is also often useful for canonicalizing data and for producing human-readable output. More formally, the output of any sorting algorithm must satisfy two conditions: The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); The output is a permutation (a reordering, yet retaining all of the original elements) of the input. Exa

Binary Search

Image
Binary search  is the most popular Search algorithm. It is efficient and also one of the most commonly used techniques that are used to solve problems. If all the names in the world are written down together in order and you want to search for the position of a specific name, binary search will accomplish this in a maximum of  35  iterations. Binary search works only on a sorted set of elements. To use binary search on a collection, the collection must first be sorted. When the binary search is used to perform operations on a sorted set, the number of iterations can always be reduced on the basis of the value that is being searched. Let us consider the following array: By using a linear search, the position of element 8 will be determined in the  9 t h  iteration. Let's see how the number of iterations can be reduced by using a binary search. Before we start the search, we need to know the start and end of the range. Let's call them  Low  and  High .

Linear Search

Image
Linear search is used on a collection of items. It relies on the technique of traversing a list from start to end by exploring properties of all the elements that are found on the way. For example, consider an array of integers of size  N . You should find and print the position of all the elements with value  x . Here, the linear search is based on the idea of matching each element from the beginning of the list to the end of the list with the integer  x , and then printing the position of the element if the condition is `True'. Implementation: for ( start to end of array ) { if ( current_element equals to 5 ) { print ( current_index ); } }