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
// | |
// Base64URLUtils.swift | |
// CodeRepo | |
// | |
// Created by tramp on 23/04/2018. | |
// Copyright © 2018 tramp. All rights reserved. | |
// | |
import Foundation |
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
/// https://www.raywenderlich.com/741-swift-algorithm-club-swift-merge-sort | |
/// divide and conquer | |
/// merge sort O(nlogn) | |
/// | |
/// - Parameter array: unsorted array | |
/// - Returns: sorted array | |
func mergeSort<T: Comparable>(_ array: [T]) -> [T] { | |
guard array.count > 1 else { return array } | |
let middleIndex = array.count / 2 |
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
/// from swift-algorithm-club | |
/// O(n^2) | |
func insertionSort(_ array: [Int]) -> [Int] { | |
var a = array | |
for x in (1..<a.count) { | |
var y = x | |
while y > 0 && a[y] < a[y - 1] { | |
a.swapAt(y - 1, y) |
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
func selectionSort(_ array: [Int]) -> [Int] { | |
guard array.count > 1 else { return array } | |
var a = array | |
for x in 0..<(a.count - 1) { | |
var lowest = x | |
for y in x + 1 ..< a.count { | |
if a[y] < a[lowest] { | |
lowest = y | |
} | |
} |
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
func selectionSort(_ array: [Int]) -> [Int] { | |
guard array.count > 1 else { return array } | |
var a = array | |
for x in 0..<(a.count - 1) { | |
var lowest = x | |
for y in x + 1 ..< a.count { | |
if a[y] < a[lowest] { | |
lowest = y | |
} | |
} |
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
func selectionSort(_ array: [Int]) -> [Int] { | |
guard array.count > 1 else { return array } | |
var a = array | |
for x in 0..<(a.count - 1) { | |
var lowest = x | |
for y in x + 1 ..< a.count { | |
if a[y] < a[lowest] { | |
lowest = y | |
} | |
} |
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
// use a button click to show how layoutIfNeeded() works | |
// article link: https://medium.com/@abhimuralidharan/ios-swift-setneedslayout-vs-layoutifneeded-vs-layoutsubviews-5a2b486da31c | |
@IBAction func heightPressed(_ sender: AnyObject) { | |
view.layoutIfNeeded() | |
if(self.blueHeight.constant == 25.0) | |
{ | |
self.blueHeight.constant = self.view.bounds.height - 100.0 | |
} | |
else | |
{ |
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
/// 为UIView的内容生成一张图片 | |
/// | |
/// - Parameter view: 生成图片的View | |
/// - Returns: 返回生成的图片 | |
func generatePictureIn(view: UIView) -> UIImage? { | |
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0) | |
guard let ctx = UIGraphicsGetCurrentContext() else { return nil } | |
view.layer.render(in: ctx) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() |
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
/// from Data Structures & Algorithms in Swift book | |
/// bubble sort worst(O(n^2), if array is already sorted, best is O(n)) | |
/// | |
/// - Parameter array: target array | |
public func bubbleSort<Element>(_ array: inout [Element]) where Element: Comparable { | |
guard array.count > 2 else { return } | |
for end in (1..<array.count).reversed() { | |
var swapped = false // optimize, if array is already sorted, do less useless work | |
for current in 0..<end { | |
if array[current] > array[current + 1] { |
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
// | |
// UIDevice+XMUtils.h | |
// AwesomeTips | |
// | |
// Created by kangzubin on 2018/9/20. | |
// Copyright © 2018 KANGZUBIN. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> |
OlderNewer