<?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: Shilpa Jain</title>
    <description>The latest articles on DEV Community by Shilpa Jain (@jainroe).</description>
    <link>https://dev.to/jainroe</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%2F56621%2F2313dc87-72da-4348-acb7-a0d62ea854a5.jpg</url>
      <title>DEV Community: Shilpa Jain</title>
      <link>https://dev.to/jainroe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jainroe"/>
    <language>en</language>
    <item>
      <title>Stacks in a (food)shell</title>
      <dc:creator>Shilpa Jain</dc:creator>
      <pubDate>Wed, 21 Feb 2018 12:39:57 +0000</pubDate>
      <link>https://dev.to/jainroe/stacks-in-a-foodshell-2hi8</link>
      <guid>https://dev.to/jainroe/stacks-in-a-foodshell-2hi8</guid>
      <description>

&lt;p&gt;Welcome back, daredevils! I see you like to live dangerously.&lt;/p&gt;

&lt;p&gt;If you’ve no idea about I am talking about, &lt;a href="https://jshilpa.com/is-there-life-after-learning-data-structures/"&gt;you should check out this post before reading this article.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The goal of this data structures series is to ruin your life(for good) by thinking and applying data structures to real life setting. It will not introduce you to the practical world of programming, but a mastery of the concepts will provide a start toward understanding the nature of computation.&lt;/p&gt;

&lt;p&gt;Let me start this series with simplest of all yet among the most important data structure – ‘Stacks’. In recent times, this word is being used everywhere. Be it about companies looking for full stack developers to startups sharing their tech stacks.&lt;/p&gt;

&lt;p&gt;So what is a stack, exactly?&lt;/p&gt;

&lt;p&gt;Before I proceed with the explanation, let me give a fair warning. This article is not advised for people who are on dieting.&lt;/p&gt;

&lt;p&gt;Food appears in many of our examples for simple reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, food is easier to visualize than complex codes &amp;amp; abstract symbols.&lt;/li&gt;
&lt;li&gt;Second, I want to provide you with a little distraction. They can get on your nerve at times, and a little distraction will help you keep your sanity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;** Prerequisite For This Article**  :&lt;/p&gt;

&lt;p&gt;The reader must be comfortable reading English and must be pro at controlling food cravings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What the heck is Stack?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Although the word has different meanings — in programming, the stack is a collection of objects(data) that are inserted( &lt;strong&gt;pushed&lt;/strong&gt; ) and removed( &lt;strong&gt;popped&lt;/strong&gt; ) according to the &lt;em&gt;last-in, first-out (&lt;/em&gt; &lt;strong&gt;&lt;em&gt;LIFO&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;) principle.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hear you say this was a bit complicated. Don’t worry, brace yourself for (food) example to simply this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TZR1LIfB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jshilpa.com/wp-content/uploads/2018/02/pringles-105x300.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TZR1LIfB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jshilpa.com/wp-content/uploads/2018/02/pringles-105x300.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pringles.com/uk/products.html"&gt;Source: Pringles Website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s talk about Pringles tube today. If you observe closely, Pringles tube is nothing but a stack of potato chips. The last chip to go in the tube during packaging is the first chip to go in our stomach while eating.&lt;/p&gt;

&lt;p&gt;You simply can’t have chips at the bottom of the potato tube before eating the chips present at the top as the tube has a single opening at the top.&lt;/p&gt;

&lt;p&gt;You see what I did there?&lt;/p&gt;

&lt;p&gt;Well, resisted the urge for having the Chips and taught you crucial concepts of Stacks.&lt;/p&gt;

&lt;p&gt;If you go back and see the definition of the stack earlier, you’ll see I have bolded few terms push, pop and LIFO. Let’s try to connect the dots now.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Additions (push) &amp;amp; Removals(pop)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just like we add(push) and remove chips(pop) from the top of the Pringles tube, in Stacks you add and remove just from one end i.e. from the top only.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Last item In is the First item Out (LIFO)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just like you can’t have chips at the bottom without finishing the upper ones, the item which was pushed in the stack last will be the first one to be taken out.&lt;/p&gt;

