From 299a67caeb984577561aaf1921271015558bfd6e Mon Sep 17 00:00:00 2001 From: Julio Brazil Date: Mon, 29 Oct 2018 11:45:21 -0300 Subject: [PATCH] addition of convenience method --- Bubble Sort/MyPlayground.playground/Contents.swift | 4 +++- .../MyPlayground.playground/Sources/BubbleSort.swift | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Bubble Sort/MyPlayground.playground/Contents.swift b/Bubble Sort/MyPlayground.playground/Contents.swift index 8d27d801e..6188a0763 100644 --- a/Bubble Sort/MyPlayground.playground/Contents.swift +++ b/Bubble Sort/MyPlayground.playground/Contents.swift @@ -3,4 +3,6 @@ import Foundation var array = [4,2,1,3] print("before:",array) -print("after:",BubbleSort(array)) +print("after:", bubbleSort(array)) +print("after:", bubbleSort(array, <)) +print("after:", bubbleSort(array, >)) diff --git a/Bubble Sort/MyPlayground.playground/Sources/BubbleSort.swift b/Bubble Sort/MyPlayground.playground/Sources/BubbleSort.swift index 9c091b661..09e01542b 100644 --- a/Bubble Sort/MyPlayground.playground/Sources/BubbleSort.swift +++ b/Bubble Sort/MyPlayground.playground/Sources/BubbleSort.swift @@ -23,14 +23,18 @@ import Foundation /// Performs the bubble sort algorithm in the array /// -/// - Parameter elements: the array to be sorted +/// - Parameter elements: a array of elements that implement the Comparable protocol /// - Returns: an array with the same elements but in order -public func BubbleSort (_ elements: [T]) -> [T] where T: Comparable { +public func bubbleSort (_ elements: [T]) -> [T] where T: Comparable { + return bubbleSort(elements, <) +} + +public func bubbleSort (_ elements: [T], _ comparison: (T,T) -> Bool) -> [T] { var array = elements for i in 0..