Searching and Sorting Algorithm
What is Linear Search ? And what is binary search ? What is linear search? And what's binary search? We're going to code it here because it is asked a lot. Binary search is a smart algorithm named Binary Search what does binary search do. I will give you a real life example of binary search that when you will do binary search. The linear search is very simple , quite a straight forward algorithm to find all the elements of the array one by one : Where the element is found, it has to be said that the search is over. Or if you reach the end of an array, then you have to say that the. search is. over. If the element could not be found, that 's the end , that's the end. that's all. get our element , ok You have been told that 238 page no. should be opened inside a thousand pages in a book. If you turned the pages one by one, you would have to turn 238 pages. If pages were stitched at random, then you would not be able to do this. This pages would have been stitched randomly. Binary search works just like I took the example of the book. do 238 page number so what is the first condition of binary search , the condition is , should be sorted array Okay. Sorted array Array must be sorted , okay. Array needs to be sorted this is its first condition. You can add any number of elements to a sorted array. So I 'll keep track of three things here: Low, High, and Mid. One will keep the track of high , will keep track of mid. So here I am making a table Low , High , and Mid , OK So I would say now my mid is low And my high is what it is , okay And now I will calculate the new mid. I calculate the mid (8+4 ) /2= 6 My mid turns 6 , so this will be my mid , so it will be the mid. Linear vs Binary Search: For you guys Surely everyone must read this, access this , Downloading from the description , you will get the link somewhere. When I upload the video , it takes me some time to put the notes on the website. I do everything according to my own , right here And I try to do everything in time , put the video and then after that here I have the notes. I did not talk about this , here it is a little important to tell it So look at this , which is our Linear Search and Binary Search : What do we people have to do , if you are looking at linear search , then its complexity is O ( n ) But if you look here binary search So I 'm halving the array until it runs out , does not converge speak the same ( log n ) So here you people will not face any issue , okay So this thing must have been cleared to you guys , I hope. int searchindex = linear search. Do return 1 means I have got this element here in the array or I will do one thing that I will return only the index of the array where I have. Otherwise, what will i do otherwise i will return -1 So let 's see here whether it works or not. As soon as the element is found, while traversing this array Then you return the index where that element was found. Otherwise what you do is -1 If we come to the end while searching in this array then you return -1. As this return i ; is written , the function terminates and returns The function 's activation record in this stack gets lost. Sorting helps us in this way when we have a lot of comments. In binary search, our array has to be sorted otherwise we ca n't perform binary search. A sorted array enables faster retrieval of any element quickly. That's the advantage of having a sorted array. We 'll move to the next video now and then what we 'll see our algorithms. Good overview of what sorting is, why to use sorting. In fact, we do sorting in our daily lives too. If someone gives you a big bundle of money, you perform sorting on it. How do you sort it? You swap a note from one place to another. That means you swap their positions. You swap the position of the note in your hand. The procedure to sort is known as the sorting algorithm. And what the time complexity of a sorting algorithm is. We have a lot of sorting algorithms.
Written for
- Institution
- MAKAUT University
- Course
- Data Structure and Algorithm
Document information
- Uploaded on
- March 19, 2023
- Number of pages
- 24
- Written in
- 2022/2023
- Type
- Class notes
- Professor(s)
- Nil
- Contains
- All classes
Subjects
-
searching algorithm
-
sorting algorithm