<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Heyder Shukurov on Medium]]></title>
        <description><![CDATA[Stories by Heyder Shukurov on Medium]]></description>
        <link>https://medium.com/@heydershukurov?source=rss-325da1e0acbc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*fcRPYFe1mfKkmBZY.</url>
            <title>Stories by Heyder Shukurov on Medium</title>
            <link>https://medium.com/@heydershukurov?source=rss-325da1e0acbc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 17:16:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@heydershukurov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Insertion sort algorithm realization]]></title>
            <link>https://medium.com/@heydershukurov/insertion-sort-algorithm-realization-1456c80660ef?source=rss-325da1e0acbc------2</link>
            <guid isPermaLink="false">https://medium.com/p/1456c80660ef</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[insertion-sort]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <dc:creator><![CDATA[Heyder Shukurov]]></dc:creator>
            <pubDate>Sun, 21 Oct 2018 17:49:03 GMT</pubDate>
            <atom:updated>2018-10-21T17:49:03.174Z</atom:updated>
            <content:encoded><![CDATA[<p>Insertion Sort algorithm is one of the common known sorting algorithms.<br>Using this algorithm you can sort all types of arrays. Algorithm is based on simple card sorting method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*OASOnBWkECLk_iRFiw1O0Q.jpeg" /><figcaption>Card sorting</figcaption></figure><p>We start with an empty left hand and the cards face down on the table. We then remove one card at a time from the table and insert it into the correct position in the left hand. To find the correct position for a card, we compare it with each of the cards already in the hand, from right to left</p><p>If we translate this method to the computer code we can sort any type of array.</p><p>The logic is quite simple.<br>Let’s do it step by step. Imagine we have array of numbers like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*tkMp7FbXBgb9JTUFoqC8Sg.jpeg" /><figcaption>Simple Array of numbers</figcaption></figure><p>Step 1:<br>The first number in the array is 7. Lets try to get another card the second card for comparing with first one.<br>If the second card is less than first one then make it first like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*TbMXSfXXNmbqFgKjn2tu8Q.jpeg" /><figcaption>First and second elements are sorted</figcaption></figure><p>Step 2: <br>Now let’s move on. We have sorted first and second cards. It’s time to settle second and third one. Operation is the same except we must sort not only 2 cards but also the cards which we have already sorted previously like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*ie8ouEAzVfuMCr-yptzTVw.jpeg" /><figcaption>Second and third elements are sorted</figcaption></figure><p>Step 3:<br>In the illustration above we can see the base process. Compare “n” with “n+1” if “n+1” is lesser than “n” flip their place and check previously flipped ones with “n” after controlling all the elements your array will look like illustration below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*eLAiIitie-sW6wRoOkZPVw.jpeg" /></figure><p>After knowing the basic concept of algorithm we can write our code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2e4b3b95f1daf4e5fc53fc28e910c07/href">https://medium.com/media/c2e4b3b95f1daf4e5fc53fc28e910c07/href</a></iframe><p>Step 1:<br>In the line 1 we have declared array of numbers;</p><p>Step 2: <br>In the line 2–10 we have loop over every elements of array.</p><p>Step 3:<br>First index is 1 and the element corresponding to index 1 is 6 (Remember array indexing starting with 0).<br>In the line 3 we have declared variable element and assigned the number corresponding to the “array[index]” =&gt; “6”.</p><p>Step 4: <br>This time in line 4 we have declared another variable by name compareWith and assigned “index -1”. Doing so will give us the access to the previous element by array index to compare with current one.</p><p>Step 5:<br>In the line 5–8 we have initialized while loop and the loop controller is checking for compareWith index for not to be less than 0.If statement is true then the second statement is getting control. In this statement we comparing current element with previous element <br>“array[compareWith] &gt; element” =&gt; “7 &gt; 6”</p><p>Step 6:<br>In the line 6 we moved element to the right. That’s because 6 is lesser than 7 <br>Now the array is looks like this: [7, 7, 4, 1, 3, 5, 2];</p><blockquote>Hmm… <br>It’s not good because we didn’t moved element “7” we just copied it. Let’s correct our mistake. In step 8</blockquote><p>Step 7:<br>In the line 7 we substracting compareWith 1. Doing so will give us a chance to compare our current element with 2 , 3 or n step previous elements.<br>if we compare 4th element with 3rd one,we have to compare with 2nd,and 1st one too.</p><p>Step 8:<br>In the line 9 finally we can assign our current element to it’s own place like this. “array[compareWith + 1] = element”.</p><p>In the end we can write our own sort function like below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ea9ab883523a51fcb30f084b59fc404/href">https://medium.com/media/3ea9ab883523a51fcb30f084b59fc404/href</a></iframe><p>Just tweaking compare operant we can make our sort algorithm to sort given array ascending or descending;</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1456c80660ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>