<?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 Senad Meškin on Medium]]></title>
        <description><![CDATA[Stories by Senad Meškin on Medium]]></description>
        <link>https://medium.com/@senadmeskin?source=rss-854f8db6bcb7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*lSksYjwhdXUvCrt7PzwhQg.jpeg</url>
            <title>Stories by Senad Meškin on Medium</title>
            <link>https://medium.com/@senadmeskin?source=rss-854f8db6bcb7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 30 May 2026 06:06:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@senadmeskin/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[Rotate an array — C#]]></title>
            <link>https://medium.com/@senadmeskin/rotate-an-array-c-aa247c9f1bff?source=rss-854f8db6bcb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa247c9f1bff</guid>
            <category><![CDATA[reversing]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Senad Meškin]]></dc:creator>
            <pubDate>Mon, 02 Sep 2019 22:28:43 GMT</pubDate>
            <atom:updated>2019-09-03T14:06:33.243Z</atom:updated>
            <content:encoded><![CDATA[<h3>Rotate an array — C#</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HQVdt9kxizkYMEer" /><figcaption>Photo by <a href="https://unsplash.com/@cgower?utm_source=medium&amp;utm_medium=referral">Christopher Gower</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Array rotation is when you move array items by a defined number of places.<br>Let&#39;s say that we have an array of integers [1, 2, 3, 4, 5]that we need to rotate by 2 places in order to get an array [3, 4, 5, 1, 2] .</p><p>So, what did we do here?<br>If we do it by just swapping places, our logic would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f934649a1fddd7f1b2e0e79e361289a/href">https://medium.com/media/7f934649a1fddd7f1b2e0e79e361289a/href</a></iframe><p>Maybe you could use the above code to rotate small arrays, but please don’t use it as a solution.</p><p>If we apply more thinking into this then we can apply some logic and avoid nested loops and reduce the number of operations needed to rotate an array.</p><p><strong>Algorithm:</strong></p><pre>1. Split array into two parts, first part is items from index 0 to the numberOfRotatitons-1.<br>2. Reverse first array<br>3. Reverse second array<br>4. Reverse whole array</pre><p>This looks like an easy solution, now let&#39;s explain it with the real data.</p><pre>We have an array [1, 2, 3, 4, 5],  and we need to rotate it by 2 places. Our first array will be from index 0 to index 1 (including value at the index 1) [1, 2], and second array will be from index 2 to index 4 [3, 4, 5].</pre><pre>Reversing first array [1, 2] we would get [2, 1].<br>Reversing second array [3, 4, 5] we would get [5, 4, 3]</pre><pre>Result of our work till now is an array [2, 1, 5, 4, 3]</pre><pre>Now we apply forth step of our algorithm and reverse whole array [2, 1, 5, 4, 3] into [3, 4, 5, 1, 2].</pre><p><strong>How do we convert this into the code?</strong></p><p>In order to put this algorithm into the C# code, I will create two functions, one function will be logic for rotation, and the second function will be logic for reversing an array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7efb933b1cdf322a97d532cff0f6ec3c/href">https://medium.com/media/7efb933b1cdf322a97d532cff0f6ec3c/href</a></iframe><p>Let’s explain the code above:</p><p>If we have an array [1, 2] and we need to reverse that array then we need to swap values from index 1 to index 0. In order to save value from index 0, we need to put it into a tempvariable so we can later assign this value to the index 1 of our array.</p><pre>array [1, 2]<br>temp = array[0] = 1<br>array[0] = array[1] = 2<br>array[1] = temp = 1<br>and we get an array [1, 2]</pre><p>Now we have an array that looks like this [2, 1, 3, 4, 5] and we need to reverse second part so we will call our method ReverseArray(arr, 2, arr.Length-1)</p><p>and now we have an array that looks like [2, 1, 5, 4, 3] , and if we reverse it from start to the end ReverseArray(arr, 0, arr.Length-1) we will get an array that looks like [3, 4, 5, 1, 2]</p><p>In order to put this code in use, we need to create another function which will execute all those ReverseArray calls.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4bcc37afe625909a7f7328b76a16be0/href">https://medium.com/media/b4bcc37afe625909a7f7328b76a16be0/href</a></iframe><p><strong>EXTRA</strong></p><blockquote>If you are asking your self where to put this code I will explain in details how you can create a new console project and put this into the code.</blockquote><p><strong>Step 1:</strong></p><p>If you don’t have dotnet core you can get it at the URL <a href="https://dotnet.microsoft.com/download">https://dotnet.microsoft.com/download</a></p><p>Download SDK and runtime so you can run and build applications.</p><p><strong>Step 2:</strong></p><p>If you don’t have an IDE (Integrated Development Environment) you can download Visual Studio Code for free from this link: <a href="https://code.visualstudio.com/download">https://code.visualstudio.com/download</a></p><p><strong>Step 3:</strong></p><p>In your terminal/console/powershell (whatever pleases you) create a folder for this test e.g. RotatingArrays</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/551/1*o-76GEY4JlL9SzSagYOhPg.jpeg" /><figcaption>Create and open a folder</figcaption></figure><p>Now, when everything is created you can create a project by typing into the console dotnet new console</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*XFt777Aj2KPJJRwCbFbn-w.jpeg" /><figcaption>Creating a project with dotnet command</figcaption></figure><p>Now you should be able to start Visual Studio Code by just typing code . into your console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*kV3BPISuMTy6m8rVCpTgmA.jpeg" /><figcaption>starting code in the active folder</figcaption></figure><p>If everything is OK, you will see Visual Studio Code with the project that we created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*ee52may1F7LPc91lBag9_A.jpeg" /><figcaption>Visual Studio Code with loaded project</figcaption></figure><p>Now, we can put our code inside it and our code should look like</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9514341a69b8e22fab0d7fc2b9658bd/href">https://medium.com/media/a9514341a69b8e22fab0d7fc2b9658bd/href</a></iframe><p>In order to see results of our work you can simply start a terminal inside Visual Studio Code, or use your own and write a command dotnet run and your application will be compiled and then started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*ad3_g5sBBThqocnmwRAIJw.jpeg" /><figcaption>Running app inside Visual Studio Code terminal</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa247c9f1bff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>