 Hello everyone, I am Mrs. Meenakshi Shrigandhi, Assistant Professor from Walchin Institute of Technology, Shalapur. Welcome to the video lecture on sequential search. Learning outcome. At the end of this session, students will be able to implement sequential search using C programming. What is searching is? Searching is the process of finding a given data in a list of data. It decides whether a search key is present in the data or not. It is the algorithmic process of finding a particular item in a collection of items. Searching methods. Sequential search or linear search. A 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. A binary search also known as half-interval search, logarithmic search or binary chop is a search algorithm that finds the position of a target value within a sorted array by repeatedly dividing the search interval in half. Hashing. Hashing is a process of converting a given key into address. A hash function is used to generate the address according to a mathematical algorithm. The result of a hash function is known as a hash value or simply a hash. Sequential search. A sequential search or a linear search is a method for finding an element within a list. So given a list of elements, it sequentially checks a target element with each element of the list until a match is found or the whole list has been searched. Sequential search is usually very simple to implement and is practical when the list has only few elements. This is because the number of comparison in sequential search is more if the list size is more and it is also practical when performing a single search in an unordered list. Let us see illustration of sequential search. Now consider an array A with this 8 elements and we want to find an element with value 97. So this is how the array is storing the elements. It is having it is starting with the index value 0 and it is ending with the index with the size minus 1 and our role is to find the target element with value 97 from this array. So for that we are setting a pointer with at the index 0. Now I am comparing A of 0th element which is 24 with value with the target element that is 97. So here we can understand that A of 0th element is not equal to 97. In that case we are moving this pointer to the next index that is 1. Again A of 1th element that is 18 is compared to 97. Again we can see that A of 1th element is not equal to 97. For that we are moving the pointer to the next index that is 2. Now A of 2 element is compared to 97 and A of 2 element is minus 25, 25 is not equal to 97. For that we are moving the pointer again to the next element that is with index 3. A of 3 element is now compared with the target element 97. Again we can see that A of 3 element is 32. 32 is not equal to 97. So the pointer is moved ahead now with value with index value 4. Again we can see that A of 4 is not equal to 97 because A of 4 is 87. 87 is not equal to 97. So the pointer is moved again to the next index. Now the next index is 5. A of 5 is minus 56. So minus 56 is not equal to 97. This pointer is moved ahead with index 6. A of 6 is now equal to 97. So we can say that our target is found at A of 6. So here in this case we can see that the number of comparisons are more when the target element is placed far ahead and the number of comparisons are less if the target element is placed at the beginning of the array. So based on this we are going to see the illustration of best case when it is achieved for sequential search. So consider the array again and here I want to find the target element that is 24. The array is represented in this form with index 0 to 7 and here in this case the target element is 24. Again the pointer is set to the 0th index. Now the comparison is done with A of 0 and the target element and we can see that A of 0 is equal to 24. So the element which I want to search is found at the very first place. So we can see that this is the best case of sequential search because in only one comparison the target element has been found. When the search is done only with one comparison the best case is achieved. So when the number of comparisons are less the best case is achieved. Let us just see the illustration of worst case of sequential search. Again we can consider an array with 8 elements. Now in this case I have the target element 22. Array is again represented in this form with index 0 to 7 and the target element now in this case I want to find the element with value 22. For this the pointer is again set to index 0. A of 0th element is compared with the target element and we can see that A of 0 is not equal to 22. So this pointer is moved ahead to the index 1. A of 1 is not equal to 22. So we can see that the pointer is moved ahead again. A of 2 is not equal to 22 because A of 2 is minus 25 and minus 25 is not equal to 22. So the pointer is again moved ahead to the index 3. A of 3 that is 32 is not equal to 22. So the pointer is moved to the next index that is 4 and A of 4 is not equal to 24 because A of 4 is 87 and that is not equal to 22. So the pointer is moved to the next index with 5 and A of 5 is not equal to 22. So this pointer is now moved to the next index that is 6. A of 6 is not equal to 22 as A of 6 is 97 and this is not equal to 22. So this pointer is now moved to the next index that is index 7 and here we can see that A of 7 that is 22 is equal to 22. So we can say that the target has been found at A of 7. So the worst case of sequential search is achieved when the target element is placed at the last place. So in the worst case the number of comparisons are equal to the size of the array. So let us see the advantages and disadvantages of sequential search. Advantages, sequential search is simple and is very easy to understand and implement. It can be implemented on an array of any order let it be sorted or unsorted. Disadvantages, it is much slower compared to other searching method as it takes lot of comparison to find a particular element from an array. It is used only for small amount of data. Worst case is achieved if the target element is not there in the array and still n number of comparisons are done. Try to think an answer. What is the algorithm for sequential search? Pause the video for some time and note down the answer in your book. Algorithm for sequential search. At the very first step input an array of n element and target to be searched. Then initialize the pointer i equal to 0 and repeat step 3 if i is less than the size of the array that is n by incrementing i by 1 with each iteration. And in each iteration check if target is equal to a of i. If target is equal to a of i then we can print the message that target is found and you can use a break statement to come out of the loop. Now after coming out of the loop check if i is equal to n. If i is equal to n then we can print the message as target is not found. Let us see the implementation of sequential search using C programming. Here in this case so here in this case we have declared some variables. We have declared an array of size 50 and n i is the pointer and the target is to take the element to be searched. At the very beginning we are asking the user to how many elements he wants to enter in the array and that is collected that value is collected in the variable n. And after that we are using a for loop and it will execute till it reaches i reaches equal to n. So we are taking n elements from the user to enter into the array and with each iteration we are asking the user to enter an element and that element is entered at the ith position of the array. After that we are taking the element to be searched and that element is collected in the target variable. We are using the for loop. We are using the for loop where i is initialized to 0 and it will repeat till i reaches to n. And with each iteration every time the comparison is done between the target element and the ith element of the array. Whenever the comparison is equal then we can say that the target element is found and we are printing the message that the target element is found at the ith plus 1th position. And after that use a break statement to come out of the for loop. This for loop is repeated till the element is found or it will reach to the end of the array. If the element if i reaches equal to the size of the array then we can say that the element is not found in the array. So this was the implementation of sequential search using C programming. These are my references. Thank you.