From c0072db422b31d94c087adc58522cb1bd8c2c8e2 Mon Sep 17 00:00:00 2001 From: Pubudu Mihiranga De Silva Date: Tue, 19 Oct 2021 09:07:19 +0530 Subject: [PATCH 1/7] add bubble sort algorithm using swift --- .../Bubble Sort/BubbleSort.swift | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Sorting Algorithms/Bubble Sort/BubbleSort.swift diff --git a/Sorting Algorithms/Bubble Sort/BubbleSort.swift b/Sorting Algorithms/Bubble Sort/BubbleSort.swift new file mode 100644 index 0000000..f71222f --- /dev/null +++ b/Sorting Algorithms/Bubble Sort/BubbleSort.swift @@ -0,0 +1,33 @@ +// +// BubbleSort.swift +// +// +// Created by Pubudu Mihiranga on 19/10/21. +// + +import Foundation + +//MARK: - Bubble Sort algorithm + +func bubbleSort(_ dataArray: [Int]) -> [Int] { + var unsortedArray = dataArray + for _ in 0...unsortedArray.count { + for value in 1...unsortedArray.count - 1 { + if unsortedArray[value-1] > unsortedArray[value] { + let tempValue = unsortedArray[value-1] + unsortedArray[value-1] = unsortedArray[value] + unsortedArray[value] = tempValue + } + } + } + + return unsortedArray +} + +//MARK: - Usage + +let dataArray = [3, 4, 5, 12, 0, 8, 1] +print("Unsorted := \(dataArray)") //output := Unsorted := [3, 4, 5, 1, 0, 8, 1] + +let sortedArray = bubbleSort(dataArray) +print("Sorted := \(sortedArray)") //output := Sorted := [0, 1, 1, 3, 4, 5, 8] From 2a0443b3dc90db550abaf2a0a82630502f65572f Mon Sep 17 00:00:00 2001 From: Pubudu Mihiranga De Silva Date: Tue, 19 Oct 2021 17:45:12 +0530 Subject: [PATCH 2/7] update the bubble sort logic --- .../Bubble Sort/BubbleSort.swift | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/Sorting Algorithms/Bubble Sort/BubbleSort.swift b/Sorting Algorithms/Bubble Sort/BubbleSort.swift index f71222f..0b4b614 100644 --- a/Sorting Algorithms/Bubble Sort/BubbleSort.swift +++ b/Sorting Algorithms/Bubble Sort/BubbleSort.swift @@ -1,6 +1,6 @@ // // BubbleSort.swift -// +// // // Created by Pubudu Mihiranga on 19/10/21. // @@ -11,16 +11,26 @@ import Foundation func bubbleSort(_ dataArray: [Int]) -> [Int] { var unsortedArray = dataArray - for _ in 0...unsortedArray.count { - for value in 1...unsortedArray.count - 1 { - if unsortedArray[value-1] > unsortedArray[value] { - let tempValue = unsortedArray[value-1] - unsortedArray[value-1] = unsortedArray[value] - unsortedArray[value] = tempValue + + if unsortedArray.count <= 1 { + return dataArray + } + + let lastPosition = unsortedArray.count - 1 + var shouldSwap = true + + while shouldSwap { + shouldSwap = false + for i in 1...lastPosition { + if unsortedArray[i-1] > unsortedArray[i] { + let tempValue = unsortedArray[i-1] + unsortedArray[i-1] = unsortedArray[i] + unsortedArray[i] = tempValue + shouldSwap = true } } } - + return unsortedArray } @@ -31,3 +41,4 @@ print("Unsorted := \(dataArray)") //output := Unsorted := [3, 4, 5, 1, 0, 8, 1] let sortedArray = bubbleSort(dataArray) print("Sorted := \(sortedArray)") //output := Sorted := [0, 1, 1, 3, 4, 5, 8] + From 7039cdb43f936dfd8859a7de18da435aa507ed4b Mon Sep 17 00:00:00 2001 From: Pubudu Mihiranga De Silva Date: Wed, 20 Oct 2021 00:09:51 +0530 Subject: [PATCH 3/7] update bubble sort algorithm logic --- .../Bubble Sort/BubbleSort.swift | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Sorting Algorithms/Bubble Sort/BubbleSort.swift b/Sorting Algorithms/Bubble Sort/BubbleSort.swift index 0b4b614..107b41a 100644 --- a/Sorting Algorithms/Bubble Sort/BubbleSort.swift +++ b/Sorting Algorithms/Bubble Sort/BubbleSort.swift @@ -9,11 +9,16 @@ import Foundation //MARK: - Bubble Sort algorithm -func bubbleSort(_ dataArray: [Int]) -> [Int] { - var unsortedArray = dataArray +func bubbleSort(_ dataArray: [Int]?) -> [Int] { - if unsortedArray.count <= 1 { - return dataArray + //check if dataArray is nil + guard var unsortedArray = dataArray else { + return [] + } + + //check if dataArray length is equal 1 + if unsortedArray.count == 1 { + return dataArray! } let lastPosition = unsortedArray.count - 1 @@ -36,9 +41,18 @@ func bubbleSort(_ dataArray: [Int]) -> [Int] { //MARK: - Usage -let dataArray = [3, 4, 5, 12, 0, 8, 1] -print("Unsorted := \(dataArray)") //output := Unsorted := [3, 4, 5, 1, 0, 8, 1] +let dataArray1 = [3, 4, 5, 12, 0, 8, 1] +print("Unsorted := \(dataArray1)") //output := Unsorted := [3, 4, 5, 1, 0, 8, 1] +let sortedArray1 = bubbleSort(dataArray1) +print("Sorted := \(sortedArray1)") //output := Sorted := [0, 1, 1, 3, 4, 5, 8] + +let dataArray2: [Int]? = nil +print("Unsorted := \(dataArray2)") //output := Unsorted := nil +let sortedArray2 = bubbleSort(dataArray2) +print("Sorted := \(sortedArray2)") //output := Sorted := [] -let sortedArray = bubbleSort(dataArray) -print("Sorted := \(sortedArray)") //output := Sorted := [0, 1, 1, 3, 4, 5, 8] +let dataArray3 = [5] +print("Unsorted := \(dataArray3)") //output := Unsorted := [5] +let sortedArray3 = bubbleSort(dataArray3) +print("Sorted := \(sortedArray3)") //output := Sorted := [5] From 0995c31b8eb12f05eba7205ad1b889dbfa00b308 Mon Sep 17 00:00:00 2001 From: Pubudu Mihiranga De Silva Date: Wed, 20 Oct 2021 00:11:57 +0530 Subject: [PATCH 4/7] update bubble sort algorithm logic --- Sorting Algorithms/Bubble Sort/BubbleSort.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorting Algorithms/Bubble Sort/BubbleSort.swift b/Sorting Algorithms/Bubble Sort/BubbleSort.swift index 107b41a..d6aa294 100644 --- a/Sorting Algorithms/Bubble Sort/BubbleSort.swift +++ b/Sorting Algorithms/Bubble Sort/BubbleSort.swift @@ -18,7 +18,7 @@ func bubbleSort(_ dataArray: [Int]?) -> [Int] { //check if dataArray length is equal 1 if unsortedArray.count == 1 { - return dataArray! + return unsortedArray } let lastPosition = unsortedArray.count - 1 From ec275f4df58b8d3f3460eacd326fc3b6db90e996 Mon Sep 17 00:00:00 2001 From: Pubudu Mihiranga De Silva Date: Wed, 20 Oct 2021 00:32:38 +0530 Subject: [PATCH 5/7] update bubble sort algorithm logic --- Sorting Algorithms/Bubble Sort/BubbleSort.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorting Algorithms/Bubble Sort/BubbleSort.swift b/Sorting Algorithms/Bubble Sort/BubbleSort.swift index d6aa294..42560f5 100644 --- a/Sorting Algorithms/Bubble Sort/BubbleSort.swift +++ b/Sorting Algorithms/Bubble Sort/BubbleSort.swift @@ -17,7 +17,7 @@ func bubbleSort(_ dataArray: [Int]?) -> [Int] { } //check if dataArray length is equal 1 - if unsortedArray.count == 1 { + if unsortedArray.count < 2 { return unsortedArray } From 26ba069f1974dc83503b956988b13b40624c44e7 Mon Sep 17 00:00:00 2001 From: Pubudu Mihiranga <66482798+pubudu-m@users.noreply.github.com> Date: Wed, 20 Oct 2021 07:54:34 +0530 Subject: [PATCH 6/7] Update BubbleSort.swift --- Sorting Algorithms/Bubble Sort/BubbleSort.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorting Algorithms/Bubble Sort/BubbleSort.swift b/Sorting Algorithms/Bubble Sort/BubbleSort.swift index 42560f5..1363fc0 100644 --- a/Sorting Algorithms/Bubble Sort/BubbleSort.swift +++ b/Sorting Algorithms/Bubble Sort/BubbleSort.swift @@ -17,7 +17,7 @@ func bubbleSort(_ dataArray: [Int]?) -> [Int] { } //check if dataArray length is equal 1 - if unsortedArray.count < 2 { + guard unsortedArray.count > 1 else { return unsortedArray } From 8fa3b7d0230dd4d8d08877612cf5aa5e1983b568 Mon Sep 17 00:00:00 2001 From: Pubudu Mihiranga <66482798+pubudu-m@users.noreply.github.com> Date: Wed, 20 Oct 2021 12:12:49 +0530 Subject: [PATCH 7/7] Update BubbleSort.swift --- Sorting Algorithms/Bubble Sort/BubbleSort.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Sorting Algorithms/Bubble Sort/BubbleSort.swift b/Sorting Algorithms/Bubble Sort/BubbleSort.swift index 1363fc0..f1dab72 100644 --- a/Sorting Algorithms/Bubble Sort/BubbleSort.swift +++ b/Sorting Algorithms/Bubble Sort/BubbleSort.swift @@ -13,7 +13,7 @@ func bubbleSort(_ dataArray: [Int]?) -> [Int] { //check if dataArray is nil guard var unsortedArray = dataArray else { - return [] + fatalError("Bubble Sort failed") } //check if dataArray length is equal 1 @@ -28,9 +28,7 @@ func bubbleSort(_ dataArray: [Int]?) -> [Int] { shouldSwap = false for i in 1...lastPosition { if unsortedArray[i-1] > unsortedArray[i] { - let tempValue = unsortedArray[i-1] - unsortedArray[i-1] = unsortedArray[i] - unsortedArray[i] = tempValue + unsortedArray.swapAt(i-1, i) shouldSwap = true } } @@ -49,7 +47,7 @@ print("Sorted := \(sortedArray1)") //output := Sorted := [0, 1, 1, 3, 4, 5, 8] let dataArray2: [Int]? = nil print("Unsorted := \(dataArray2)") //output := Unsorted := nil let sortedArray2 = bubbleSort(dataArray2) -print("Sorted := \(sortedArray2)") //output := Sorted := [] +print("Sorted := \(sortedArray2)") //output := Fatal error: Bubble Sort failed let dataArray3 = [5] print("Unsorted := \(dataArray3)") //output := Unsorted := [5]