&lt;p&gt;The fundamental operations of Stack are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;push(element)&lt;/strong&gt; — Adding new elements at the top of the stack&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pop()&lt;/strong&gt;— Removing/Returning element from the top of the stacks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;peek()/top()&lt;/strong&gt; — Returns a reference to the top element of the stack&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;is_empty()&lt;/strong&gt; — Returns Boolean true is the stack has no elements&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;len(stack)&lt;/strong&gt;– Returns the number of elements in the stack.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yes, Yes…I hear you say I just explained first two methods to you.&lt;/p&gt;

&lt;p&gt;Let’s take another example to understand rest of them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VfyO__eU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AfS3azFCGe6k7tGUMoOdiSw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VfyO__eU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AfS3azFCGe6k7tGUMoOdiSw.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Say you have a stack of pancakes(an organized pile, one on top of another).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Peek/top :&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One should eat one pancake at a time. Any more than that at once, and you don’t get enough syrup on them.&lt;/p&gt;

&lt;p&gt;So, you’ll use peek/top function on the stack to get the topmost pancake.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Is_empty :&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am sure you are wondering who eats one pancake at a time. Together (stacked), with a layer of syrup applied to the top of each one is a proper way to eat this stack of cakes. Let’s say I ate the entire pancake in one go. Now, if someone asks me is there any pancake left, I’ll turn my head to say no.&lt;/p&gt;

&lt;p&gt;Similarly, this is_empty function tells us the status of elements in the stack. If the stack is empty, it will return true otherwise false.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;len(stack) :&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If I call this function on our pancake stack, it will merely return a number of pancakes (5 in our case).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And here we are. At the bottom of the stack.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two ways to implement a stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using &lt;a href="https://jshilpa.com/array-of-s-u-n-s-h-i-n-e/"&gt;array&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Using &lt;a href="https://jshilpa.com/seekers-guide-to-linked-list/"&gt;linked list&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like I mentioned at the beginning of the article, this article aimed at covering concepts of the stack. I will cover the code implementation with this data structures in future articles.&lt;/p&gt;

&lt;p&gt;That being said you can go ahead and get your bellies full now.&lt;/p&gt;

&lt;p&gt;See you soon!&lt;/p&gt;

&lt;p&gt;Thanks for reading! If you liked this post, you can check out &lt;a href="https://jshilpa.com"&gt;my other writings here&lt;/a&gt; or please consider &lt;a href="https://goo.gl/forms/QwZzdUPzEega5VvN2"&gt;entering your email&lt;/a&gt; here if you’d like to be added to my once-weekly email list or follow me on &lt;a href="https://twitter.com/JainShilpa26"&gt;Twitter&lt;/a&gt; &lt;/p&gt;


</description>
      <category>datastructures</category>
      <category>softwareengineering</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>The Ultimate Beginners Guide To Analysis of Algorithm</title>
      <dc:creator>Shilpa Jain</dc:creator>
      <pubDate>Tue, 06 Feb 2018 16:45:58 +0000</pubDate>
      <link>https://dev.to/jainroe/the-ultimate-beginners-guide-to-analysis-of-algorithm-84h</link>
      <guid>https://dev.to/jainroe/the-ultimate-beginners-guide-to-analysis-of-algorithm-84h</guid>
      <description>&lt;p&gt;The other day, I came across a post on StackOverflow which read “Is theoretical computer science(TCS) useful?”. I was completely caught off guard because, well, I was in the middle of a learning about asymptotic behavior complex math notations and couldn’t think of one use-case where I could apply it to my day-to-day coding. This post made me think “Why do I even need to know about &lt;a href="https://en.wikipedia.org/wiki/Theoretical_computer_science" rel="noopener noreferrer"&gt;theoretical CS&lt;/a&gt; if it is not used practically. Especially when my natural interests and abilities lie more in programming than in mathematical analysis.&lt;/p&gt;

&lt;p&gt;So over the weekend, I did some research. Why is this important? Is the scope of the concepts I am learning everyday restricted only to answering algorithms complexity interview questions? What about their application after that?&lt;/p&gt;

&lt;p&gt;There has to be more!&lt;/p&gt;

&lt;h3&gt;
  
  
  Is Theoretical Computer Science(TCS) useful?
