<?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: Millicent Kinoti</title>
    <description>The latest articles on DEV Community by Millicent Kinoti (@millicentkinoti).</description>
    <link>https://dev.to/millicentkinoti</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%2F879820%2F6b64aa35-7513-4152-ae9c-823a2d1a16ba.jpeg</url>
      <title>DEV Community: Millicent Kinoti</title>
      <link>https://dev.to/millicentkinoti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/millicentkinoti"/>
    <language>en</language>
    <item>
      <title>Deep Dive Into Data Structures and Algorithms: Big O Notation</title>
      <dc:creator>Millicent Kinoti</dc:creator>
      <pubDate>Thu, 07 Jul 2022 09:02:16 +0000</pubDate>
      <link>https://dev.to/millicentkinoti/deep-dive-into-data-structures-and-algorithms-big-o-notation-oe1</link>
      <guid>https://dev.to/millicentkinoti/deep-dive-into-data-structures-and-algorithms-big-o-notation-oe1</guid>
      <description>&lt;p&gt;In my &lt;a href="https://dev.to/crunchyanomaly/introduction-to-data-structures-and-algorithms-34lb"&gt;previous article&lt;/a&gt; I gave a brief introduction on data structures and the different types. In this article, we will dive expound on ways of analyzing the efficiency of algorithms. &lt;/p&gt;

&lt;h2&gt;
  
  
  Binary Search
&lt;/h2&gt;

&lt;p&gt;Binary search is an algorithm whose input is a sorted list of elements. Suppose you are searching for a word in a dictionary starting with M. You could start at the beginning and keep flipping until you get to M; but this would be a bad approach. It makes more sense starting from near the middle.&lt;br&gt;
In binary search, if the element you are looking for is in the list, it returns its position, otherwise None. Binary search only works when your list is sorted.&lt;/p&gt;

&lt;p&gt;Let's implement binary search in python&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;binary_search&lt;/em&gt; function takes a &lt;strong&gt;sorted&lt;/strong&gt; array &lt;em&gt;nums&lt;/em&gt; and an item.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def binary_search(nums, item):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the item is in the array, the function returns it position. To keep track of what part of array you have to search through, use &lt;em&gt;low&lt;/em&gt; for the lower part of the array and &lt;em&gt;high&lt;/em&gt; for the upper part of the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;low = 0
high = len(nums) - 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While you haven't narrowed it down  to one element check the &lt;em&gt;mid&lt;/em&gt; element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while low &amp;lt;= high:
        mid = (low + high) // 2
        guess = nums[mid]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;em&gt;guess&lt;/em&gt; is equal to the &lt;em&gt;item&lt;/em&gt;, the index is returned. If the guess is too low, you update &lt;em&gt;low&lt;/em&gt; accordingly and if too high, update &lt;em&gt;high&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; if guess == item:
            return mid
        if guess &amp;gt; item:
            high = mid -1
        else:
            low = mid + 1
    return None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Full Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def binary_search(nums, item):
    low = 0
    high = len(nums) - 1

    while low &amp;lt;= high:
        mid = (low + high) // 2
        guess = nums[mid]
        if guess == item:
            return mid
        if guess &amp;gt; item:
            high = mid -1
        else:
            low = mid + 1
    return None

nums = [20, 38, 74, 90, 98, 110]
print(binary_search(nums, 98))
print(binary_search(nums, 22))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's run the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ python3 binary_search.py   
4
None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first statement returns 4, since 98 is at index 4 on the list and the second statement returns None since 22 is not on the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Running time&lt;/strong&gt;&lt;br&gt;
The running time of an algorithm grows with the input size, although it may vary for different inputs of the same size. Binary search runs in &lt;em&gt;logarithmic time&lt;/em&gt;. If a list has 128 names, the maximum number of steps it would take is 7(2 pow 7 is 128).&lt;/p&gt;

&lt;h2&gt;
  
  
  Big O Notation
&lt;/h2&gt;

&lt;p&gt;Big O notation is a special notation that is used to show how an algorithm scales with respect to the size of its growth.Growth can be sublinear, linear, superlinear etc. You need to know how the running time increases as the size of the list increases. It establishes a &lt;em&gt;worst case scenario&lt;/em&gt;. Big O notation lets you compare the number of operations. For example, binary search needs log n operations to &lt;br&gt;
to check a list of size n. The running time in Big O notation is &lt;strong&gt;O(log n)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Common Big O run times sorted from fastest to slowest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;O(log n) -known as &lt;em&gt;log time&lt;/em&gt;. Example : Binary search&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(n) - known as &lt;em&gt;linear time&lt;/em&gt;. Example : Simple search&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(n * log n) : A fast sorting algorithm like quicksort&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(n2) : A slow sorting algorithm like selection sort.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(n!) : A really slow algorithm like the travelling salesperson.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The speed of an algorithm is not measured in seconds but in growth of the number of operations. Instead, we talk about how quickly the tun time of an algorithm increases as the size of the input increases. In my next article, we will cover on the different Big O run times.&lt;/p&gt;

