Getting Idea About Searching or Familiar With Linear Search and Binary Search
2503

😀 Hello Leetcoders !!
I hope you all doing good .
Let me give you a quick introduction about myself.

Myself  Ishika Rastogi , a 3rd year Student studying Computer Science and Engineering (Data science) as a major . I'm just an average person like other's.
I'm not talented enough, but I work hard & what I know is "Work Hard , Stay Positive and Good Things Will Happen.

I'm not a big coder actually i'm a big learner!!
..........................................................................................................................................................................................................
..........................................................................................................................................................................................................

It's not an language specific article. Trust me, you will learn a lot. HAPPY LEARNING!

So, with that let's start. Hello, my fellow programmer's this post is all about Searching .

If you want to become master, then you have to follow some rules, only after that you can become Master's in Searching

Rules are as follows:

  • Bring up your pen and a fresh new notebook where you have to write all of these thing's which I will teach you right now.
  • If you had started learning Searching, then don't quit. Here's why, ask yourself this question when you feel about to quit, "If you had to leave, then why you had start?"
  • Look back again at above 2 rule's
    Remember : Practice makes a men perfect.

                                                                  #Introduction 

Ques::1 What is searching ?

Ans::    Searching in data structure refers to the process of finding the required information from a collection of items . These sets of items can be in 
different forms, such as an array, linked list , graph, or tree etc.
Second and most firstly question come in mind is Why do we have reqirement of Searching Algorithm?Why do we not search one by one every elements in the list?

Okk so don't worry😌 , here we have an ans -

If you search an element one by one in a list or between the set of items then it takes a lot of time to search an item one by one .
We often need to find one particular item of data amongst many hundreds, thousands, millions or more. 

Without them you would have to look at each item of data – each phone number or business addresses and anything else – individually, to see whether it is what you are looking for. In a large set of data, it will take a long time to do this. Instead, a searching algorithm can be used to help find the item of data you are looking for.

This is why searching algorithms are important.

There are various searching method that can be perform search on a data set.The choice of a particular searching method in a given situation depends on a number of factors , such as

  1. order of elements in the list, i.e , random or sorted.
  2. size of the list .

Okk in this whole journey we are learn about two(2) types of searching algorithm-->>

1.Linear search .
2. Binary search .

linear search is also called as a Sequential search .
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.

In array we have to first define the size of the Array
Let's say:-

int n;                                 // size of array
int arr[n];
for(int i=0;i<n;i++)
{
cin>>a[i];                        // enter the array values 
}

Array :- [3,2,18,33,21,5,7]
K=33.

image

Here , element (K=33) will be find lineraly or called sequentially.

Complexity::
worst case : O(n )
best case : O(1)

image

Advantages                                                                 Disadvantages
1. easy to implement                                                      1. inefficient for larged sized list.
2. it does not require the list to be sorted                              2. it does not leverage  the presence if any pre-existing sort order .

Real-life Application's :-

  1. pickup the nearest phonebook and open it to the first page of names.
                                                                      #Binary Search

Binary search follows the divide and conquer approach in which the list is divided into two halves, and the item is compared with the middle element of the list. If the match is found then, the location of the middle element is returned.
Otherwise, we search into either of the halves depending upon the result produced through the match.
image

Time Complexity :
worst case : O(log2n)
best case : O(1)
Advantages                                                                         Disadvantages
1. requires lesser number of iteration                                       1. it requires list to be sorted 
2. faster than linear search                                       2. comparison to linear search it seem to be little difficult to implement.

Real-life Application's :-
`

  1. Dictionary
  2. Debugging a linear piece of code
  3. Figuring out resource requirements for a large system
    `
    Approach of Bineary Search:
    image

now let k=68(the element which would be find)
now array will divide into three parts:

1. left subarray 
2. right subarray 
3. mid

now the comparing will start , if searching element is greater than mid element , pointer will move right side of array and if searching element is less than the mid , pointer will move towards left subarray.
i.e

 if (arr[mid] == k)  

if ans is NO -->>>we have two options either to move towards right or left .
image

As see in this example , K=68 is greater than mid element =45, so the pointer will move towards right side .

image
Here, the complexity only based on right side of array.
Lets see the implementation of linear search-

image

output -

image

Let's see the implementation of Binary Search -

This implementation is based on iterative method.

image

output-

image

Now just look on the Recursive Method.

image

Output-

image



                                                Some **basic** to **medium** to **hard** questions for practice Bineary Search.


  1. Find Target Indices After Sorting Array.
  2. Intersection of Two Arrays
  3. Guess Number Higher or Lower
  4. Find the element that appears once in a sorted array, and the rest element appears twice (Binary search).
  5. Compare Strings by Frequency of the Smallest Character.
  6. Koko Eating Bananas.
  7. Find the Distance Value Between Two Arrays.
  8. Building Boxes.
  9. Swim in Rising Water.
  10. Capacity To Ship Packages Within D Days
    So these are the some practice question here, some questions are solving by me and some for your practice.
    So , Let's start----->>>

1. Find Target Indices After Sorting Array.

Problem Statement-
You are given a 0-indexed integer array nums and a target element target.A target index is an index i such that nums[i] == target.Return a list of the target indices of nums after sorting nums in non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order. Example- Input: nums = [1,2,5,2,3], target = 2
Output: [1,2].

SOLUTION::
image

....................................................................................................................................................................................................................................................

Try practicing these question by yourself and if anyone could find most useful method to solve these questions can put there solutions on comment box or 
can share any kind of information so that others also take advantage and learn new approaches. Its all means to spread knowledge as much as we can .

............................................................................................................................................................................................................................

Comments (5)