<?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: Bhav Kushwaha</title>
    <description>The latest articles on DEV Community by Bhav Kushwaha (@bhavkushwaha).</description>
    <link>https://dev.to/bhavkushwaha</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%2F1007413%2F9d9e9d51-3eb3-43bc-9502-e443a29f5f11.jpeg</url>
      <title>DEV Community: Bhav Kushwaha</title>
      <link>https://dev.to/bhavkushwaha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhavkushwaha"/>
    <language>en</language>
    <item>
      <title>QuickSort - Time Analysis (Part2)</title>
      <dc:creator>Bhav Kushwaha</dc:creator>
      <pubDate>Fri, 29 Sep 2023 14:26:16 +0000</pubDate>
      <link>https://dev.to/bhavkushwaha/mergesort-vs-quicksort-3o05</link>
      <guid>https://dev.to/bhavkushwaha/mergesort-vs-quicksort-3o05</guid>
      <description>&lt;h2&gt;
  
  
  Stating the Fundamentals
&lt;/h2&gt;

&lt;p&gt;1) Suppose we have a simple function to print every item in a list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def print_items(list):
 for items in list:
  print item
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function goes through every item in the list and prints out.&lt;/p&gt;

&lt;p&gt;2) Now consider the same function but with a 1 second sleep() associated with every iteration in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from time import sleep
def print_items2(list):
 for items in list:
  sleep(1)
  print item
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Key points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Both functions loop through the list once, so theoriticaly they both have O(n) time complexity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But the print_items() is evidently faster than the latter since it doesn't contains the sleep function and is executed faster.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So even though both functions are the same speed in Big O notation like O(n), print_items is practically faster.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From the above example we gather:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When we write Big O notation like O(n), it really means this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ryc6r3fe3eku6u4z5jl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ryc6r3fe3eku6u4z5jl.png" alt="bigO" width="258" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;c is some fixed amount of time that your algorithm takes, called constant.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Example, it might be &lt;strong&gt;10 ms&lt;/strong&gt; for print_items and &lt;strong&gt;1 sec&lt;/strong&gt; for print_items2.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example of Binary Search vs Simple Search
&lt;/h2&gt;

&lt;p&gt;Suppose both the algorithms had the following constants:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljyuh64ywa617w2nvp6a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljyuh64ywa617w2nvp6a.png" alt="BSearchvsSSearch" width="410" height="80"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might assume Simple Search is way faster due to a very low constant value than Binary Search.&lt;/p&gt;

&lt;p&gt;But now considering a list of &lt;strong&gt;4 Billion&lt;/strong&gt; Elements!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgutcje2jj5zv76iefvs7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgutcje2jj5zv76iefvs7.png" alt="4Billion" width="500" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE :&lt;/strong&gt; Here the constant c doesn't get considered and Binary Search is proved to be more faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  QuickSort vs MergeSort Case
&lt;/h2&gt;