&lt;/h3&gt;

&lt;p&gt;Big YES!&lt;/p&gt;

&lt;p&gt;TCS isn’t only about mathematical analysis or should be learned by programmers only for a job interview. If you plan to make a career as a programmer, your job will probably involve not just building coolest and most useful software today, but building it in a way that makes it useful and most efficient to use. You will also have to think about how and why what you are doing works, as well as in which bounds it operates.&lt;/p&gt;

&lt;p&gt;It’s like in the Karate Kid movie scene with Jackie Chan and Jaden Smith. Here, Jackie Chan tells Dre to put on and take off his jacket, over and over again.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/T10ycFr770g"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So, do you think Jackie was teaching him how to put on his jacket and take it off? NO! He was teaching him the skills necessary to become highly successful in kung-fu.&lt;/p&gt;

&lt;p&gt;This is what TCS does for us. It lays a solid foundation of fundamental abstract concepts which help us make the right choices as a developer.&lt;/p&gt;

&lt;p&gt;In this article, targeted at programmers who know all about coding but who don’t have any TCS background, I present to you one of the most important theoretical concepts of computer science: After reading this post, you will be able to understand all the common terms computer scientists use such as “algorithms”, “algorithm complexity analysis”, “Big Θ”, “asymptotic behavior” and “worst-case analysis”.&lt;/p&gt;

&lt;p&gt;This post does not have any mathematical prerequisites and I plan to build a firm basics background needed to study different algorithms with a firmer understanding of the theory behind them.&lt;/p&gt;

&lt;p&gt;Let’s get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A) What is an Algorithm?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An Algorithm can be defined as a list of steps that you can follow to complete a task.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might be thinking, well, that was a bouncer. Didn’t the title read something like a &lt;strong&gt;beginners&lt;/strong&gt; guide to study of algorithms?&lt;/p&gt;

&lt;p&gt;Okay! Let’s take an example to simplify the definition. Let’s say I want to shop for a book online. Read the definition again after going through the below example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BuyBook(bookname){
 1. Open Amazon
 2. Search for the bookname
 3. If in stock, add to the cart
 4. Proceed with checkout process
 5. Wait dearly for your book to be shipped
}

BuyBook('Into The Water')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, I want to buy a book named ‘Into The Water’ online. So, the first thing I did was visited Amazon. I searched on Amazon for my book, ‘Into the Water’ and found it was in stock. I proceeded with adding to the cart and with the final checkout process. Done!&lt;/p&gt;

&lt;p&gt;Can you try to connect the dots with the definition now?&lt;/p&gt;

&lt;p&gt;What I did above was followed a series of steps in order to achieve some goal(buying book in our case). This is what algorithm primary purpose is&lt;/p&gt;

&lt;p&gt;or see the below graphic! 😉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F500%2F1%2AD2Nx7mm3Q-OENTG_Vq_btA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F500%2F1%2AD2Nx7mm3Q-OENTG_Vq_btA.jpeg"&gt;&lt;/a&gt;Source: &lt;a href="https://1stwebdesigner.com/graphic-design-jokes/" rel="noopener noreferrer"&gt;1stWebDesigner&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay then.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;B) What is an analysis of algorithm? Why do we even have to analyze them?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Analysis of algorithms can be defined as a theoretical study of computer-program &lt;strong&gt;performance&lt;/strong&gt; and resource usage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, I’ve written word performance in above definition in bold words. Simply because our main focus throughout this article would be about computer program performance.&lt;/p&gt;

&lt;p&gt;But, before we start talking about it, can you think of any other thing more important than the performance in programming?&lt;/p&gt;

&lt;p&gt;Um, like the program maybe super-duper fast, but gives wrong output?&lt;/p&gt;

&lt;p&gt;Will this work? No!&lt;/p&gt;

&lt;p&gt;There are many things like correctness, simplicity, maintainability, robustness, security, functionality and most important user-friendliness which is way more important than the performance of the program.&lt;/p&gt;

