• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • About
  • Portfolio
  • Privacy Policy

Swift

Bubble Sort

June 25, 2014 By Ravi Shankar 2 Comments

The best way to practice a new language is to write more programs using that language. Here is a Bubble Sort program written in Swift Programming language.

Data to be sorted = {12, 56, 32, 23, 67, 87, 45, 23,10, 11}

Bubble Sort

Bubble Sort is performed by comparing two number starting from the left and swapping the greater number to the right. And then moves one position to right until end.

Bubble Sort in Swift

This has a function named swapNumbers for swapping the values of array for the given two indexes. Then the for loop that compares the two numbers starting from left to right and uses swapNumbers function to swap the values if the left is greater than right.

var inputArr = [12,56,32,23,67,87,45,23,10,11]

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

let temp = inputArr[index1]

inputArr[index1] = inputArr[index2]

inputArr[index2] = temp

}

for var index: Int = inputArr.count–1; index > 1; –index {

for var jIndex: Int = 0; jIndex < index; ++jIndex {

if inputArr[jIndex] > inputArr[jIndex + 1] {

swapNumbers(jIndex, jIndex+1)

}

}

}

inputArr

Here is the Swift code tested in Playground, you can see the sorted results in the sidebar

Bubble Sort in Swift Programming language

Filed Under: Apple, Programming, Xcode Tagged With: Apple, Swift, Untitled, Xcode

Decode job advert posted in Swift Language

March 20, 2014 By Ravi Shankar 1 Comment

I came across this unique Swift job advert in stackoverflow.com posted by Criteo. (Need to add override for the init function, missed in the original job post)

var hello = "Hello, I am a Swift Program, paste me in the playground"

var doyouswift = "You think you have what it takes to build the next mobile advertising technologies ?"

class Hire {

    var r = "&"

    var d = "r"

    var ec = "@rt"

    func email() -> String{

        return "look at gogogo's"

    }

}

class HireMe: Hire{

    var me = "[email protected]"

    var crit = "e"

    let o = ".com"

  override init() {

        super.init()

        ec = "ruit"

    }

override func email() -> String{

        return super.email()+" preview, remove quotes and braces to reveal the email."

    }

}

var gogogo = HireMe()

println(gogogo.email())


Filed Under: Apple, Develop, Programming Tagged With: Apple, Job, Swift

  1. Pages:
  2. «
  3. 1
  4. 2
  5. 3
  6. 4
« Previous Page

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to know the size of the folders in iCloud
  • Errors were encountered while preparing your device
  • We have blocked all requests from this device – Firebase Phone Authentication
  • iPhone is not available error message in Xcode
  • Clear CocoaPods cache, re-download and reinstall all pods

Copyright 2021 © rshankar.com