&lt;p&gt;In this case &lt;strong&gt;the constant&lt;/strong&gt; associated with the time complexity plays an important role is determining the better performing Sorting Algorithm.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Quicksort has a &lt;strong&gt;smaller constant&lt;/strong&gt; than Mergesort!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sof if they're both O(n) time, then &lt;strong&gt;quicksort is faster&lt;/strong&gt;!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And Quicksort is faster in practice too, since it &lt;strong&gt;hits the average case way more often&lt;/strong&gt; than the worst case.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Different Cases of Quicksort
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Worst Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The performance of quicksort heavily depends on the pivot you choose.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Suppose you always choose the first element as the pivot, and you call a quicksort on an array that is already sorted.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fswww252nokkv7381s5sn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fswww252nokkv7381s5sn.png" alt="WorstCase" width="590" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Here, quicksort doesn't check whether the array is sorted or not, it will try to sort it again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stack Size in Worst Case: O(n)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now instrad, suppose you always picked the middle element as the pivot. Look at the call stack now:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6zzhq8vb0oq9fp8oqmw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6zzhq8vb0oq9fp8oqmw.png" alt="middleElement" width="500" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It's so short! Because you divided the array in half everytime, you don't need to make as many recursive calls. You hit the base case sooner and the call stack is much shorter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stack Size in Best Case: O(logn)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Average Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now take any element as the pivot and the rest of elements are divided in sub-arrays. You will get the result exactly same as the best case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stack Size in Average Case: O(logn)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Something about the Call Stack at every level
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The first operation takes O(n) time since you touched all 8 elements on this level of stack. But actually you touch O(n) elements on every level of the call stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk1lw55k9cgi4l62qq00n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk1lw55k9cgi4l62qq00n.jpg" alt="stacksize" width="500" height="612"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Even if you partition the array differently, you still are touching O(n) elements everytime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxvg2wl6whbdz4ym1f3jl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxvg2wl6whbdz4ym1f3jl.jpg" alt="O(n)" width="590" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So each level takes O(n) time to complete.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We conclude with the above findings that the &lt;strong&gt;Height of Call Stack is O(logn)&lt;/strong&gt; and &lt;strong&gt;each level takes O(n)&lt;/strong&gt; time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39odo19dinoc1wvbniix.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39odo19dinoc1wvbniix.jpg" alt="Conclusion" width="590" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The entire Quicksort Algorithm will take:
&lt;code&gt;O(n) * O(nlogn) = O(nlogn)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Time Complexity of the Best-Case &amp;amp; Average-Case of Quicksort.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the Worst-Case, there are O(n) levels, so the algorithm takes &lt;code&gt;O(n) * O(n) = O(n^2)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you always choose a random element in the array as the pivot, quicksort will complete in O(nlogn) time on average.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quicksort is one of the fastest sorting algorithms out there!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>timecomplexity</category>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>sorting</category>
    </item>
    <item>
      <title>Quicksort (Grokking Algorithms)</title>
      <dc:creator>Bhav Kushwaha</dc:creator>
      <pubDate>Fri, 29 Sep 2023 07:02:48 +0000</pubDate>
      <link>https://dev.to/bhavkushwaha/quicksort-grokking-algorithms-42ef</link>
      <guid>https://dev.to/bhavkushwaha/quicksort-grokking-algorithms-42ef</guid>
      <description>&lt;p&gt;Quicksort  is a sorting algorithm which is frequently used in real life.&lt;/p&gt;

&lt;p&gt;Let's explore different senarios where we can implement Quicksort:-&lt;/p&gt;

&lt;h2&gt;
  
  
  Array Containing 0 or 1 Element
&lt;/h2&gt;

&lt;p&gt;Empty arrays and single element array will not need to be sorted. The base case will be the result for them.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiwh2h99w0jlihuizbwqq.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiwh2h99w0jlihuizbwqq.png" alt="0 Array &amp;amp; 1 Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Array Containing 2 Elements
&lt;/h2&gt;

&lt;p&gt;An array containing two Elements is also easy to sort.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7jxl0v6a2prxqpyqcb0.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7jxl0v6a2prxqpyqcb0.png" alt="2 Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Array Containing 3 Elements
&lt;/h2&gt;

&lt;p&gt;In order to sort an array containing three elements, we need to break it down to the base case using the Divide &amp;amp; Conquer approach.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8lgvyhuga46wxjt2yp5.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8lgvyhuga46wxjt2yp5.png" alt="3 Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First pickup an element from the array, called the &lt;strong&gt;pivot&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(Will discuss how to pickup a good pivot later, for now lets take the first element as pivot.)&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6bckbn415ozovgyghyy.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6bckbn415ozovgyghyy.png" alt="pivot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now find the elements smaller than the pivot and the elements larger than the pivot.&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb6aqx14of9nt2dn9k5il.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb6aqx14of9nt2dn9k5il.png" alt="partioning"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;Partioning&lt;/strong&gt;. You have :&lt;br&gt;
1) A sub-array of all the numbers less than the pivot&lt;br&gt;
2) The pivot&lt;br&gt;
3) A sub-array of all the numbers more than the pivot&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The two two sub-arrays obtained are not neccessarily sorted.
Therefore, we come come across 2 cases:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;1) If the &lt;strong&gt;sub-arrays are sorted&lt;/strong&gt; then we can combine the whole thing&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;left array + pivot + right array = Sorted Array&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;2) If the &lt;strong&gt;sub-arrays are not sorted&lt;/strong&gt; then we have to sort them.&lt;/p&gt;

