title: TimSort in Swift
body: <p>I am trying to implement TimSort in Swift.
  I have referred to both these links : <a href="https://github.com/graphoarty/scripts/blob/master/Sorting%20Algorithms/tim_sort.py" rel="nofollow noreferrer">This</a> and <a href="https://www.geeksforgeeks.org/timsort/" rel="nofollow noreferrer">this</a>
  The code which I have converted to Swift is :</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
  var arr : [Int] = []
  let run : Int = 5
  
  override func viewDidLoad() {
      super.viewDidLoad()
      for _ in 0..&lt;10 {
          arr.append(Int(arc4random_uniform(100)))
      }
      timSort()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  func insertionSort(_ array:[Int]) -&gt; [Int] {
      var a = array
      for x in 1..&lt;a.count {
          var y = x
          while y &gt; 0 &amp;&amp; a[y] &lt; a[y - 1] {
              a.swapAt(y - 1, y)
              y -= 1
          }
      }
      return a
  }
  
  func merge(leftPile: [Int], rightPile: [Int]) -&gt; [Int] {
      var leftIndex = 0
      var rightIndex = 0
  
      var orderedPile = [Int]()
  
      while leftIndex &lt; leftPile.count &amp;&amp; rightIndex &lt; rightPile.count {
          if leftPile[leftIndex] &lt; rightPile[rightIndex] {
              orderedPile.append(leftPile[leftIndex])
              leftIndex += 1
          } else if leftPile[leftIndex] &gt; rightPile[rightIndex] {
              orderedPile.append(rightPile[rightIndex])
              rightIndex += 1
          } else {
              orderedPile.append(leftPile[leftIndex])
              leftIndex += 1
              orderedPile.append(rightPile[rightIndex])
              rightIndex += 1
          }
      }
  
      while leftIndex &lt; leftPile.count {
          orderedPile.append(leftPile[leftIndex])
          leftIndex += 1
      }
  
      while rightIndex &lt; rightPile.count {
          orderedPile.append(rightPile[rightIndex])
          rightIndex += 1
      }
  
      return orderedPile
  }
  
  func timSort() {
      print("Unsorted : \(arr)")
      for i in stride(from: 0, to: arr.count, by: run) {
          print("i : \(min((i + run),(arr.count)))")
          arr.replaceSubrange(i..&lt;min((i + run),(arr.count)), with: insertionSort(Array(arr[i..&lt;min((i + run),(arr.count))])))
      }
      print("after insertion sort \(arr)")
  
      var runCount = run
      while runCount &lt; arr.count{
          for x in stride(from: 0, to: arr.count, by: 2 * runCount) {
              print("x : \(x) runcount \(runCount) calc : \(x + 2 * runCount)")
              arr.replaceSubrange(x..&lt;min((x + 2 * runCount),(arr.count)), with: merge(leftPile: Array(arr[x..&lt;(x + runCount)]), rightPile: Array(arr[(x + runCount)..&lt;min((x + 2 * runCount),(arr.count))])))
          }
          runCount = runCount * 2
      }
  
      print("Sorted : \(arr)")
  }
  }
  </code></pre>
  
  <p>The issue that I am facing is that when I execute code in both the links, it works with any run value (like <code>run = 7</code>), but the same isn't happening in my code. </p>
  
  <p>My code only works properly when <code>run = 5</code> and <code>arr.count = 10</code>. In all other cases, it crashes on <code>arr.replaceSubrange(x..&lt;min((x + 2 * runCount),(arr.count)), with: merge(leftPile: Array(arr[x..&lt;(x + runCount)]), rightPile: Array(arr[(x + runCount)..&lt;min((x + 2 * runCount),(arr.count))])))</code> this line.</p>
  
  <p>I have tried various ways out but not able to get the issue.
  Please could someone help me point it out.</p>