E N D
Presentationon 4.1.2 Algorithms
What is algorithm? • Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. • An algorithm can be implemented using a programming language.
Categories of algorithm From the data structure point of view, following are some important categories of algorithms: • Search − Algorithm to search an item in a data structure. • Sort − Algorithm to sort items in a certain order. • Insert − Algorithm to insert item in a data structure. • Update − Algorithm to update an existing item in a data structure. • Delete − Algorithm to delete an existing item from a data structure.
Categories of algorithm From the data structure point of view, following are some important categories of algorithms: • Search − Algorithm to search an item in a data structure. • Sort − Algorithm to sort items in a certain order. • Insert − Algorithm to insert item in a data structure. • Update − Algorithm to update an existing item in a data structure. • Delete − Algorithm to delete an existing item from a data structure.
Linear Search Algorithm • Linear search is a very simple search algorithm. • In this type of search, a sequential search is made over all items one by one. • Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.
Linear Search Algorithm (contd.) Pseudocode FUNCTION linear_search(list, n, value) FOR Index ← 1 TO n IF list[Index] == value THEN return Index END IF EXIT FOR return(-1) END FUNCTION
Binary Search Algorithm • Binary search is a fast search algorithm. • This search algorithm works on the principle of divide and conquer. • For this algorithm to work properly, the data collection should be in the sorted form. • Binary search looks for a particular item by comparing the middle item of the collection. • If a match occurs, then the index of item is returned. • If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. • Otherwise, the item is searched for in the sub-array to the right of the middle item. • This process continues on the sub-array as well until the size of the sub-array reduces to zero.
Binary Search Algorithm (contd.) How Binary Search Works: The following is our sorted array and let us assume that we need to search the location of value 31 using binary search. • First, we shall determine middle index of the array by using this formula − mid = (low + high) / 2 • Here it is, (0 + 9 ) / 2 = 4 (integer value of 4.5). So, 4 is the middle index of the array.
Binary Search Algorithm (contd.) • Now we compare the value stored at location 4, with the value being searched, i.e. 31. • As the value is greater than 27 and we have a sorted array, so we also know that the target value must be in the upper portion of the array. • We change our low to mid + 1 which is 5 now.
Binary Search Algorithm (contd.) • Find the new mid value again. mid =(5+9)/2=7 • We compare the value stored at location 7 with our target value 31. • The value stored at location 7 is more than what we are looking for. So, the value must be in the lower part from this location.
Binary Search Algorithm (contd.) • The value stored at location 7 is more than what we are looking for. So, the value must be in the lower part from this location and we change high to mid−1. high= 7 − 1 = 6 • We calculate the mid again. This time it is (5+6)/2 = 5.
Binary Search Algorithm (contd.) • We compare the value stored at location 5 with our target value. We find that it is a match. • We conclude that the target value 31 is stored at location 5.
Binary Search Algorithm (contd.) Conditions necessary for the use of a binary search algorithm: • List of values must be sorted in ascending or descending order, depending on the search algorithm • Input must be compatible with the comparison algorithm
Binary Search Algorithm (contd.) Pseudocode: MAX = N MIN = 1 FOUND = false loc = −1 WHILE (FOUND = false) AND (MAX > = MIN) MID = (MAX + MIN) DIV 2 IF T = LIST [MID] THEN Ioc = MID FOUND = true ELSE IF T < LIST[MID] MAX = MID-1 ELSE MIN = MID+1 END WHILE