&lt;p&gt;We can do that by applying quicksort on the sub-arrays (since we know the results of sorting an array containing 0 or 1 elements or 2 elements array).&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;quicksort([15, 10]) + [33] + quicksort([])&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;&amp;gt; [10, 15, 33]&lt;/code&gt;&lt;br&gt;
&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9o8odjgx4op0bdvyf6ce.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9o8odjgx4op0bdvyf6ce.png" alt="sorted 3 Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similary, we can choose any element of the array as pivot and apply the above steps to obtain a sorted array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Array Containing 4 Elements
&lt;/h2&gt;

&lt;p&gt;Suppose we are given an array&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fem8yta6znqi6cw77y2pu.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fem8yta6znqi6cw77y2pu.png" alt="4 Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We choose &lt;strong&gt;33&lt;/strong&gt; as the pivot element.&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vv2s9cvlob7mwzjq4jk.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vv2s9cvlob7mwzjq4jk.jpg" alt="pivot33"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The left sub-array has 3 elements. We had already seen how to sort an array of three elements: call quicksort on it recursively.&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6zurmxdouw6lwcdhz6o.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6zurmxdouw6lwcdhz6o.png" alt="sub-array sort"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Therefore, we can now easily repeat the steps learned above to sort the 4 elements array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Sorting an array of 5 elements (Different Pivots)
&lt;/h2&gt;

&lt;p&gt;Given Array :&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6oxbt04mj8noz6sh5c8.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6oxbt04mj8noz6sh5c8.png" alt="5 Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Different Pivot Elements :&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hf73yrhimp5al650ue2.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hf73yrhimp5al650ue2.jpg" alt="Different Pivots"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notice:&lt;/strong&gt; all the sub-arrays of any pivot lies between 0-4 elements, and we can easily sort them as seen in the above examples.&lt;/p&gt;

&lt;p&gt;Example 1)&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0tmzhcs7lglow0mda9e5.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0tmzhcs7lglow0mda9e5.jpg" alt="Example 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example 2)&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhby6puxypkc9jewabtke.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhby6puxypkc9jewabtke.jpg" alt="Example 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Code for Quicksort
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def quicksort(array):

 if len(array) &amp;lt; 2:
  return array
  # Base Case: arrays with 0 or 1 elements

 else:
  pivot = array[0]
  # Recursive Case

  less = [ i for i in array [1:] if i &amp;lt;= pivot] 
  # Sub-array to the left of the pivot

  greater = [ i for i in array [1:] if i &amp;gt; pivot] 
  # Sub-array to the right of the pivot

  return quicksort(less) + [pivot] + quicksort(greater)


print quicksort([10, 5, 2, 3])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>computerscience</category>
      <category>quicksort</category>
      <category>sorting</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Divide &amp; Conquer (Grokking Algorithms)</title>
      <dc:creator>Bhav Kushwaha</dc:creator>
      <pubDate>Tue, 29 Aug 2023 14:25:04 +0000</pubDate>
      <link>https://dev.to/bhavkushwaha/divide-conquer-grokking-algorithms-7gi</link>
      <guid>https://dev.to/bhavkushwaha/divide-conquer-grokking-algorithms-7gi</guid>
      <description>&lt;p&gt;In this article we will use 2 Examples to explain the concept of Divide &amp;amp; Conquer.&lt;/p&gt;

&lt;p&gt;The First Example will be a visual one and the latter one will be a code one.&lt;/p&gt;

&lt;h2&gt;
  
  
  EXAMPLE 1
&lt;/h2&gt;

&lt;p&gt;Suppose you are a farmer with a plot of land. You want to divide this farms evenly into square plots. You want the plots to be as big as possible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zZ-QQOsT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ofy9vwaq7hbppxfzfk0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zZ-QQOsT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ofy9vwaq7hbppxfzfk0.jpg" alt="1680 m x 640 m Farmland" width="457" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following Constraints are present:-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zXahALZe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xciro894i0eoqc6fci6r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zXahALZe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xciro894i0eoqc6fci6r.png" alt="Constraints" width="500" height="158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here to solve the farmer's problem, we will use &lt;strong&gt;Divide &amp;amp; Conquer&lt;/strong&gt; Strategy!&lt;/p&gt;