&lt;p&gt;Example: &lt;a href="https://www.recode.net/2017/11/7/16620812/snapchat-evan-spiegel-redesign-users-q3-earnings" rel="noopener noreferrer"&gt;Snapchat CEO Evan Spiegel plans to redesign Snapchat.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Snapchat app works as it is supposed to be, but still, Evan Spiegel plans to redesign it. Because based on feedback, they found out the app was a little hard to understand and they plan to improve it by making it easier to use. Here, user-friendliness clearly outweighs algorithms.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So, clearly, performance lies at the bottom of the heap as compared to above -mentioned features. Then, why am I writing a post about performance then?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Because sometimes user-friendliness is directly related to performance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F491%2F1%2AvfbJLo5_iFw6ItRr9-jNtA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F491%2F1%2AvfbJLo5_iFw6ItRr9-jNtA.jpeg"&gt;&lt;/a&gt;I’ll just wait here. (Source:imgflip.com)&lt;/p&gt;

&lt;p&gt;Imagine sitting in front of the website which takes forever to load. Often, performance measures the line between feasible and in-feasibility. In real time setting, if an application is not fast enough, it is simply not functional. Or if it uses too much memory, it is not going to work for us.&lt;/p&gt;

&lt;p&gt;Armed with this knowledge, let’s try to analyze a simple problem of sorting:&lt;/p&gt;

&lt;p&gt;Now, we are going to use an algorithm called Bubble Sort. I’ll write this algorithm in pseudo-code(kind of programming language written in English)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bubblesort( var a as array )
    for i from 1 to N
        for j from 0 to N - i
           if a[j] &amp;gt; a[j + 1]
              swap( a[j], a[j + 1] )
end func
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the input will have a sequence of numbers and output will give a sorted list of numbers. The function Bubblesort accepts an array of data as input and will generate a sorted list of numbers.&lt;/p&gt;

&lt;p&gt;Difficult to comprehend? Let’s take a simple example&lt;/p&gt;

&lt;p&gt;Here our input to our BubbleSort function is {6, 5,3}. We want our algorithm to give us our desired output, sorted list like this {3,5,6}&lt;/p&gt;

&lt;p&gt;How our algorithms works:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Round 1:&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;# Our aim to to sort our list from smallest to largest. If we find numbers out of order, we swap them.

# Start with first element and compare it with next.
{ **6, 5** , 3}

# Compare 6 and 5. Since 6 &amp;gt; 5 -&amp;gt; Perform swap
{5, 6, 3}

# Now,look at second and third element and compare.
{5, **6, 3** }

# Compare 6 and 3. Since 6 &amp;gt; 3 -&amp;gt; Perform swap
{5, 3, 6}

# Round 1 output:{5, 3, 6}
# The list is still not sorted.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we start off by comparing first and second number, 6 and 5 in our case and swap them as they are out of order. We proceed with comparing 2nd and 3rd element,6 and 3 and swap them too as 3 is a smaller number than 6. We continue this till we reach the last element and we get a list {5,3,6} like this.&lt;/p&gt;

&lt;p&gt;The list is still not sorted and we continue with the similar approach till the entire list gets sorted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Round 2:&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;# First round gave us list {5, 3, 6}. We continue with our steps of comparing and swapping if we find elements out of order.

# Start with first element and compare it with next.
{ **5, 3** , 6}

# Compare 5 and 3. Since 5 &amp;gt; 3-&amp;gt; Perform swap
{3, 5, 6}

# Now,look at second and third element and compare.
{3, **5, 6** }

# Compare 5 and 6. Since 5 &amp;lt; 6 -&amp;gt; NO SWAPPING
{3, 5, 6}

# The list is sorted now. We got our desired output.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our List is finally sorted.&lt;/p&gt;

&lt;p&gt;Simple, right? (In case, you are finding it difficult to understand Bubble Sort or want to know more about it, &lt;a href="http://jshilpa.com/bubbleology%E2%80%8A-%E2%80%8Athe-study-of-bubble-sort/" rel="noopener noreferrer"&gt;I’ve covered all its nitty-gritty in this post&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C) Let’s analyze the above algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every developer has time running on their mind. How long will my program take to give me my desired output?&lt;/p&gt;

