title
7.1 Linear Search Algorithm | Linear Search in C | Data Structures Tutorials

description
What is Linear Search? Linear Search Algorithm full explanation with Code. Step by step instruction showing how Linear Search works. DSA Full Course: https: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU ****************************************** See Complete Playlists: C Programming Course: https://www.youtube.com/playlist?list=PLdo5W4Nhv31a8UcMN9-35ghv8qyFWD9_S C++ Programming: https://www.youtube.com/playlist?list=PLdo5W4Nhv31YU5Wx1dopka58teWP9aCee Python Full Course: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bZSiqiOL5ta39vSnBxpOPT Printing Pattern in C: https://www.youtube.com/playlist?list=PLdo5W4Nhv31Yu1igxTE2x0aeShbKtVcCy DAA Course: https://www.youtube.com/playlist?list=PLdo5W4Nhv31ZTn2P9vF02bkb3SC8uiUUn Placement Series: https://www.youtube.com/playlist?list=PLdo5W4Nhv31YvlDpJhvOYbM9Ap8UypgEy Dynamic Programming: https://www.youtube.com/playlist?list=PLdo5W4Nhv31aBrJE1WS4MR9LRfbmZrAQu Operating Systems: //www.youtube.com/playlist?list=PLdo5W4Nhv31a5ucW_S1K3-x6ztBRD-PNa DBMS: https://www.youtube.com/playlist?list=PLdo5W4Nhv31b33kF46f9aFjoJPOkdlsRc ************************************************* Connect & Contact Me: Facebook: https://www.facebook.com/Jennys-Lectures-CSIT-Netjrf-316814368950701/ Quora: https://www.quora.com/profile/Jayanti-Khatri-Lamba Instagram: https://www.instagram.com/jayantikhatrilamba/ #linearsearch #datastructures #jennyslectures