&lt;p&gt;The Divide &amp;amp; Conquer Strategy consists of two cases:-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Base Case&lt;/strong&gt; : The simplest possible case!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Recursive Case&lt;/strong&gt; : Divid eor decrease your problem until it becomes the Base Case.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now solving the Farmer's problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's start by marking out the bigest boxes you can use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GPzKsVzc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr63vnxkcuk4yy5qfms9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GPzKsVzc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr63vnxkcuk4yy5qfms9.png" alt="640x400" width="600" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can fit two 640x640 boxes in there and some land is still left to be divided!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, apply the same algorithm to the remaining land!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The biggest box we can create is 400 x 400 m with 400 x 240 remaining area.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QQGA6thE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1jrf8q005vjvm1le5si.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QQGA6thE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1jrf8q005vjvm1le5si.png" alt="400x240" width="500" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now mark a box to get an even smaller segment. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jRWghfQz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbh4xkrmdsue19x3vswp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jRWghfQz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbh4xkrmdsue19x3vswp.png" alt="240x160" width="500" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Going for an even smaller box!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bKSPkG21--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zienycsqlc0cmu4m6ohl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bKSPkG21--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zienycsqlc0cmu4m6ohl.png" alt="160x80" width="500" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hey Look! we encountered the &lt;strong&gt;Base Case&lt;/strong&gt; !&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4VcJK9D9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mzyrt3scomxtxg46jyf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4VcJK9D9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mzyrt3scomxtxg46jyf.png" alt="80x80" width="400" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So for the original farm, the biggest plot size you can use is 80 x 80 m.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UU7WC3cn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udnz7ddjw124xzwb5luc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UU7WC3cn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udnz7ddjw124xzwb5luc.png" alt="final" width="500" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  EXAMPLE 2
&lt;/h2&gt;

&lt;p&gt;You are given an array of numbers. You have to add all the numbers and return the total.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Qmcc8bu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psd9lywckh6ekbat2n6p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Qmcc8bu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psd9lywckh6ekbat2n6p.png" alt="array" width="198" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's pretty easy to do it with a loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def sum(arr):
 total = 0
 for x in arr:
  total += x
 return total
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the challenge is to do it using Recursion!&lt;/p&gt;

&lt;p&gt;Follow these steps to solve the problem:-&lt;/p&gt;

&lt;p&gt;1 ) Figure out the &lt;strong&gt;Base Case&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Think about the simplest case. Here, if we get an array with 0 or 1 element then thats pretty easy to sum up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XiyJLwQs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gql05npg0f7n3n7b6qpz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XiyJLwQs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gql05npg0f7n3n7b6qpz.png" alt="base case" width="430" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2 ) We need to move more closer to an empty array wit hevery &lt;strong&gt;recursive call&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3MLeddft--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9dqxzgokmuwrhffkv3u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3MLeddft--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9dqxzgokmuwrhffkv3u.png" alt="recursive case 1" width="355" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kqXOdL0y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amaminraijmvajezoa5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kqXOdL0y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amaminraijmvajezoa5t.png" alt="recursive case 2" width="500" height="58"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3 ) Now the &lt;strong&gt;Sum&lt;/strong&gt; Function Works :-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's Principal :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N-dZk5oh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qx4brwr94woaonespvba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N-dZk5oh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qx4brwr94woaonespvba.png" alt="principal" width="500" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sum Function in Action and Recursion taking place :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qD2mtNv6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esgeemodkao35wo7x06d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qD2mtNv6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esgeemodkao35wo7x06d.png" alt="Final" width="500" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; When we are writing a recursive function involving an array, the base case is often an empty array with one element.&lt;/p&gt;

</description>
      <category>divideandconquer</category>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Chapter 3 : Recursion (Grokking Algorithms)</title>
      <dc:creator>Bhav Kushwaha</dc:creator>
      <pubDate>Wed, 26 Jul 2023 05:33:47 +0000</pubDate>
      <link>https://dev.to/bhavkushwaha/chapter-3-recursion-grokking-algorithms-4k3j</link>
      <guid>https://dev.to/bhavkushwaha/chapter-3-recursion-grokking-algorithms-4k3j</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yjpj5xvE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09y8jd7fby1eem3xcpcd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yjpj5xvE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09y8jd7fby1eem3xcpcd.jpg" alt="Cover Image" width="342" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Topics covered in Chapter 3 : Recursion
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is Recursion?&lt;/li&gt;
&lt;li&gt;Base Case &amp;amp; Recursive Case&lt;/li&gt;
&lt;li&gt;The Stack&lt;/li&gt;
&lt;li&gt;Call Stack&lt;/li&gt;
&lt;li&gt;The Call Stack with Recursion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is Recursion?
&lt;/h2&gt;

