-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from azolinmf/master
Cocktail sort algorithm
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
/* | ||
Cocktail Sort (or Cocktail shaker sort) is a variation of Bubble sort. | ||
The Bubble sort algorithm always traverses elements from left and moves the largest element | ||
to its correct position in first iteration and second largest in second iteration and so on. | ||
Cocktail Sort traverses through a given array in both directions alternatively. | ||
*/ | ||
|
||
import Foundation | ||
|
||
func cocktailSort<T: Comparable>(_ a: [T]) -> [T] { | ||
var list = a | ||
var swapped = true | ||
var start = 0 | ||
var end = list.count - 1 | ||
|
||
while (swapped) { | ||
swapped = false | ||
|
||
for i in start..<end { | ||
if (list[i] > list[i + 1]) { | ||
list.swapAt(i, i+1) | ||
swapped = true | ||
} | ||
} | ||
|
||
if (!swapped) { | ||
break | ||
} | ||
swapped = false | ||
end -= 1 | ||
|
||
for index in stride(from: end-1, through: start, by: -1) { | ||
if (list[index] > list[index + 1]) { | ||
list.swapAt(index, index+1) | ||
swapped = true | ||
} | ||
} | ||
start += 1 | ||
} | ||
|
||
return list | ||
} | ||
|
||
// The code below can be used for testing | ||
|
||
//var numbers = [2, -4, 4, 6, 1, 12, 9, 0] | ||
//numbers = cocktailSort(numbers) | ||
//print(numbers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
/* | ||
Pancake sorting is the mathematical problem of sorting a disordered stack | ||
of pancakes in order of size when a spatula can be inserted at any | ||
point in the stack and used to flip all pancakes above it. | ||
*/ | ||
|
||
import Foundation | ||
|
||
func flip(array: [Int], key: Int) -> [Int] { | ||
var flippedArray = array | ||
var pos = key | ||
var start = 0 | ||
var aux = 0 | ||
|
||
while (start < pos) { | ||
aux = flippedArray[start] | ||
flippedArray[start] = flippedArray[pos] | ||
flippedArray[pos] = aux | ||
|
||
start += 1 | ||
pos -= 1 | ||
} | ||
|
||
return flippedArray | ||
} | ||
|
||
func pancakeSort(_ array: [Int]) -> [Int] { | ||
var list = array | ||
var currentSize = list.count | ||
for _ in (1 ..< currentSize).reversed() { | ||
|
||
let listToSearch = list[0...currentSize-1] | ||
let max = listToSearch.max() ?? 0 | ||
let indexOfMax = listToSearch.firstIndex(of: max) ?? 0 | ||
|
||
if indexOfMax != currentSize - 1 { | ||
list = flip(array: list, key: indexOfMax) | ||
list = flip(array: list, key: currentSize - 1) | ||
} | ||
|
||
currentSize -= 1 | ||
} | ||
|
||
return list | ||
} | ||
|
||
// The code below can be used for testing | ||
//var numbers = [2, 4, 6, 12, 3, -2, 9, 14, 22, 0, 18] | ||
//numbers = pancakeSort(numbers) | ||
//print(numbers) |