detail
{'title': '7.1 Linear Search Algorithm | Linear Search in C | Data Structures Tutorials', 'heatmap': [{'end': 640.759, 'start': 571.753, 'weight': 0.759}, {'end': 742.088, 'start': 725.253, 'weight': 0.72}, {'end': 875.236, 'start': 859.765, 'weight': 0.724}, {'end': 906.31, 'start': 887.465, 'weight': 1}], 'summary': "Covers linear search algorithm, including its working principle and code implementation using a for loop to search for the number 42 in an array of 8 elements. it also discusses improving loop efficiency, the 'break' statement, and the time complexity of linear search, with worst case time complexity as o(n) and average case time complexity as o(n/2+1), while mentioning binary search.", 'chapters': [{'end': 437.393, 'segs': [{'end': 36.633, 'src': 'embed', 'start': 0.169, 'weight': 0, 'content': [{'end': 3.432, 'text': 'going to write down a piece of code for this linear search algorithm.', 'start': 0.169, 'duration': 3.263}, {'end': 9.737, 'text': 'now, first of all, i am going to take one example and we will see the working principle of this linear search.', 'start': 3.432, 'duration': 6.305}, {'end': 12.72, 'text': 'see, this is also known as sequential search.', 'start': 9.737, 'duration': 2.983}, {'end': 15.702, 'text': 'fine, now, suppose this is our data.', 'start': 12.72, 'duration': 2.982}, {'end': 18.704, 'text': 'this is our array having how many elements.', 'start': 15.702, 'duration': 3.002}, {'end': 25.47, 'text': 'n is equal to 8, 8 elements and index is from 0 to 7.', 'start': 18.704, 'duration': 6.766}, {'end': 36.633, 'text': 'right now, suppose you want to search a data And value of this data or key, or you can take any variable.', 'start': 25.47, 'duration': 11.163}], 'summary': 'Demonstrating linear search algorithm with 8 elements array.', 'duration': 36.464, 'max_score': 0.169, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU169.jpg'}, {'end': 264.826, 'src': 'embed', 'start': 227.483, 'weight': 1, 'content': [{'end': 233.206, 'text': 'or you can write down a function of linear search and you can call that function in your main function.', 'start': 227.483, 'duration': 5.723}, {'end': 234.526, 'text': 'either method you can follow.', 'start': 233.206, 'duration': 1.32}, {'end': 237.227, 'text': 'fine, i am going to follow here the iterative approach.', 'start': 234.526, 'duration': 2.701}, {'end': 238.348, 'text': 'i am going to write the for loop.', 'start': 237.227, 'duration': 1.121}, {'end': 248.682, 'text': 'we are going to start a loop from this index, zeroth index, and the loop will go till end right now.', 'start': 239.935, 'duration': 8.747}, {'end': 251.524, 'text': 'see, here we have one for loop.', 'start': 248.682, 'duration': 2.842}, {'end': 264.826, 'text': 'we will take one variable i i is equal to starting from zero and i less than n and i plus plus, because here n is 8.', 'start': 251.524, 'duration': 13.302}], 'summary': 'Implementing linear search with an iterative approach using a for loop, starting from index 0 and going till the end, with n equal to 8.', 'duration': 37.343, 'max_score': 227.483, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU227483.jpg'}], 'start': 0.169, 'title': 'Linear search algorithm', 'summary': 'Explains the linear search algorithm, its working principle, and code implementation using a for loop, with an example of searching for the number 42 in an array of 8 elements, and the stopping conditions for element found and not found.', 'chapters': [{'end': 437.393, 'start': 0.169, 'title': 'Linear search algorithm', 'summary': 'Explains the linear search algorithm, its working principle, and code implementation using a for loop, with an example of searching for the number 42 in an array of 8 elements, and the stopping conditions for element found and not found.', 'duration': 437.224, 'highlights': ['The linear search algorithm involves starting the search from the beginning of the array and accessing each element one by one to compare with the desired data, with stopping conditions of finding the data or reaching the end of the array.', 'The code for the linear search algorithm can be implemented using an iterative method with a for loop, accessing each element of the array and comparing with the desired data to print the location of the element if found.', 'The example of searching for the number 42 in an array of 8 elements demonstrates the working of the linear search algorithm, with the element found at index 5 being printed as the result of the search.']}], 'duration': 437.224, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU169.jpg', 'highlights': ['The example of searching for the number 42 in an array of 8 elements demonstrates the working of the linear search algorithm, with the element found at index 5 being printed as the result of the search.', 'The code for the linear search algorithm can be implemented using an iterative method with a for loop, accessing each element of the array and comparing with the desired data to print the location of the element if found.', 'The linear search algorithm involves starting the search from the beginning of the array and accessing each element one by one to compare with the desired data, with stopping conditions of finding the data or reaching the end of the array.']}, {'end': 716.182, 'segs': [{'end': 521.323, 'src': 'embed', 'start': 437.393, 'weight': 0, 'content': [{'end': 439.854, 'text': 'no, now control will go out from this for loop.', 'start': 437.393, 'duration': 2.461}, {'end': 443.013, 'text': 'now here the drawback is what for this code?', 'start': 441.072, 'duration': 1.941}, {'end': 450.117, 'text': 'drawback is what we have found the element at index fifth, but still still this.', 'start': 443.013, 'duration': 7.104}, {'end': 455.98, 'text': 'this code is searching for sixth location also and for seventh index also.', 'start': 450.117, 'duration': 5.863}, {'end': 459.862, 'text': 'now, what is the point to search after finding the data?', 'start': 455.98, 'duration': 3.882}, {'end': 460.903, 'text': 'right we have.', 'start': 459.862, 'duration': 1.041}, {'end': 463.804, 'text': 'we have found the data now at fifth index only.', 'start': 460.903, 'duration': 2.901}, {'end': 465.799, 'text': 'we have found 42.', 'start': 463.804, 'duration': 1.995}, {'end': 467.001, 'text': 'now, what should be done?', 'start': 465.799, 'duration': 1.202}, {'end': 472.21, 'text': 'this algorithm or this code should have stopped at here only.', 'start': 467.001, 'duration': 5.209}, {'end': 476.557, 'text': 'but this is not stopping now you have to add one more condition here.', 'start': 472.21, 'duration': 4.347}, {'end': 479.442, 'text': 'right, you have to write what break.', 'start': 476.557, 'duration': 2.885}, {'end': 487.01, 'text': 'after this printf, you will write break and after you will close this.', 'start': 483.268, 'duration': 3.742}, {'end': 494.575, 'text': 'if statement now see this break means what, as soon as control will go to this break statement.', 'start': 487.01, 'duration': 7.565}, {'end': 500.718, 'text': 'then then what the control will go out from the loop in which break statement is written.', 'start': 494.575, 'duration': 6.143}, {'end': 502.399, 'text': 'now, break statement is written.', 'start': 500.718, 'duration': 1.681}, {'end': 503.62, 'text': 'in which loop?', 'start': 502.399, 'duration': 1.221}, {'end': 504.721, 'text': 'which for loop here?', 'start': 503.62, 'duration': 1.101}, {'end': 511.622, 'text': 'so we found the data at index 5, at index 5, right again.', 'start': 505.621, 'duration': 6.001}, {'end': 512.942, 'text': 'control will go to here.', 'start': 511.622, 'duration': 1.32}, {'end': 513.782, 'text': 'only break.', 'start': 512.942, 'duration': 0.84}, {'end': 521.323, 'text': 'break means the control will go out out from this loop, out from this loop here only now.', 'start': 513.782, 'duration': 7.541}], 'summary': 'The code has a drawback of continuing to search after finding the data at the fifth index, requiring the addition of a break statement to stop the loop.', 'duration': 83.93, 'max_score': 437.393, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU437393.jpg'}, {'end': 640.759, 'src': 'heatmap', 'start': 571.753, 'weight': 0.759, 'content': [{'end': 580.458, 'text': 'whatever you will write here, fine, it means if i value has been raised to 8.', 'start': 571.753, 'duration': 8.705}, {'end': 583.199, 'text': 'here 8 is what number of element it means.', 'start': 580.458, 'duration': 2.741}, {'end': 588.64, 'text': 'if i value becomes same as this n value number of elements.', 'start': 583.199, 'duration': 5.441}, {'end': 590.681, 'text': 'in that case data is not present.', 'start': 588.64, 'duration': 2.041}, {'end': 592.541, 'text': 'you can say right.', 'start': 590.681, 'duration': 1.86}, {'end': 594.402, 'text': 'so what you will write here?', 'start': 592.541, 'duration': 1.861}, {'end': 609.446, 'text': 'you will write if, if i value equal to equal to n, in that case you can write here printf element not found.', 'start': 594.402, 'duration': 15.044}, {'end': 616.377, 'text': "if you don't write this condition, if you don't write this one.", 'start': 611.375, 'duration': 5.002}, {'end': 626.18, 'text': 'one second method to print this statement is element not found is see, you can take one variable here at the starting.', 'start': 616.377, 'duration': 9.803}, {'end': 628.241, 'text': 'you can take either found or flag.', 'start': 626.18, 'duration': 2.061}, {'end': 629.342, 'text': "it's up to you.", 'start': 628.241, 'duration': 1.101}, {'end': 632.363, 'text': "i'm taking found, a variable.", 'start': 629.342, 'duration': 3.021}, {'end': 640.759, 'text': 'found is equal to zero in the program at starting, and if you find the element, in that case you will do what you will.', 'start': 632.363, 'duration': 8.396}], 'summary': "If i equals n, print 'element not found'.", 'duration': 69.006, 'max_score': 571.753, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU571753.jpg'}, {'end': 640.759, 'src': 'embed', 'start': 611.375, 'weight': 3, 'content': [{'end': 616.377, 'text': "if you don't write this condition, if you don't write this one.", 'start': 611.375, 'duration': 5.002}, {'end': 626.18, 'text': 'one second method to print this statement is element not found is see, you can take one variable here at the starting.', 'start': 616.377, 'duration': 9.803}, {'end': 628.241, 'text': 'you can take either found or flag.', 'start': 626.18, 'duration': 2.061}, {'end': 629.342, 'text': "it's up to you.", 'start': 628.241, 'duration': 1.101}, {'end': 632.363, 'text': "i'm taking found, a variable.", 'start': 629.342, 'duration': 3.021}, {'end': 640.759, 'text': 'found is equal to zero in the program at starting, and if you find the element, in that case you will do what you will.', 'start': 632.363, 'duration': 8.396}], 'summary': "Use a variable 'found' to indicate element presence or absence in the program.", 'duration': 29.384, 'max_score': 611.375, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU611375.jpg'}], 'start': 437.393, 'title': 'Improving loop efficiency and searching algorithm in c', 'summary': "Discusses the inefficiency of a search algorithm in a for loop, highlighting the need to add a stopping condition and explaining the use of the 'break' statement to improve efficiency. additionally, it explains a searching algorithm in c, demonstrating the process of checking if an element is present by iterating through the elements and using a flag variable to indicate the presence of the element.", 'chapters': [{'end': 542.77, 'start': 437.393, 'title': 'Improving loop efficiency', 'summary': "Discusses the inefficiency of a search algorithm in a for loop, highlighting the need to add a stopping condition and explaining the use of the 'break' statement to improve efficiency.", 'duration': 105.377, 'highlights': ['The code continues searching for elements even after finding the desired data, leading to inefficiency and unnecessary iterations.', "The need to add a 'break' statement after finding the desired data is emphasized to stop the loop from searching further, improving efficiency and reducing unnecessary iterations.", "The chapter explains the functionality of the 'break' statement, highlighting its role in exiting the loop as soon as the condition is met, thereby improving the algorithm's efficiency."]}, {'end': 716.182, 'start': 542.77, 'title': 'Searching algorithm in c', 'summary': 'Explains a searching algorithm in c, demonstrating the process of checking if an element is present by iterating through the elements and using a flag variable to indicate the presence of the element.', 'duration': 173.412, 'highlights': ['The algorithm demonstrates the process of checking if an element is present by iterating through the elements and using a flag variable to indicate the presence of the element.', "The program starts by initializing a variable 'found' to zero, and if the element is found, 'found' is set to one, indicating the element's presence.", "If the value of 'found' remains zero after the loop, it indicates that the element is not present, and the program prints 'element not present.'"]}], 'duration': 278.789, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU437393.jpg', 'highlights': ["The need to add a 'break' statement after finding the desired data is emphasized to stop the loop from searching further, improving efficiency and reducing unnecessary iterations.", 'The code continues searching for elements even after finding the desired data, leading to inefficiency and unnecessary iterations.', "The chapter explains the functionality of the 'break' statement, highlighting its role in exiting the loop as soon as the condition is met, thereby improving the algorithm's efficiency.", 'The algorithm demonstrates the process of checking if an element is present by iterating through the elements and using a flag variable to indicate the presence of the element.', "The program starts by initializing a variable 'found' to zero, and if the element is found, 'found' is set to one, indicating the element's presence.", "If the value of 'found' remains zero after the loop, it indicates that the element is not present, and the program prints 'element not present.'"]}, {'end': 908.572, 'segs': [{'end': 795.986, 'src': 'heatmap', 'start': 725.253, 'weight': 0, 'content': [{'end': 727.996, 'text': 'first of all we are going to check in best case.', 'start': 725.253, 'duration': 2.743}, {'end': 733.543, 'text': 'best case is what suppose this is our array and you want to find out.', 'start': 727.996, 'duration': 5.547}, {'end': 735.965, 'text': 'data is equal to 15.', 'start': 733.543, 'duration': 2.422}, {'end': 736.665, 'text': 'in linear search.', 'start': 735.965, 'duration': 0.7}, {'end': 742.088, 'text': 'we are going to start from the initial point of the array, from the 0th index.', 'start': 736.665, 'duration': 5.423}, {'end': 746.169, 'text': 'find out, check 15 first at first index.', 'start': 742.088, 'duration': 4.081}, {'end': 748.37, 'text': 'at 0th index we have value 15.', 'start': 746.169, 'duration': 2.201}, {'end': 750.171, 'text': '15 is equal to this data.', 'start': 748.37, 'duration': 1.801}, {'end': 756.354, 'text': 'yes, it means in one comparison only you have got the data.', 'start': 750.171, 'duration': 6.183}, {'end': 762.296, 'text': 'so in the best case, the time complexity would be order of 1 only.', 'start': 756.354, 'duration': 5.942}, {'end': 763.497, 'text': 'what about worst case?', 'start': 762.296, 'duration': 1.201}, {'end': 775.128, 'text': 'worst case means, suppose you want to find out the data 17 here, how many comparisons you have to do?', 'start': 766.42, 'duration': 8.708}, {'end': 780.873, 'text': 'first of all 15, then 5, then with 20, then 35, 2, 42, 67 and then 17.', 'start': 775.128, 'duration': 5.745}, {'end': 788.159, 'text': 'and here you got the data means n, n comparisons, eight comparisons, right.', 'start': 780.873, 'duration': 7.286}, {'end': 795.986, 'text': 'so that is why here the time complexity in worst case is order of n, the number of elements, total number of elements n.', 'start': 789.113, 'duration': 6.873}], 'summary': "In best case, time complexity is o(1); in worst case, it's o(n) with 8 comparisons.", 'duration': 59.321, 'max_score': 725.253, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU725253.jpg'}, {'end': 906.31, 'src': 'heatmap', 'start': 859.765, 'weight': 1, 'content': [{'end': 863.607, 'text': 'if data is present at here, only in that case, how many comparisons are there?', 'start': 859.765, 'duration': 3.842}, {'end': 865.809, 'text': 'two only plus.', 'start': 863.607, 'duration': 2.202}, {'end': 867.57, 'text': 'third case is data is present here.', 'start': 865.809, 'duration': 1.761}, {'end': 869.812, 'text': 'only in that case, how many comparisons?', 'start': 867.57, 'duration': 2.242}, {'end': 875.236, 'text': 'one, two and three, like that till n comparisons?', 'start': 869.812, 'duration': 5.424}, {'end': 887.465, 'text': 'many cases sum of all the cases right, divided by number of cases, are n right, and i hope you know, you know the formula of this one, 1 plus 2,', 'start': 876.38, 'duration': 11.085}, {'end': 893.788, 'text': 'plus 3 up to n, that is n into n plus 1 divided by 2, and here we have n, n n.', 'start': 887.465, 'duration': 6.323}, {'end': 900.505, 'text': 'So the time complexity would be order of n plus 1 divided by 2.', 'start': 895.18, 'duration': 5.325}, {'end': 901.005, 'text': 'you can say', 'start': 900.505, 'duration': 0.5}, {'end': 903.207, 'text': 'So this is all about linear search.', 'start': 901.606, 'duration': 1.601}, {'end': 906.31, 'text': 'In next video I am going to talk about binary search.', 'start': 903.247, 'duration': 3.063}], 'summary': 'In linear search, average time complexity is o(n/2); next video: binary search', 'duration': 55.312, 'max_score': 859.765, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU859765.jpg'}], 'start': 716.562, 'title': 'Time complexity of linear search', 'summary': 'Explains the time complexity of linear search, with worst case time complexity as o(n) and average case time complexity as o(n/2+1), and includes comparisons and mentions of binary search.', 'chapters': [{'end': 795.986, 'start': 716.562, 'title': 'Time complexity in linear search', 'summary': 'Explains the time complexity in linear search, stating that the best case time complexity is o(1) and the worst case time complexity is o(n), with examples and comparisons.', 'duration': 79.424, 'highlights': ['The best case time complexity in linear search is O(1) as it requires only one comparison to find the data.', 'The worst case time complexity in linear search is O(n) as it requires n comparisons to find the data.']}, {'end': 908.572, 'start': 795.986, 'title': 'Time complexity of linear search', 'summary': 'Explains the time complexity of linear search, stating that the worst case time complexity is n, and the average case time complexity is order of n plus 1 divided by 2, with examples of case comparisons and a mention of binary search.', 'duration': 112.586, 'highlights': ['The worst case time complexity of linear search is n, as the maximum number of iterations in the loop is determined by the input size (n).', 'In the average case, the time complexity of linear search is order of n plus 1 divided by 2, calculated using the formula 1 plus 2 plus 3 up to n, which simplifies to n into n plus 1 divided by 2.', 'The chapter also mentions the examples of case comparisons for different data positions in the linear search, providing insights into the number of comparisons in various scenarios.']}], 'duration': 192.01, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/C46QfTjVCNU/pics/C46QfTjVCNU716562.jpg', 'highlights': ['The worst case time complexity in linear search is O(n) as it requires n comparisons to find the data.', 'In the average case, the time complexity of linear search is order of n plus 1 divided by 2, calculated using the formula 1 plus 2 plus 3 up to n, which simplifies to n into n plus 1 divided by 2.', 'The best case time complexity in linear search is O(1) as it requires only one comparison to find the data.', 'The worst case time complexity of linear search is n, as the maximum number of iterations in the loop is determined by the input size (n).', 'The chapter also mentions the examples of case comparisons for different data positions in the linear search, providing insights into the number of comparisons in various scenarios.']}], 'highlights': ['The worst case time complexity in linear search is O(n) as it requires n comparisons to find the data.', 'The average case time complexity of linear search is O(n/2+1), calculated using the formula 1 plus 2 plus 3 up to n, which simplifies to n into n plus 1 divided by 2.', 'The best case time complexity in linear search is O(1) as it requires only one comparison to find the data.', 'The example of searching for the number 42 in an array of 8 elements demonstrates the working of the linear search algorithm, with the element found at index 5 being printed as the result of the search.', 'The code for the linear search algorithm can be implemented using an iterative method with a for loop, accessing each element of the array and comparing with the desired data to print the location of the element if found.', 'The linear search algorithm involves starting the search from the beginning of the array and accessing each element one by one to compare with the desired data, with stopping conditions of finding the data or reaching the end of the array.', "The need to add a 'break' statement after finding the desired data is emphasized to stop the loop from searching further, improving efficiency and reducing unnecessary iterations.", "The chapter explains the functionality of the 'break' statement, highlighting its role in exiting the loop as soon as the condition is met, thereby improving the algorithm's efficiency.", 'The algorithm demonstrates the process of checking if an element is present by iterating through the elements and using a flag variable to indicate the presence of the element.', "The program starts by initializing a variable 'found' to zero, and if the element is found, 'found' is set to one, indicating the element's presence.", "If the value of 'found' remains zero after the loop, it indicates that the element is not present, and the program prints 'element not present.'"]}