</description>
      <category>python</category>
      <category>datastructures</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to Data Structures and Algorithms</title>
      <dc:creator>Millicent Kinoti</dc:creator>
      <pubDate>Tue, 21 Jun 2022 10:05:58 +0000</pubDate>
      <link>https://dev.to/millicentkinoti/introduction-to-data-structures-and-algorithms-34lb</link>
      <guid>https://dev.to/millicentkinoti/introduction-to-data-structures-and-algorithms-34lb</guid>
      <description>&lt;h2&gt;
  
  
  Data Structure
&lt;/h2&gt;

&lt;p&gt;A data structure can be defined as a specialized method of organizing and storing data so that execution of operations on data can be more efficient. Data can be organized in many ways such as lists or dictionaries in Python.&lt;/p&gt;

&lt;p&gt;Data structures can be of two types: primitive and non primitive&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Primitive data structures&lt;/strong&gt;&lt;br&gt;
Primitive data structures can simply be defined as data types. Data types are elementary. They cannot be further subdivided. They include int, char, double.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Non Primitive data structures&lt;/strong&gt;&lt;br&gt;
Non primitive data structure is a data structure which can be used to store data of more than one type.&lt;br&gt;
Non primitive data structure can be subdivided into two categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Linear data structure.&lt;/li&gt;
&lt;li&gt;Non-linear data structure&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1. Linear data structure&lt;/strong&gt;&lt;br&gt;
The elements of a linear data structure are stored in a sequential manner. Types of linear data structure include array, list, stack and queues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array&lt;/strong&gt;&lt;br&gt;
An array is a data structure that stores elements of the same data type in contiguous and adjacent memory locations. An item stored in an array is called an element and an index indicates the position of the element within in an array. The index of an array starts from 0, therefore if an array has 10 elements, the index will start from 0 to 9.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List&lt;/strong&gt;&lt;br&gt;
A list is a data structure which stores elements in an ordered manner. It allows repetition and therefore a single piece of data can occur more than once in a list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack&lt;/strong&gt;&lt;br&gt;
A stack is data structure that holds an ordered, linear sequence of items. It uses the Last In, First Out(LIFO) structure to access the elements in the stack. A real life example is that of a stack of plates. You can only access the plates from the top of the stack and can only add a plate to the top of the stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Queue&lt;/strong&gt;&lt;br&gt;
A queue is similar to a stack but it is open at both its ends. It uses the First In, First Out technique to access the elements. Data insertion occurs at the rear end or the tail of the queue whole deletion at the front end or the head of the queue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Non Linear Data Structure&lt;/strong&gt;&lt;br&gt;
The elements are not arranged sequentially rather in a hierarchical manner. Elements cannot be traversed in a single run. Examples include trees and graphs&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trees&lt;/strong&gt;&lt;br&gt;
 A tree is a hierarchical data structure that consists of nodes connected by edges. A node is a  structure which contains data  and pointers to its child nodes.An edge is  used to connect two nodes. Every node except the root node is connected by exactly one incoming edge from another node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Graphs&lt;/strong&gt;&lt;br&gt;
A graph is a data structure made up of nodes or vertices and edges&lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithms
&lt;/h2&gt;

&lt;p&gt;An algorithm is a set of well-defined instructions to solve a particular problem. For example, an algorithm to add two numbers can be as follows:&lt;/p&gt;

&lt;p&gt;Step 1: Start.&lt;br&gt;
Step 2: Declare variables num1, num2 and sum.&lt;br&gt;
Step 3: Read values num1 and num2.&lt;br&gt;
Step 4: Add num1 and num2.&lt;br&gt;
Step 5: Assign the result to sum. sum ← num1 + num2.&lt;br&gt;
Step 6: Display sum.&lt;br&gt;
Step 7: Stop&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Characteristics of an algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Finiteness&lt;/strong&gt; - Must terminate after a finite number of steps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unambiguous&lt;/strong&gt; - Should be clear and unambiguous.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Independent&lt;/strong&gt; - Should be independent of any programming code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Input&lt;/strong&gt; - It should have 0 or more well defined inputs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output&lt;/strong&gt; - It should have 1 or more well-defined outputs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Effectiveness&lt;/strong&gt; - It must be possible to perform each step of the algorithm correctly and in a finite amount of time. Effectiveness is determined by:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Space complexity&lt;/strong&gt; :The memory space needed by a specific algorithm to be executed. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time complexity&lt;/strong&gt;: The time required by a program to be completed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
