Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. Here's how to implement the bubble sort algorithm in Swift:
main.swift398 chars19 lines
To use the function, simply pass in an array of comparable elements:
main.swift87 chars4 lines
Note that the inout keyword is used in the function signature to allow the function to modify the array in place.
gistlibby LogSnag