Skip to content

Instantly share code, notes, and snippets.

@danielmartin
Created November 18, 2017 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmartin/37d7935907a1988ece2a59826e598eea to your computer and use it in GitHub Desktop.
Save danielmartin/37d7935907a1988ece2a59826e598eea to your computer and use it in GitHub Desktop.
Generates a set of PPM images from the intermediate results of Swift's sorting algorithm
import Foundation
var sample = [11, 57, 55, 37, 54, 41, 30, 8, 53, 4, 47, 58, 18, 56, 17, 12, 39, 28, 16, 63, 40, 27, 50, 48, 19, 2, 25, 52, 13, 59, 64, 9, 26, 24, 23, 44, 21, 0, 6, 62, 61, 7, 29, 43, 38, 33, 51, 34, 3, 42, 22, 46, 5, 1, 10, 32, 60, 15, 49, 45, 35, 20, 36, 31]
let elementWidth = 10
let elementHeight = 100
let markerHeight = 10
sample.withUnsafeMutableBufferPointer { ptr -> () in
ptr.sort {
vprintf("P6\n%d %d\n255\n", getVaList([elementWidth*ptr.count, elementHeight]))
for y in 0..<elementHeight {
let edge = y < markerHeight || y > elementHeight - markerHeight
for x in 0..<elementWidth*ptr.count {
let elemIndex = x / elementWidth
let isSelected = elemIndex == ptr.index(of: $0) || elemIndex == ptr.index(of: $1)
var r = 0, g = 0, b = 0
if edge && isSelected {
r = 255
} else {
r = (ptr[elemIndex] * 255) / ptr.count
g = r
b = r
}
vprintf("%c%c%c", getVaList([r, g, b]))
}
}
return $0 < $1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment