Skip to content

Instantly share code, notes, and snippets.

View 0xABCCBA's full-sized avatar
🏠
Working from home

3.14≤π 0xABCCBA

🏠
Working from home
View GitHub Profile
//
// Base64URLUtils.swift
// CodeRepo
//
// Created by tramp on 23/04/2018.
// Copyright © 2018 tramp. All rights reserved.
//
import Foundation
@0xABCCBA
0xABCCBA / MergeSort.swift
Last active August 29, 2018 02:23
MergeSort
/// 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
@0xABCCBA
0xABCCBA / InsertionSort.swift
Created August 30, 2018 06:08
InsertionSort
/// 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)
@0xABCCBA
0xABCCBA / SelectionSort.swift
Created August 31, 2018 01:05
SelectionSort
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
}
}
@0xABCCBA
0xABCCBA / SelectionSort.swift
Created August 31, 2018 01:05
SelectionSort
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
}
}
@0xABCCBA
0xABCCBA / SelectionSort.swift
Created August 31, 2018 01:05
SelectionSort
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
}
}
@0xABCCBA
0xABCCBA / layoutIfNeeded.swift
Created January 8, 2019 02:53
use layoutIfNeeded
// 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
{
@0xABCCBA
0xABCCBA / generatePictureForAView.swift
Created January 21, 2019 07:25
generatePictureForAView
/// 为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()
@0xABCCBA
0xABCCBA / BubbleSort.swift
Last active August 26, 2019 09:25
BubbleSort
/// 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] {
@0xABCCBA
0xABCCBA / UIDevice+XMUtils.h
Created October 15, 2019 11:31 — forked from kangzubin/UIDevice+XMUtils.h
The sample code to get iPhone device name.
//
// UIDevice+XMUtils.h
// AwesomeTips
//
// Created by kangzubin on 2018/9/20.
// Copyright © 2018 KANGZUBIN. All rights reserved.
//
#import <UIKit/UIKit.h>