diff --git a/DIRECTORY.md b/DIRECTORY.md index e469005..fc30603 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/sorts/CocktailSort.swift b/sorts/CocktailSort.swift new file mode 100644 index 0000000..d9b1110 --- /dev/null +++ b/sorts/CocktailSort.swift @@ -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(_ 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.. 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) diff --git a/sorts/PancakeSort.swift b/sorts/PancakeSort.swift new file mode 100644 index 0000000..3a016fb --- /dev/null +++ b/sorts/PancakeSort.swift @@ -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)