<?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 Zafer Şevik on Medium]]></title>
        <description><![CDATA[Stories by Zafer Şevik on Medium]]></description>
        <link>https://medium.com/@notestomyself?source=rss-5e72745341a8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EIzlp6GOpa8P1cH_oiW11g.jpeg</url>
            <title>Stories by Zafer Şevik on Medium</title>
            <link>https://medium.com/@notestomyself?source=rss-5e72745341a8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 11:58:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@notestomyself/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[Strange Unity Error and Fix]]></title>
            <link>https://medium.com/@notestomyself/strange-unity-error-and-fix-c840708ed67e?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/c840708ed67e</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[solutions]]></category>
            <category><![CDATA[failure]]></category>
            <category><![CDATA[error]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Fri, 22 Jun 2018 17:40:01 GMT</pubDate>
            <atom:updated>2018-06-22T17:40:01.794Z</atom:updated>
            <content:encoded><![CDATA[<p>Unity Version: 2017.4.2f2</p><p>I have a custom mainTemplate.gradle file under Plugins/Android directory and already has a dependency to gradle 2.1.0 . I think Unity error message is hardcoded and misleading. It says “Gradle version 2.10 is required.” (You can see the whole error message below)</p><h3>Error Message</h3><p>CommandInvokationFailure: Gradle build failed. <br>/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java -classpath “/Applications/Unity 2017.4.2f2/PlaybackEngines/AndroidPlayer/Tools/gradle/lib/gradle-launcher-4.0.1.jar” org.gradle.launcher.GradleMain “-Dorg.gradle.jvmargs=-Xmx2048m” “assembleDebug”</p><p>stderr[</p><p>FAILURE: Build failed with an exception.</p><p>* Where:<br>Build file ‘/Users/zafersevik/Documents/core-tech-unity-1/Core-Tech-Unity/Temp/gradleOut/build.gradle’ line: 34</p><p>* What went wrong:<br>A problem occurred evaluating root project ‘gradleOut’.<br>&gt; Failed to apply plugin [id ‘com.android.application’]<br> &gt; Gradle version 2.10 is required. Current version is 4.0.1. If using the gradle wrapper, try editing the distributionUrl in /Users/zafersevik/Documents/core-tech-unity-1/Core-Tech-Unity/Temp/gradleOut/gradle/wrapper/gradle-wrapper.properties to gradle-2.10-all.zip</p><p>* Try:<br>Run with — stacktrace option to get the stack trace. Run with — info or — debug option to get more log output.</p><p>BUILD FAILED in 5s</p><h3>Solution</h3><p>When I updated my gradle version to 2.3.0</p><p>Changing <strong>classpath ‘com.android.tools.build:gradle:2.1.0’</strong></p><p>to <strong>classpath ‘com.android.tools.build:gradle:2.3.0’</strong></p><p>solves the failure.</p><p>Take away is clear: do not to trust Unity error messages.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c840708ed67e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[__bridge vs __bridge_transfer vs __bridge_retained]]></title>
            <link>https://medium.com/@notestomyself/bridge-vs-bridge-transfer-vs-bridge-retained-f84e6b6406d1?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f84e6b6406d1</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[bridging]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[objective-c]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Thu, 19 Apr 2018 22:56:53 GMT</pubDate>
            <atom:updated>2018-04-26T10:03:08.395Z</atom:updated>
            <content:encoded><![CDATA[<h3>__bridge</h3><p>Two way OBJECTIVE-C ← → CF</p><p>Transfers a pointer between Objective-C and Core Foundation with no transfer of ownership</p><pre>-(void)greetViaBridge<br>{<br>    CFStringRef stringRef = CFStringCreateWithCString(NULL, &quot;Hello World via bridge&quot;, kCFStringEncodingUTF8);</pre><pre>    NSString *text = (__bridge NSString *) stringRef;<br>    NSLog(@&quot;%@&quot;, text);</pre><pre>    CFRelease(stringRef);<br>}</pre><h3>__bridge_transfer</h3><p>CF → OBJECTIVE-C</p><p>Moves a Core Foundation pointer to Objective-C with transfer of the ownership to ARC</p><pre>-(void)greetViaBridgeTransfer<br>{<br>    CFStringRef stringRef = CFStringCreateWithCString(NULL, &quot;Hello World via bridge transfer&quot;, kCFStringEncodingUTF8);</pre><pre>    NSString *text = (__bridge_transfer NSString *) stringRef;<br>      <br>    NSLog(@&quot;%@&quot;, text);<br>}</pre><h3>__bridge_retained</h3><p>OBJECTIVE-C → CF</p><p>Moves an Objective-C pointer to Core Foundation and transfers ownership and also responsibility of releasing the memory to you</p><pre>-(void)greetViaBridgeRetained<br>{<br>    NSString *text = @&quot;Hello World via bridge retained&quot;;<br>    CFStringRef stringRef = (__bridge_retained CFStringRef)text;<br>    NSLog(@&quot;NSString: %@&quot;, text);<br>    NSLog(@&quot;CFStringRef: %@&quot;, stringRef);</pre><pre>    CFRelease(stringRef);<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f84e6b6406d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to integrate external frameworks to an iOS project using Carthage?]]></title>
            <link>https://medium.com/@notestomyself/how-to-integrate-external-frameworks-to-an-ios-project-using-carthage-25a8fe260488?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/25a8fe260488</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[dependencies]]></category>
            <category><![CDATA[carthage]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Thu, 08 Feb 2018 21:41:40 GMT</pubDate>
            <atom:updated>2018-02-08T21:41:40.846Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is Carthage?</h4><p>Carthage is a simple dependency manager for macOS and iOS. You could think it as an alternative tool to Cocoapods.</p><p>Some advantages of using Carthage</p><ul><li>Carthage doesn’t modify your Xcode project</li><li>Carthage fetches and builds project to create frameworks. You have the full control over the frameworks you use.</li><li>There is no centralised authority forcing library authors to submit their codes (or pods). Any framework project on Github can be downloaded as a dependency via Carthage.</li><li>Carthage enables sharing Swift code, Objective-C code and assets like images together by using dynamic frameworks. (Note: iOS 8 or later versions supports dynamic frameworks)</li></ul><h4>How to installing?</h4><p>You can download Carthage.pkg from <a href="https://github.com/Carthage/Carthage/releases">https://github.com/Carthage/Carthage/releases</a></p><p>I prefer to use Homebrew. After installing Homebrew. Open your terminal run brew update and brew install carthage . You are ready to define your project dependencies.</p><h4>Getting Frameworks</h4><p>1- Create a Cartfile in your project folder</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-q6ZB88iOzjEZ0zZZUlv7Q.png" /><figcaption>Cartfile in project folder</figcaption></figure><p>2- Open Cartfile on Xcode or on your favorite text editor and add dependencies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*HdzcNyOGv8Iz7N8VsSwPxg.png" /><figcaption>Cartfile content</figcaption></figure><p>I’m integrating only one dependency but you can list all your dependencies in this file. And now save and close your Cartfile.</p><p>3- Go back to terminal and navigate to your project’s home directory by</p><p>cd ~/Path/To/Your/Project</p><p>4- Build dependencies with this carthage update --platform iOS command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eDHVBX9Vcl3mkkuG4z-Hiw.png" /><figcaption>Carthage folder and Cartfile.resolved created after update</figcaption></figure><p>Because of --platform iOS option you entered in your command frameworks are only built for iOS</p><p>Cartfile.resolved contains the versions of dependencies Carthage downloaded.</p><p>Carthage/Checkouts contains the source code for each dependecy.</p><p>Carthage/Build contains the built framework which you want to integrate to your project. These frameworks sometimes downloaded from Github releases page and sometimes built from scratch by using the source code in Checkouts folder. (You could have frameworks in your Build folder and no code in Checkouts folder, this means frameworks are just downloaded from Github)</p><h4>Integrating Frameworks</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ujhFNCQr8d9jG6930uAdKQ.png" /><figcaption>At the end of integration</figcaption></figure><p>You need to link created binaries and create a run script and then your project is ready to run. Step by step framework integration</p><p>1- Back to Xcode and click project in the project navigator.</p><p>2- Select project target</p><p>3- Select Build Phases</p><p>4- Add your libraryName.framework files to ‘Link Binary With Libraries’ part (you can drag and drop from Finder)</p><p>5- Create a Run Script</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6hzI9uuOXPl6GgT4KQdf4w.png" /><figcaption>Select “New Run Script Phase” to create a run script</figcaption></figure><p>On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase”. Create a Run Script in which you specify your shell, add the following contents to the script area below the shell:</p><p>/usr/local/bin/carthage copy-frameworks</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LzEQwCohunSxLoNX0DcIIw.png" /></figure><p>This script works around an <a href="http://www.openradar.me/radar?id=6409498411401216">App Store submission bug</a> triggered by universal binaries and ensures that necessary bitcode-related files and dSYMs are copied when archiving.</p><p>6- Add the paths to the frameworks you want to use under “Input Files”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VzfnWApELcwXLP7ePzrHnQ.png" /></figure><p>7- Add the paths to the copied frameworks to the “Output Files”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ckd0LwBMwob_6rgMooVzNA.png" /><figcaption>To make subsequent project runs faster</figcaption></figure><p>With output files specified alongside the input files, Xcode only needs to run the script when the input files have changed or the output files are missing. This means dirty builds will be faster when you haven’t rebuilt frameworks with Carthage.</p><p>Your project is ready to run with it’s dependencies.</p><h4>Conclusion</h4><p>Although Carthage needs a little manual work for each new dependency added to the Cartfile, having a project file not modified by an other tool is a preferable gain.</p><p>Carthage 👍</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25a8fe260488" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement Binary Search in Swift?]]></title>
            <link>https://medium.com/@notestomyself/how-to-implement-binary-search-in-swift-6867840302ed?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/6867840302ed</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[binary-search]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[search-algorithm]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Thu, 05 Oct 2017 07:57:18 GMT</pubDate>
            <atom:updated>2017-10-05T07:57:18.371Z</atom:updated>
            <content:encoded><![CDATA[<p>Binary Search is an efficient algorithm used to find an item from an ordered list. You can implement Binary Search algorithm recursively by halving the size of the list given for the search in every step. Binary Search runs in <em>lg n</em> time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*fMQ2cLdIEr3DoHitYyiSsg.png" /></figure><h4>Define the problem and algorithm interface</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd2f87e91b769b39fc0b0922f727d7f3/href">https://medium.com/media/cd2f87e91b769b39fc0b0922f727d7f3/href</a></iframe><p>You have numbers array and two index values requested indexOfTargetValue and indexOfTargetValueNotInNumbers. At the end of implementation indexOfTargetValue should be 10 and indexOfTargetValueNotInNumbers should be -1. (-1 indicates that item is not in the list)</p><p>Using generics enables us to use binarySearch function to search in all arrays having items implementingComparable protocol.</p><p>As you can see there are two functions, binarySearch is the function used as an interface and binarySearchInRange is the function to use in recursive calls.</p><h4>Solve the problem for the base case</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f21ba8856bf731a513c4a2b0224bf029/href">https://medium.com/media/f21ba8856bf731a513c4a2b0224bf029/href</a></iframe><p>Using middle index as guessedIndex gives us 3 possibility.</p><ul><li>array[guessedIndex] might be smaller than targetValue</li><li>array[guessedIndex] might be equal to targetValue</li><li>array[guessedIndex] might be bigger than targetValue</li></ul><p>Equality case is the base case for Binary Search algorithm and means solution is found targetValue is in array and index is guessedIndex .</p><h4>Handling cases different than the base case</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7d79ba2ba7f0f57d79fcbccf89eec3e/href">https://medium.com/media/d7d79ba2ba7f0f57d79fcbccf89eec3e/href</a></iframe><p>In every recursive call, ordered array enables you to move minimum or maximum indices to a nearer location to the targetValue index in array.</p><p>By calling binarySearchInRange with new minimum and maximum index values recursive algorithm is ready to run for items exist in the array.</p><h4>What if item is not in the array?</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f13af7a6a485b0f1c1ba8701c6c602d/href">https://medium.com/media/2f13af7a6a485b0f1c1ba8701c6c602d/href</a></iframe><p>If minimumIndex exceeds the value of maximumIndex than it is clear that item is not in the list and returning -1 matchs our initial expectation.</p><p>You can see the full source code in action by downloading <a href="https://github.com/zafersevik/algorithms-in-swift">BinarySearch.playground</a></p><p>Happy coding</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6867840302ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement Selection Sort in Swift?]]></title>
            <link>https://medium.com/@notestomyself/how-to-implement-selection-sort-in-swift-c3c981c6c7b3?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3c981c6c7b3</guid>
            <category><![CDATA[playground]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Mon, 18 Sep 2017 14:04:08 GMT</pubDate>
            <atom:updated>2017-09-18T14:04:08.606Z</atom:updated>
            <content:encoded><![CDATA[<p>Selection Sort is a sorting algorithm with O(n²) time complexity. Selection Sort is an algorithm simple to understand and inefficient (time consuming) to use in most cases especially for large data sets. Still it can be logical to use when memory space is limited.</p><h3>Define the problem and algorithm interface</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb22a8c4393d42cb800f7b94ad0e1a8e/href">https://medium.com/media/eb22a8c4393d42cb800f7b94ad0e1a8e/href</a></iframe><p>Using generics enables us to use selectionSort function to sort all arrays having items implementingComparable protocol. array defined as inoutvariable to make in-place changes on array.</p><h3>Divide the sorting problem</h3><p>Dividing sorting problem changes the sorting problem into a <em>finding minimum problem</em>. You need to implement an indexOfMinimumItem function that takes arguments:</p><ul><li>array which has sorted and unsorted parts</li><li>startIndex the index of the unsorted part of the array</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed93a09c3cd300d36bbf56cc31bfab1e/href">https://medium.com/media/ed93a09c3cd300d36bbf56cc31bfab1e/href</a></iframe><p>You can get minimumItemIndex in subarray starting from given index by using indexOfMinimumItem function. By swapping values at minimumItemIndex and startIndex, you move the smaller values to the front of the array one by one.</p><p>Before swapping, you need to check that minimumItemIndex and index are different.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/551/1*H2bCd6eoIONJIUnG5Jm9sQ.gif" /><figcaption>How Selection Sort works</figcaption></figure><h3>Conquer the finding minimum problem</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72d826f2f0941ffb3749b7b20f4c6d62/href">https://medium.com/media/72d826f2f0941ffb3749b7b20f4c6d62/href</a></iframe><p>By finding and returning the minimum in the subarray with give startIndex Selection Sort implementation is finished.</p><p>You can see the full source code in action by downloading <a href="https://github.com/zafersevik/algorithms-in-swift">SelectionSort.playground</a></p><p>Happy coding</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3c981c6c7b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How do you implement Quick Sort in Swift?]]></title>
            <link>https://medium.com/@notestomyself/how-do-you-implement-quick-sort-in-swift-d0dd0308a473?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0dd0308a473</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[divide-and-conquer]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Mon, 11 Sep 2017 14:29:52 GMT</pubDate>
            <atom:updated>2017-09-11T14:29:52.451Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*tR69wI6GIcuGCp_69Be2rg.png" /></figure><p>Quick Sort (also called <strong>Partition-Exchange Sort</strong>) is an efficient divide and conquer algorithm developed by Tony Hoare in 1959. In worst case it takes O(n²) time but on average Quick Sort takes O(n log n) time and outperforms Merge and Heap Sort.</p><h3>Define a method interface to call recursively</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c46d3a1435ddef80d884c0520a6d45c9/href">https://medium.com/media/c46d3a1435ddef80d884c0520a6d45c9/href</a></iframe><p>numbers is an array of unsorted Int s. Your target is to implement a Quick Sort algorithm for any array with items conform Comparable protocol.</p><p>To make in-place changes instead of returning a new array after each call array parameter is marked inout</p><h3>Divide the problem into subproblems</h3><p>To divide the problem into smaller subproblems you need to define a partition function. partition function uses a pivot value to separate the array into 2 subarrays and returns the index of the item which has a value higher than the items with lower index and lower than the items with higher index. Let’s don’t think about the partition function for now, it just enables us to divide the problem into 2 smaller subproblems.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9aa98ba7d4e56ea9d3f9affa2a0d127/href">https://medium.com/media/e9aa98ba7d4e56ea9d3f9affa2a0d127/href</a></iframe><h3>Decide on the base case to stop recursion</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbae8ba4ae24791fa77e803b796eff26/href">https://medium.com/media/dbae8ba4ae24791fa77e803b796eff26/href</a></iframe><p>Actually when you have a startIndex == endIndex you have a one element array and you can’t divide it into smaller arrays further.</p><h3>Conquer the problem by implementing partition function</h3><p>[2, 8, 1, 3, 7, 5, 4, 9, 6] — &gt; you might set the right most value 6 as the pivot (You can pick the pivot element in different ways, implementation slightly changes)</p><p>partition function makes in-place changes on the array to separate the array into lower and higher subarrays.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hitiMFLjtZnIdEMXpQQEpw.jpeg" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c0e68ae9722673e8100031d8dc7bd78/href">https://medium.com/media/8c0e68ae9722673e8100031d8dc7bd78/href</a></iframe><h3>Create a better interface for usage</h3><p>Just to make usage of the quickSort function easier, you can supply a better interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e4862f3a0cfc9cc16754c108f711b0f/href">https://medium.com/media/4e4862f3a0cfc9cc16754c108f711b0f/href</a></iframe><p>And we are done implementing Quick Sort.</p><p>Download <a href="https://github.com/zafersevik/algorithms-in-swift">QuickSort.playground</a> to see the full source code in action.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0dd0308a473" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Insertion Sort in Swift]]></title>
            <link>https://medium.com/@notestomyself/insertion-sort-in-swift-fc593b6a7564?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc593b6a7564</guid>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[divide-and-conquer]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 14:44:15 GMT</pubDate>
            <atom:updated>2017-09-11T13:07:42.659Z</atom:updated>
            <content:encoded><![CDATA[<p>Insertion Sort is a simple sorting algorithm runs in О(<em>n²</em>) time. It is less efficient than Quick Sort, Merge Sort or Heap Sort on larger datasets. But it is an efficient algorithm while inserting items to an already sorted dataset.</p><h3>Define the problem and the algorithm interface</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/475702b34a2de1928eac6e10224c3375/href">https://medium.com/media/475702b34a2de1928eac6e10224c3375/href</a></iframe><p>Using generics enables us to use insertionSort function to sort all arrays having items implementingComparable protocol. array defined as inout variable to make in-place changes on array.</p><h3>Divide the sorting problem</h3><p>Dividing sorting problem changes the sorting problem into an insertion problem. You need to implement an insert function that takes arguments:</p><ul><li>array which has sorted and unsorted parts</li><li>rightIndex the index of the last inserted (or sorted) element,</li><li>value the value of the item to insert</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac6df4f06cbe2339b1b3c8e36fc1de9a/href">https://medium.com/media/ac6df4f06cbe2339b1b3c8e36fc1de9a/href</a></iframe><p>If you implement an insert function which transfers the items one by one from unsorted part of the array to the sorted part, the sorting problem will be solved by inserting items.</p><h3>Conquer the item insertion problem</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*HfykZA3INDyATYGqmQdi2w.jpeg" /></figure><p>Time to conquer the problem for one item. insertionSort function will call insert for all items except the zeroth item (since it is not needed to sort an array with one item) so you just need to worry about the value at hand while implementing insert.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d544d2229b6e5efac6b570e4ee581c8c/href">https://medium.com/media/d544d2229b6e5efac6b570e4ee581c8c/href</a></iframe><p>You need to shift right all items bigger than the value unless index takes a value equal to / smaller than zero. At the end you set the item to the first location where value is bigger than the compared array item. (if value is smaller than the all items compared, value is set to array[0])</p><h3>Merge</h3><p>Since you are doing changes on the actual dataset, and inserting each element to it’s location in the dataset. You don’t need a merge stage for Insertion Sort algorithm.</p><h3>Finishing touch</h3><p>Insertion sort algorithm is ready but a small refinement can make it better.</p><h4>Add a check for empty array</h4><p>Currently our insertion sort algorithm will crash if you try to sort an empty array. By adding a check you can fix this issue.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77963ca1622189549c1b6ece17a107ae/href">https://medium.com/media/77963ca1622189549c1b6ece17a107ae/href</a></iframe><p>Download <a href="https://github.com/zafersevik/algorithms-in-swift">InsertionSort.playground</a> to see the full source code in action.</p><p>Happy coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc593b6a7564" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Merge Sort in Swift]]></title>
            <link>https://medium.com/@notestomyself/merge-sort-in-swift-ae33679251e7?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae33679251e7</guid>
            <category><![CDATA[playground]]></category>
            <category><![CDATA[divide-and-conquer]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Thu, 24 Aug 2017 10:58:18 GMT</pubDate>
            <atom:updated>2017-09-15T13:02:46.812Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*v0TKIqmzJXCdnnA4my3JKQ.gif" /></figure><p>Merge sort is a divide and conquer algorithm that runs in O(n log n) time and was invented in 1945 by John von Neumann.</p><p>Divide and conquer algorithms breaks a problem into similar, smaller subproblems to solve the original problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E5IMbHQQjoH91TcGRqkR5Q.jpeg" /><figcaption>The 3 parts of divide and conquer algorithm</figcaption></figure><p>After this quick introduction let’s dive into the problem and code an in-place, generic implementation of merge sort in Swift.</p><h3>Define a method interface to call recursively</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1674242aadf2cdfb128c57547e0336e/href">https://medium.com/media/b1674242aadf2cdfb128c57547e0336e/href</a></iframe><p>numbers is an array of unsorted Int s. Your target is to implement a merge sort algorithm for any array with items conform Comparable protocol.</p><p>To make in-place changes instead of returning a new array after each call array parameter is marked inout</p><h3>Divide the problem into subproblems</h3><p>You need to find the index midway between left most and right most indexes to divide our problem to smaller merge sort subproblems.</p><pre>let middleIndex = (startIndex + endIndex) / 2</pre><p>Now you can define 2 subproblems instead of 1 big problem to solve</p><pre>mergeSort(array:&amp;array, startIndex:startIndex, endIndex:middleIndex)    <br>mergeSort(array:&amp;array, startIndex:middleIndex+1, endIndex:endIndex)</pre><p>After solving these 2 subproblems you just need to combine solutions to have the problem solved</p><pre>merge(array: &amp;array, <br>      startIndex: startIndex, <br>      middleIndex: middleIndex, <br>      endIndex: endIndex)</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f85675843a71d5da0e0f2b23bbf7a878/href">https://medium.com/media/f85675843a71d5da0e0f2b23bbf7a878/href</a></iframe><h3>Decide what is the base case to stop recursion</h3><p>Now you know how to divide the problem into subproblems but you need to decide when to stop. If you get to a point where each subarray has only 1 item in it, you don’t need and actually can’t continue creating subarrays.</p><p>2 subarrays having 1 item is our <em>base case</em> to solve the merge sort problem.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/031c67a1da9a5fb8c1298a5ba6d23569/href">https://medium.com/media/031c67a1da9a5fb8c1298a5ba6d23569/href</a></iframe><h3>Combine subproblem solutions to get the solution of the initial problem</h3><p>The last thing you need to implement is the merge method. Merge sort like it’s name imply is doing it’s real job in merging stage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aD4KMgxny280GmZx-fdOZA.png" /></figure><p>After creating 2 subarrays, you have 3 while loops to set items from leftSubarray or rightSubarray to the original array.</p><p>Until the exhaustion of one of the subarrays you need to compare values in left and right subarrays and set the value to the index position in array. When this comparison ends and while loop returns, you need to copy all the items of the remaining (not exhausted subarray’s) items to array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a1ed4f69e1736e26d58429481851d66/href">https://medium.com/media/5a1ed4f69e1736e26d58429481851d66/href</a></iframe><h3>Create a better interface for mergeSort</h3><p>You finished implementing the merge sort in Swift but you can improve the function’s interface so that anyone can use the mergeSort function without needing to supply 3 arguments or thinking about inner implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3be087f1d421c7183cfa6423d85b817/href">https://medium.com/media/a3be087f1d421c7183cfa6423d85b817/href</a></iframe><p>Download <a href="https://github.com/zafersevik/algorithms-in-swift">MergeSort.playground</a> to see the full source code in action.</p><p>Happy coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae33679251e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[String Rotation in Swift]]></title>
            <link>https://medium.com/@notestomyself/string-rotation-in-swift-ab5b38721c56?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab5b38721c56</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Mon, 12 Jun 2017 07:57:41 GMT</pubDate>
            <atom:updated>2017-06-12T07:57:41.790Z</atom:updated>
            <content:encoded><![CDATA[<h4>Q 1.9 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (e.g. “waterbottle” is a rotation of “erbottlewat”).</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a9868a1dc9c262dfec958abe3a4e99b/href">https://medium.com/media/0a9868a1dc9c262dfec958abe3a4e99b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab5b38721c56" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zero Matrix in Swift]]></title>
            <link>https://medium.com/@notestomyself/zero-matrix-in-swift-2459a56bf03a?source=rss-5e72745341a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/2459a56bf03a</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Zafer Şevik]]></dc:creator>
            <pubDate>Sun, 11 Jun 2017 10:18:45 GMT</pubDate>
            <atom:updated>2017-06-11T20:03:07.697Z</atom:updated>
            <content:encoded><![CDATA[<h4>Q 1.8 Write an algorithm such that if an element in an MxN matrix is zero, it’s entire row and column are set to zero.</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e4b5a69251954eb3d8348b289abad09/href">https://medium.com/media/7e4b5a69251954eb3d8348b289abad09/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2459a56bf03a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>