1 / 16

Algorithms

This is a topic of A-level Computer Science.

monirMDIC
Download Presentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Presentationon 4.1.2 Algorithms

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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

  14. 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

  15. Q/A Session

  16. Thank you very much.

More Related