Skip to content

Impproves documentation of Bubble Sort #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Brute-Force String Search/BruteForceStringSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Brute-force string search
*/
extension String {
/// Searches the string for the pattern provided, returning the index of ocurrency. This algorithm uses a brute force approach.
/// - Parameter pattern: The stering patter to be searched.
/// - Returns: where the pattern starts to occur.
func indexOf(_ pattern: String) -> String.Index? {
for i in self.characters.indices {
var j = i
Expand Down
7 changes: 6 additions & 1 deletion Bubble Sort/MyPlayground.playground/Sources/BubbleSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ import Foundation

/// Performs the bubble sort algorithm in the array
///
/// - Parameter elements: a array of elements that implement the Comparable protocol
/// - Parameter elements: Array of Comparable elements to be sorted
/// - Returns: an array with the same elements but in order
public func bubbleSort<T> (_ elements: [T]) -> [T] where T: Comparable {
return bubbleSort(elements, <)
}

/// Performs the bubble sort algorithm in the array
/// - Parameters:
/// - elements: Array of Comparable elements to be sorted
/// - comparison: The method that will tell the algorithm if two elements are ordered
/// - Returns: an array with the same elements but in order
public func bubbleSort<T> (_ elements: [T], _ comparison: (T,T) -> Bool) -> [T] {
var array = elements

Expand Down
48 changes: 24 additions & 24 deletions Closest Pair/ClosestPair.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
func ClosestPairOf(points: [Point]) -> (minimum:Double, firstPoint:Point, secondPoint:Point) {
var innerPoints = mergeSort(points, sortAccording : true)
let result = ClosestPair(&innerPoints, innerPoints.count)
return (result.minValue, result.firstPoint, result.secondPoint)
return (result.minimumValue, result.firstPoint, result.secondPoint)
}

func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint: Point,secondPoint: Point)
func ClosestPair(_ p : inout [Point],_ n : Int) -> (minimumValue: Double,firstPoint: Point,secondPoint: Point)
{
// Brute force if only 3 points (To end recursion)
if n <= 3
{
var i=0, j = i+1
var minDist = Double.infinity
var minimumDistance = Double.infinity
var newFirst:Point? = nil
var newSecond:Point? = nil
while i<n
{
j = i+1
while j < n
{
if dist(p[i], p[j]) <= minDist
if distance(p[i], p[j]) <= minimumDistance
{
minDist = dist(p[i], p[j])
minimumDistance = distance(p[i], p[j])
newFirst = p[i]
newSecond = p[j]
}
Expand All @@ -31,7 +31,7 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
i+=1

}
return (minDist, newFirst ?? Point(0,0), newSecond ?? Point(0,0))
return (minimumDistance, newFirst ?? Point(0,0), newSecond ?? Point(0,0))
}


Expand All @@ -54,24 +54,24 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:

// Recurse on the left and right part of the array.
let valueFromLeft = ClosestPair(&leftSide, mid)
let minLeft:Double = valueFromLeft.minValue
let minimumLeft:Double = valueFromLeft.minimumValue
let valueFromRight = ClosestPair(&rightSide, n-mid)
let minRight:Double = valueFromRight.minValue
let minimumRight:Double = valueFromRight.minimumValue

// Starting current min must be the largest possible to not affect the real calculations.
var min = Double.infinity
// Starting current minimum must be the largest possible to not affect the real calculations.
var minimum = Double.infinity

var first:Point
var second:Point

// Get the minimum between the left and the right.
if minLeft < minRight {
min = minLeft
if minimumLeft < minimumRight {
minimum = minimumLeft
first = valueFromLeft.firstPoint
second = valueFromLeft.secondPoint
}
else {
min = minRight
minimum = minimumRight
first = valueFromRight.firstPoint
second = valueFromRight.secondPoint
}
Expand All @@ -82,11 +82,11 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:

var strip = [Point]()

// If the value is less than the min distance away in X from the line then take it into consideration.
// If the value is less than the minimum distance away in X from the line then take it into consideration.
var i=0, j = 0
while i<n
{
if abs(p[i].x - line) < min
if abs(p[i].x - line) < minimum
{
strip.append(p[i])
j+=1
Expand All @@ -97,19 +97,19 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:

i=0
var x = i+1
var temp = min
var temp = minimum
var tempFirst:Point = Point(0,0)
var tempSecond:Point = Point(0,0)
// Get the values between the points in the strip but only if it is less min dist in Y.
// Get the values between the points in the strip but only if it is less minimum distance in Y.
while i<j
{
x = i+1
while x < j
{
if (abs(strip[x].y - strip[i].y)) > min { break }
if dist(strip[i], strip[x]) < temp
if (abs(strip[x].y - strip[i].y)) > minimum { break }
if distance(strip[i], strip[x]) < temp
{
temp = dist(strip[i], strip[x])
temp = distance(strip[i], strip[x])
tempFirst = strip[i]
tempSecond = strip[x]
}
Expand All @@ -118,13 +118,13 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
i+=1
}

if temp < min
if temp < minimum
{
min = temp;
minimum = temp;
first = tempFirst
second = tempSecond
}
return (min, first, second)
return (minimum, first, second)
}


Expand Down Expand Up @@ -201,7 +201,7 @@ struct Point
}
}
// Get the distance between two points a, b.
func dist(_ a: Point,_ b: Point) -> Double
func distance(_ a: Point,_ b: Point) -> Double
{
let equation:Double = (((a.x-b.x)*(a.x-b.x))) + (((a.y-b.y)*(a.y-b.y)))
return equation.squareRoot()
Expand Down
2 changes: 1 addition & 1 deletion Closest Pair/ClosestPair.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>