&lt;p&gt;But the running time of the algorithm is dependent on many things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Depends on Input :&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now let’s say the input list({3, 5 &lt;strong&gt;,&lt;/strong&gt; 6})given to our Bubble sort was already sorted. In this case, Bubble sort doesn’t have much to do. But, what if the input list(6,5,3) is reverse sorted. Bubble sort will have to do a lot of work to give us a sorted list as each element needs to be swapped.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Depends on the Input size&lt;/strong&gt; :&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now imagine sorting a list with 6 elements vs a list having 6 * 10⁹ elements. Which program will give us a faster-sorted list?&lt;/p&gt;

&lt;p&gt;So, when we talk about analyzing an algorithm with running time, we generally look at the &lt;em&gt;upper bound&lt;/em&gt;. For example, If I say this algorithm will not take more than 3 seconds to execute, we have a max limit and we know how this will work in real life setting. This upper bound gives a guarantee to the user that time taken to accomplish this task will be no more than this amount.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Different Type of Analysis Done&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;In analyzing an algorithm, rather than a piece of code, we will try and predict the number of times &lt;em&gt;“the principle activity”&lt;/em&gt; of that algorithm is performed. For example, if we are analyzing sorting algorithm like Bubble Sort, we might count the number of comparisons performed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Worst case (Done usually)&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the worst case analysis, we calculate upper bound on running time of an algorithm. It is generally a case that causes a maximum number of operations to be executed over all inputs of size n. For example, in Bubble sort, a maximum number of comparisons takes place when the array list is reverse sorted.&lt;/p&gt;

&lt;p&gt;2) &lt;strong&gt;Average case:&lt;/strong&gt; ( &lt;strong&gt;Sometimes done&lt;/strong&gt; )&lt;/p&gt;

&lt;p&gt;Arguably, average case is the most useful measure. Unfortunately, this is typically a difficult thing to measure. In average case analysis, we take all possible inputs and calculate computing time for all of the inputs. Sum all the calculated values and divide the sum by a total number of inputs. We must know (or predict) distribution of cases throughout all data sets of size n.&lt;/p&gt;

&lt;p&gt;3) &lt;strong&gt;Best Case : (Not Generally Used)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the best case analysis, we calculate lower bound on running time of an algorithm. It is a case that causes a minimum number of operations to be executed from an input of size n. For example, we might get the best behavior from Bubble sort algorithm if the input to it is already sorted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Algorithm Complexity:
&lt;/h3&gt;

&lt;p&gt;So, let’s try to calculate Bubble Sort worst-case time? Let’s try to formally measure how fast this algorithm runs.&lt;/p&gt;

&lt;p&gt;But where should I run this algorithm? Your computer or mine? Wait, if I run it on a supercomputer, it will run super-duper fast.&lt;/p&gt;

&lt;p&gt;BIG PROBLEM!&lt;/p&gt;

&lt;p&gt;We clearly need something which compares two algorithms at the idea level ignoring low-level details such as the implementation programming language, the hardware the algorithm runs on etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Showtime! Asymptotic Analysis!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here, we ignore machine dependent constants and instead of looking at the actual running time look at the growth of running time.&lt;/p&gt;

&lt;p&gt;Wait! Whaaaaaat?&lt;/p&gt;

&lt;p&gt;Trust me read this definition again after going through the below example.&lt;/p&gt;

&lt;p&gt;The following 3 asymptotic notations are mostly used to represent time complexity of algorithms.&lt;/p&gt;

&lt;p&gt;1) Θ Notation (Theta notation)&lt;/p&gt;

&lt;p&gt;2) Big O Notation&lt;/p&gt;

&lt;p&gt;3) Ω Notation (Omega notation)&lt;/p&gt;

&lt;p&gt;Let’s take a look at Θ Notation to understand how it solves our problem.&lt;br&gt;&lt;br&gt;
The Θ Notation notation follows simple two rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Drop low order terms&lt;/li&gt;
&lt;li&gt;Ignore leading constants.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Consider this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3n³ + 40n² - 10n + 409

# Let's look at Θ Notation first point, drop lower order terms
# Here, n^3 is a bigger term then n2. So we drop all lower order n terms and 409

# Now, our equation looks like 3n³. Let's look at second point now. Ignore leading constants. So, we drop 3 too.

