From 4c796cca9566e888ac6c3871f068c00f0a3a2b5e Mon Sep 17 00:00:00 2001 From: "Carlos A. King" Date: Sat, 26 Aug 2017 22:22:54 -0400 Subject: [PATCH 1/2] [Swift 4] Update Shell Sort --- .../Shell Sort.playground/Contents.swift | 34 +++++++++++++++++++ .../contents.xcplayground | 4 +++ 2 files changed, 38 insertions(+) create mode 100644 Shell Sort/Shell Sort.playground/Contents.swift create mode 100644 Shell Sort/Shell Sort.playground/contents.xcplayground diff --git a/Shell Sort/Shell Sort.playground/Contents.swift b/Shell Sort/Shell Sort.playground/Contents.swift new file mode 100644 index 000000000..9e2153f88 --- /dev/null +++ b/Shell Sort/Shell Sort.playground/Contents.swift @@ -0,0 +1,34 @@ +//: Playground - noun: a place where people can play + +import UIKit + +// last checked with Xcode 9.0b4 +#if swift(>=4.0) + print("Hello, Swift 4!") +#endif + +public func insertionSort(_ list: inout [Int], start: Int, gap: Int) { + for i in stride(from: (start + gap), to: list.count, by: gap) { + let currentValue = list[i] + var pos = i + while pos >= gap && list[pos - gap] > currentValue { + list[pos] = list[pos - gap] + pos -= gap + } + list[pos] = currentValue + } +} + +public func shellSort(_ list: inout [Int]) { + var sublistCount = list.count / 2 + while sublistCount > 0 { + for pos in 0.. + + + \ No newline at end of file From 1df06b2a962c63e44cf90aae513c149511d69a19 Mon Sep 17 00:00:00 2001 From: "Carlos A. King" Date: Sat, 26 Aug 2017 22:55:08 -0400 Subject: [PATCH 2/2] Update README.markdown --- Shell Sort/README.markdown | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/Shell Sort/README.markdown b/Shell Sort/README.markdown index c21c29685..72375e51b 100644 --- a/Shell Sort/README.markdown +++ b/Shell Sort/README.markdown @@ -117,24 +117,10 @@ Here is an implementation of Shell Sort in Swift: var arr = [64, 20, 50, 33, 72, 10, 23, -1, 4, 5] public func shellSort(_ list: inout [Int]) { - var sublistCount = list.count / 2 - while sublistCount > 0 { - - for index in 0.. list[index + sublistCount] { - swap(&list[index], &list[index + sublistCount]) - } - - guard sublistCount == 1 && index > 0 else { continue } - - if list[index - 1] > list[index] { - swap(&list[index - 1], &list[index]) - } + for pos in 0..