SwiftAnimations it's a library created to visualize changes to array. It might be a helpful tool for studying sorting algorithms.
You can find out source code with a playground here:
https://github.com/izotx/SwiftAnimations
The source code contains example of using the library with bubble sort.
Library comes with several handy methods for visualizing changes:
swapElements // Responsible for swapping elements in array
selectElements // Selecting and deselecting
insertAtIndex moves element from index a to b and shifts array
up, down, rightLeft, for more granular control over animations
Each of the methods creates and adds an animation to the list of animations.
Here is original Bubble Sort code copied from awesome Swift Algorithms Club: https://github.com/raywenderlich/swift-algorithm-club
func originalBubbleSort(_ array:inout [Int]){
for _ in 0..<array.count {
for j in 1..<array.count {
if array[j] < array[j-1] {
let tmp = array[j-1]
array[j-1] = array[j]
array[j] = tmp
}
}
}
}
In order to use library you have to:
let visualizer:ArrayAnimationView<Int> = ArrayAnimationView(frame: CGRect(x: 0, y: 0, width: 380, height: 50))
self.view.addSubview(visualizer)
var a = [3,2,0,8,5,10,6,9]
visualizer.displayArray(a)
let tmp = array[j-1]
array[j-1] = array[j]
array[j] = tmp
is performing swap of two elements of the array. The code below is a full example of animations
extension ArrayAnimationView{
func bubbleSort(_ array:inout [Int]){
for _ in 0..<array.count {
for j in 1..<array.count {
//Compare two elements: so slide down
if array[j] < array[j-1] {
let tmp = array[j-1]
array[j-1] = array[j]
array[j] = tmp
swapElements(arrayElements[j], arrayElements[j-1], j, j-1)
}
else{
selectElements([arrayElements[j], arrayElements[j-1]], [true,true])
down([arrayElements[j], arrayElements[j-1]])
up([arrayElements[j], arrayElements[j-1]])
selectElements([arrayElements[j], arrayElements[j-1]], [false,false])
}
}
}
}
}