&lt;p&gt;It is Explained using an Example of Grandma's Boxes &amp;amp; Key.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You want to open your Grandma's suitcase but the key to it is in this Box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But this box contains more boxes inside those boxes. And the key is in a box somewhere.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here we can use 2 Algorithms to find the key:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using Loops&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using Recursion&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PGKXw7nu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23lhtrakqp11mclkeb5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PGKXw7nu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23lhtrakqp11mclkeb5o.png" alt="An Image have two different approaches for solving the same box-key problem" width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Important Points to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is no 100% guaranteed performance benefit of using recursion, infact sometimes loops perform better.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Base Case &amp;amp; Recursive Case
&lt;/h2&gt;

&lt;p&gt;Every Recursive Function has two parts:-&lt;/p&gt;

&lt;p&gt;Base Case&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here, the function doesn't call itself and thus this case prevents a chance of infinite loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Recursive Case&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here, the function calls itself again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u7UlSg5P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mh9j5ez1c8rzahn5ei82.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u7UlSg5P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mh9j5ez1c8rzahn5ei82.png" alt="Base Case vs Recursive Case in a Function" width="295" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Stack
&lt;/h2&gt;

&lt;p&gt;The best example to explain a stack is a Todo-List.&lt;br&gt;
It enables two features : 1. Push() &amp;amp; 2. Pop()&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GleZNlJC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5d7zthz3gofx7f64z430.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GleZNlJC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5d7zthz3gofx7f64z430.jpg" alt="Push &amp;amp; Pop in a stack" width="500" height="216"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Call Stack
&lt;/h2&gt;

&lt;p&gt;Our computer uses a stack internally called the &lt;strong&gt;Call Stack&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def greet(name):
    print "hello, " + name + "!"
    greet2(name)
    print "getting ready to say bye..."
    bye()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function calls 2 other functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def greet2(name):
    print "how are you, " + name + "?"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def bye():
    print "ok bye!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visually the above code is implemented:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--53ifsAaT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6aq6hokkwaytd32li1c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--53ifsAaT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6aq6hokkwaytd32li1c.jpg" alt="Greet Function() called" width="255" height="114"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9oTpur0D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ul3ttmkw1f3l2wy3rn0c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9oTpur0D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ul3ttmkw1f3l2wy3rn0c.jpg" alt="Greet2 Function() called" width="458" height="195"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wp62SEkr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jsxk3odtr5nnla6p1vb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wp62SEkr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jsxk3odtr5nnla6p1vb.jpg" alt="Greet2 is executed &amp;amp; then poped" width="305" height="165"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LAoCpBfs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xszf3wol21qqwceiiv9p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LAoCpBfs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xszf3wol21qqwceiiv9p.jpg" alt="Bye Function() is called" width="242" height="149"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kFa1Lu4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drt4s4cdj6br6fe6ynq6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kFa1Lu4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drt4s4cdj6br6fe6ynq6.jpg" alt="Bye is executed &amp;amp; then poped" width="285" height="149"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Explaination: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firstly Greet Function is called&lt;/li&gt;
&lt;li&gt;Secondly Greet2 is called&lt;/li&gt;
&lt;li&gt;Greet2 is executed and poped&lt;/li&gt;
&lt;li&gt;Bye Function is called&lt;/li&gt;
&lt;li&gt;Bye is executed and poped&lt;/li&gt;
&lt;li&gt;In the end, Greet is returned too as there's nothing else to be done.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Call Stack with Recursion
&lt;/h2&gt;

&lt;p&gt;Explained by an Example - Factorial of a Number&lt;/p&gt;

&lt;p&gt;The Code for Factorial :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def fact(x):
 if x==1:
  return 1
 else:
  return x * fact(x-1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kVFlQ-yg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpm21d98z7wt2aiotgef.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kVFlQ-yg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpm21d98z7wt2aiotgef.jpg" alt="Factorial - Recursive Function implemented using a call stack" width="590" height="784"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using Stack is convenient but there's a cost; saving all that info can take up a lot of memory. Your Stack becomes too tall!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It can be solved using 1. Loops instead or 2. Tail Recursion&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Suppose we write a recursive function that runs forever, then the program will run out of space &amp;amp; it will exit with a &lt;strong&gt;Stack-Overflow Error&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>recursion</category>
    </item>
  </channel>
</rss>