#so, we finally get n³

3n³ + 40n² - 10n + 409 = Θ(n³) #Theta of n cube

# This is an engineering way of manipulating theta notation. 
# There is a mathematical way too
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;We call this function, i.e. what we put within Θ( here ), the time complexity or just complexity of our algorithm.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As n approaches infinity, Θ(n²) will always beat Θ(n³). It doesn’t matter what lower order terms or leading constants were. Even if you run Θ(n²) algorithm on slower computer and Θ(n³) algorithm on a faster computer, when we look at the growth, as the size of n increases, Θ(n²) algorithm will be faster.&lt;/p&gt;

&lt;p&gt;In complexity analysis, we only care about how many times our the &lt;em&gt;principle activity&lt;/em&gt; of our algorithm is performed as the program input (n) grows large. This is in line with our “worst-case scenario” behavior. If algo1 beats algo2 for a very large input number n in time, it’s obvious that it will do so even when the size of input n is small. So, considering this, we’ll drop all the terms that grow slowly and only keep the ones that grow fast as n becomes larger.&lt;br&gt;&lt;br&gt;
This filter of “dropping all factors” and of “keeping the largest growing term” as described above is what we call &lt;em&gt;asymptotic behavior&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Oh Boy! How Big This Post Is!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just concluding it. I have introduced two more notations Big O and Omega Notation in above section. Big O is highly popular as it deals with upper bound of an algorithm(Remember our worst-case time complexity). &lt;a href="http://jshilpa.com/the-ultimate-guide-to-big-o-notation-learning-through-examples/" rel="noopener noreferrer"&gt;I’ve covered it in another post, do have a look&lt;/a&gt;!. (It isn’t as long as this post, I promise!)&lt;/p&gt;

&lt;h3&gt;
  
  
  Closing Notes:
&lt;/h3&gt;

&lt;p&gt;Thanks for reading! After reading this post,(I hope)you understand terms like “algorithms”, “algorithm complexity analysis”, “Big Θ”, “asymptotic behavior” and “worst-case analysis”.&lt;/p&gt;

&lt;p&gt;I publish things I learn daily  &lt;a href="http://jshilpa.com" rel="noopener noreferrer"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt; or you can follow me on &lt;a href="https://twitter.com/JainShilpa26" rel="noopener noreferrer"&gt;&lt;strong&gt;Twitter&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Resources Used For This Article:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/analysis-of-algorithms-set-1-asymptotic-analysis/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/analysis-of-algorithms-set-1-asymptotic-analysis/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://discrete.gr/complexity/" rel="noopener noreferrer"&gt;http://discrete.gr/complexity/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=JPyuH4qXLZ0" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=JPyuH4qXLZ0&lt;/a&gt;&lt;/p&gt;




</description>
      <category>softwareengineering</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Big-O Notation — Learning Through Examples</title>
      <dc:creator>Shilpa Jain</dc:creator>
      <pubDate>Mon, 05 Feb 2018 15:11:18 +0000</pubDate>
      <link>https://dev.to/jainroe/the-ultimate-guide-to-big-o-notation--learning-through-examples-5ecp</link>
      <guid>https://dev.to/jainroe/the-ultimate-guide-to-big-o-notation--learning-through-examples-5ecp</guid>
      <description>&lt;p&gt;Big O notation is used to describe or calculate time complexity (worst-case performance)of an algorithm. This post will show concrete examples of Big O notation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F652%2F1%2APeVXl5ZQ0rSmDik86Zlt2g.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F652%2F1%2APeVXl5ZQ0rSmDik86Zlt2g.jpeg"&gt;&lt;/a&gt;After discovering that complexity of the algorithm won’t be taken into consideration on the exam. (Source: &lt;a href="https://www.reddit.com/r/ProgrammerHumor/comments/7jty1d/after_discovering_that_complexicity_of_the/" rel="noopener noreferrer"&gt;Reddit&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A few things to note&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This article was written with intermediate developers in mind and assumes you already know a bit about time complexity, worst case behavior.&lt;/li&gt;
