Skip to content

Commit

Permalink
Merge pull request #36 from azolinmf/master
Browse files Browse the repository at this point in the history
Cocktail sort algorithm
  • Loading branch information
carloshmartins authored May 13, 2024
2 parents 00ac786 + 3affd0b commit 7097b14
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@

## Sorts
* [Bubblesort](https://github.com/TheAlgorithms/Swift/blob/master/sorts/BubbleSort.swift)
* [Cocktailsort](https://github.com/TheAlgorithms/Swift/blob/master/sorts/CocktailSort.swift)
* [Insertionsort](https://github.com/TheAlgorithms/Swift/blob/master/sorts/InsertionSort.swift)
* [Mergesort](https://github.com/TheAlgorithms/Swift/blob/master/sorts/MergeSort.swift)
* [Pancakesort](https://github.com/TheAlgorithms/Swift/blob/master/sorts/PancakeSort.swift)
* [Quicksort](https://github.com/TheAlgorithms/Swift/blob/master/sorts/QuickSort.swift)
* [Selectionsort](https://github.com/TheAlgorithms/Swift/blob/master/sorts/SelectionSort.swift)

Expand Down
49 changes: 49 additions & 0 deletions sorts/CocktailSort.swift
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)
51 changes: 51 additions & 0 deletions sorts/PancakeSort.swift
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)

0 comments on commit 7097b14

Please sign in to comment.