Skip to content

Instantly share code, notes, and snippets.

@tmdvs
tmdvs / bubble.swift
Last active July 4, 2022 19:04
Bubble sort in Swift
// An Example of a bubble sort algorithm in Swift
//
// Essentialy this algorithm will loop through the values up to
// the index where we last did a sort (everything above is already in order/sorted)
// comparing a one value to the value before it. If the value before it is higher,
// swap them, and note the highest swap index. On the next iteration of the loop we
// only need to go as high as the previous swap.
import Foundation
@davidpdrsn
davidpdrsn / mergesort.swift
Last active August 24, 2016 13:11
Merge sort in Swift
import Foundation
// Build 100 random numbers between 0 and 100
var numbers = Int[]()
for i in 1..100 {
let n = Int(arc4random() % 101)
numbers.append(n)
}
func elementsInRange<T>(a: T[], start: Int, end: Int) -> (T[]) {
var randomNumbers = [42, 12, 88, 62, 63, 56, 1, 77, 88, 97, 97, 20, 45, 91, 62, 2, 15, 31, 59, 5]
func partition(v: Int[], left: Int, right: Int) -> Int {
var i = left
for j in (left + 1)..(right + 1) {
if v[j] < v[left] {
i += 1
(v[i], v[j]) = (v[j], v[i])
}
}
@edwardinubuntu
edwardinubuntu / gist:2988342
Created June 25, 2012 12:38
audioRouteChangeListenerCallback
void audioRouteChangeListenerCallback (
void *inUserData,
AudioSessionPropertyID inPropertyID,
UInt32 inPropertyValueSize,
const void *inPropertyValue
) {
// ensure that this callback was invoked for a route change
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
if ([inUserData isKindOfClass:[IVAudioPlayerController class]]) {