<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paul Gichure</title>
    <description>The latest articles on DEV Community by Paul Gichure (@pgichure).</description>
    <link>https://dev.to/pgichure</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F215889%2F6ac70dc4-8639-4a6c-816c-1d96392e5a38.jpg</url>
      <title>DEV Community: Paul Gichure</title>
      <link>https://dev.to/pgichure</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pgichure"/>
    <language>en</language>
    <item>
      <title>Data Structure Algorithms: Binary Search</title>
      <dc:creator>Paul Gichure</dc:creator>
      <pubDate>Mon, 21 Feb 2022 06:06:55 +0000</pubDate>
      <link>https://dev.to/pgichure/data-structure-algorithms-binary-search-7l1</link>
      <guid>https://dev.to/pgichure/data-structure-algorithms-binary-search-7l1</guid>
      <description>&lt;p&gt;Binary Search is applied on the sorted array or list of large size. It has a time complexity of O(log n) making it very fast as compared to other searching algorithms. The only limitation is that the array or list of elements must be sorted for the binary search algorithm to work on it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Algorithm&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.  Start with the middle element: 
If the target value is equal to the middle element of the array, then return the index of the middle element.
If not, then compare the middle element with the target value, 
If the target value is greater than the number in the middle index, then pick the elements to the right of the middle index, and start with Step 1.
If the target value is less than the number in the middle index, then pick the elements to the left of the middle index, and start with Step 1.
2.  When a match is found, return the index of the element matched.
3.  If no match is found, then return -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;&lt;br&gt;
Binary search has a time complexity of O(log n).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.pgichure.samples.dsa.binary;

public class BinarySearch {

    public static void main(String[] args) {

        int[] values = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

        int search_number = 13;

        int index = 0; // this will hold the index of middle elements

        int max = values.length - 1; //the maximum index
        int min = 0; //the minimum index

        int step = 0;  // to find out in how many steps we completed the search

        while(max &amp;gt;= min)
        {

            index = (max + min) / 2;
            // we made the first guess, incrementing step by 1
            step++;

            if(values[index] ==  search_number){
                System.out.println(search_number + " found at index "+ index + " after "+step+" steps");
                System.exit(0);
            }
            else if(values[index] &amp;gt;  search_number) {
                // target would be in the left half
                max = (index - 1);
            }
            else{
                // target would be in the right half
                min = (index + 1);
            }
        }
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Source Code&lt;/strong&gt;&lt;br&gt;
You can access the source code &lt;a href="https://github.com/Gichure/data-structure-and-algorithms/tree/main/binary-search"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Data Structure Algorithms: Linear Search</title>
      <dc:creator>Paul Gichure</dc:creator>
      <pubDate>Fri, 18 Feb 2022 05:18:40 +0000</pubDate>
      <link>https://dev.to/pgichure/data-structure-and-algorithms-linear-search-43l</link>
      <guid>https://dev.to/pgichure/data-structure-and-algorithms-linear-search-43l</guid>
      <description>&lt;p&gt;Data Structures are the programmatic way of storing data for efficient use. In this series, you will get great understanding on Data Structures needed to understand the complexity of enterprise level applications and need of algorithms and data structures. Good understanding of DSA helps address the following problems;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Enormous data Search&lt;/li&gt;
&lt;li&gt;Limited processor speed&lt;/li&gt;
&lt;li&gt;Handling multiple concurrent requests&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Content
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Linear Search
&lt;/h4&gt;

&lt;p&gt;Linear search is a search algorithm where 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.&lt;/p&gt;

&lt;h4&gt;
  
  
  Algorithm
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Linear Search (Array A, Value x)  

Step 1: Set i to 1  
Step 2: if i &amp;gt; n then go to step 7  
Step 3: if A[i] = x then go to step 6  
Step 4: Set i to i + 1  
Step 5: Go to Step 2  
Step 6: Print Element x Found at index i and go to step 8  
Step 7: Print element not found  
Step 8: Exit  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pseudocode
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function linear_search (list, value)  

   for each item in the list  
      if match item == value  
         return the item's location  
      end if  
   end for  

end function 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Source Code
&lt;/h4&gt;

&lt;p&gt;An example can be found &lt;a href="https://github.com/Gichure/data-structure-and-algorithms/tree/main/linear-search"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