&lt;li&gt;If this word doesn’t ring a bell, &lt;a href="https://jshilpa.com/the-ultimate-beginners-guide-to-analysis-of-algorithm/" rel="noopener noreferrer"&gt;I wrote a comprehensive guide here (free article).&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Real-Life Big-O
&lt;/h3&gt;

&lt;p&gt;Many “operations” in real life can help us with finding what the order is. When analyzing algorithms/operations, we often consider the worst-case scenario. What’s the worst that can happen to our algorithm and when does our algorithm need the most instructions to complete the execution? Big O notation will always assume the upper limit where the algorithm will perform the maximum number of iterations.&lt;/p&gt;

&lt;p&gt;Let’s assume I am standing in the front of a class of students and one of them has my bag. Here are few scenarios and ways in which I can find my bag and their corresponding order of notation.&lt;/p&gt;

&lt;h3&gt;
  
  
  O(n) — Linear Time:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; Only one student in my class who hid my bag knows about it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach:&lt;/strong&gt; I will have to ask each student individually in the class if they have my bag. If they don’t, I move on to ask the next student.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Worst-Case Scenario:&lt;/strong&gt; In the worst case scenario, I will have to ask &lt;em&gt;n&lt;/em&gt; questions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Show me the code!&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Here, given an input of size n, the number of steps required is directly related to the amount of data it is processing.&lt;/li&gt;
&lt;li&gt;Single for loops, linear search are examples of linear time&lt;/li&gt;
&lt;li&gt;In above example, an array size/input size increases, time to find desired value also increases.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  O(1) — Constant Time
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; Student who hid my bag name is known to me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach&lt;/strong&gt; : Since I know &lt;em&gt;Joe&lt;/em&gt; has my bag, I’ll directly ask him to give it to me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Show me the code!&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Here, given an input of size n, it only takes a one step for the algorithm to accomplish the task.&lt;/li&gt;
&lt;li&gt;An algorithm takes same time(constant time) to execute irrespective of the amount of data.&lt;/li&gt;
&lt;li&gt;This algorithm is not affected by the array size either.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  O(n²) — Quadratic Time:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; In the entire class, only one student knows on which student desk my bag is hidden.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach:&lt;/strong&gt; I will start questioning each student individually and ask him about all the others students too. If I don’t get the answer from the first student, I will move on to the next one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Worst-Case Scenario:&lt;/strong&gt; In the worst case scenario, I will have to ask &lt;em&gt;n2&lt;/em&gt; questions, questioning each student about other students as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Show me the code!&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Here, given an input of size n, the number of steps it takes to accomplish a task is square of n.&lt;/li&gt;
&lt;li&gt;Each pass to outer loop O(n) requires going through entire list through the inner-loop.&lt;/li&gt;
&lt;li&gt;Nested for-loops are example of quadratic time as we run a linear operation within other linear operation&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  O(log n) — Logarithmic time:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; Here, all the students know who has my bag but will only tell me if I guessed the right name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach:&lt;/strong&gt; I will divide the class in half, then ask: “Is my bag on the left side, or the right side of the class?” Then I take that group and divide it into two and ask again, and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Worst Case Scenario:&lt;/strong&gt; In the worst case, I will have to ask &lt;em&gt;log n&lt;/em&gt; questions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Show me the code!&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Here, given an input of size n, the number of steps it takes to accomplish the task are decreased by roughly 50% each time through the algorithm.&lt;/li&gt;
&lt;li&gt;O (log N) algorithms are very efficient because increasing amount of data has little effect at some point early on because the amount of data is halved on each run through.&lt;/li&gt;
&lt;li&gt;Binary search is a perfect example of this.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Closing Notes:
&lt;/h3&gt;

&lt;p&gt;Thanks for reading! I publish things I learn daily every &lt;a href="https://jshilpa.com" rel="noopener noreferrer"&gt;&lt;strong&gt;single weekday here&lt;/strong&gt;&lt;/a&gt; or you can follow me on &lt;a href="https://twitter.com/JainShilpa26" rel="noopener noreferrer"&gt;&lt;strong&gt;Twitter&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are some helpful resources to learn more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Time_complexity" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://bigocheatsheet.com/" rel="noopener noreferrer"&gt;Big O Cheat Sheet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>coding</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
