BubbleSort

struct BubbleSort : SortMethod

Implementation of the Bubble sort algorithm.

Bubble sort performs poorly in real world use and is used primarily as an educational tool. Note that using very large arrays slows down the sorting using BubbleSort considerably.

See https://en.wikipedia.org/wiki/Bubble_sort

  • Name of the BubbleSort is “BubbleSort”.

    Declaration

    Swift

    var name: String { get }
  • A short description of the Bubblesort.

    Declaration

    Swift

    var description: String { get }
  • Declaration

    Swift

    var webLinks: [(String, String)] { get }
  • Implements the size property declared in the SortMethod protocol. This is the size of the array to sort.

    Declaration

    Swift

    let size: Int
  • A varible to address the sortable (not yet sorted) size of the array.

    Declaration

    Swift

    private var sortSize: Int
  • A variable to keep track where sorting has advanced to.

    Declaration

    Swift

    private var newSize: Int
  • Loop index variable for sorting the array.

    Declaration

    Swift

    private var innerIndex: Int
  • Initializes the BubbleSort.

    Declaration

    Swift

    init(arraySize: Int)
  • Restarts the bubble sort, resets the member variables.

    Declaration

    Swift

    mutating func restart()
  • Implements the SortMethod protocol with bubble sort method. Note that the algorithm modifies the array size member variable, and that will eventually cause the method returning true. Until that, false is always returned at the end of the method. See protocol documentation for details of the method.

    Declaration

    Swift

    mutating func nextStep(array: [Int], swappedItems: inout SwappedItems) -> Bool
  • Implementation of the BubbleSort in two tight loops.

    Declaration

    Swift

    mutating func realAlgorithm(array: inout [Int])