• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips

algorithm

Insertion Sort

July 3, 2014 By Ravi Shankar 2 Comments

Insertion Sort algorithm does the following

  • Keeps the sorted numbers from left to right.
  • Compares the left with right and interchanges the number if left is greater than right.

Here is code snippet of Insertion Sort in Swift.

[code language=”swift”]var inputArr:[Int] = [Int]()

// generate random numbers
for rIndex in 0..<10 {
inputArr.append(((Int(arc4random()) % 100)))
}

func insertionSort(var inputArray :[Int]) -> [Int] {
var jIndex:Int,kIndex:Int

for kIndex in 1.. 0 && inputArray[jIndex-1] >= temp ) {
inputArray[jIndex] = inputArray[jIndex-1]
–jIndex
}
inputArray[jIndex] = temp
}

return inputArray
}

insertionSort(inputArr)[/code]

Filed Under: ios, Mac, Programming, Xcode Tagged With: algorithm, insertion Sort, Swift, Xcode

Selection Sort

July 1, 2014 By Ravi Shankar Leave a Comment

Selection Sort algorithm does the same amount of comparison( N*(N-1)/2 ) like bubble sort but the number swaps (N) is reduced drastically. This sort algorithm traverse through all the items, picks the smallest number and swaps it with left most item. Then the step is repeated for the next smallest number until it reaches the end of the elements in array. Listed below is the code for Selection Sort and screenshot of this algorithm executed in Playground.

import UIKit

func swapNumbers(index1 :Int,index2: Int) {

let temp = inputArr[index1]

inputArr[index1] = inputArr[index2]

inputArr[index2] = temp

}


var inputArr = Int[]()


// generate random numbers

for rIndex in 0..10 {

inputArr.append(((Int(arc4random()) % 100)))

}


inputArr


var minIndex: Int = 0


func selectionSort(inputArray :Int[]) {

for var index:Int = 0; index < inputArr.count-1; ++index {

minIndex = index

for (var jIndex: Int = index + 1; jIndex < inputArr.count-1; ++jIndex) {

if inputArr[jIndex] < inputArr[minIndex] {

minIndex = jIndex

}

}

swapNumbers(index, minIndex)

}

}


selectionSort(inputArr)


inputArr

  1. swapNumber function is used for swapping the numbers in the array for the given indexes.
  2. The numbers for the input array is randomly generated using the arc4random function.
  3. selectionSort function is the selectionSort algorithm that starts from the first number in the array and compares it with other elements and finds smallest one (stores the index as minIndex).
  4. Then swaps the smallest number with the left most and repeats these steps for the next number in the array until it reaches end of array.


Selection Sort algorithm in Swift

Filed Under: Develop, ios, Xcode Tagged With: algorithm, Selection Sort, Swift, Xcode

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to block keywords in Jio broadband
  • How to disable opening an app automatically at login in Mac
  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy