├── .gitignore
├── 3Sum and 4Sum
└── README.md
├── AVL Tree
├── AVLTree.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── AVLTree.swift
├── Images
│ ├── BalanceNotOK.graffle
│ ├── BalanceNotOK.png
│ ├── BalanceOK.graffle
│ ├── BalanceOK.png
│ ├── Balanced.graffle
│ ├── Balanced.png
│ ├── Height.graffle
│ ├── Height.png
│ ├── RotationStep0.jpg
│ ├── RotationStep1.jpg
│ ├── RotationStep2.jpg
│ ├── RotationStep3.jpg
│ ├── Unbalanced.graffle
│ └── Unbalanced.png
├── README.markdown
└── README_en.markdown
├── Algorithm Design.markdown
├── All-Pairs Shortest Paths
├── README.markdown
├── README_en.markdown
└── img
│ ├── d0.png
│ ├── d1.png
│ ├── d2.png
│ ├── d3.png
│ ├── example_graph.png
│ ├── original_adjacency_matrix.png
│ ├── pi0.png
│ ├── pi1.png
│ ├── pi2.png
│ ├── pi3.png
│ └── weight_comparison_formula.png
├── Array2D
├── Array2D.playground
│ ├── Contents.swift
│ └── contents.xcplayground
├── README.markdown
└── README_en.markdown
├── B-Tree
├── BTree.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── BTree.swift
│ └── contents.xcplayground
├── Images
│ ├── BTree20.png
│ ├── InsertionSplit.png
│ ├── MergingNodes.png
│ ├── MovingKey.png
│ └── Node.png
├── README.md
└── README_en.md
├── Big-O Notation.markdown
├── Binary Search Tree
├── Images
│ ├── DeleteLeaf.graffle
│ ├── DeleteLeaf.png
│ ├── DeleteOneChild.graffle
│ ├── DeleteOneChild.png
│ ├── DeleteTwoChildren.graffle
│ ├── DeleteTwoChildren.png
│ ├── MinimumMaximum.graffle
│ ├── MinimumMaximum.png
│ ├── Searching.graffle
│ ├── Searching.png
│ ├── Traversing.graffle
│ ├── Traversing.png
│ ├── Tree1.graffle
│ ├── Tree1.png
│ ├── Tree2.graffle
│ └── Tree2.png
├── README.markdown
├── README_en.markdown
├── Solution 1
│ └── BinarySearchTree.playground
│ │ ├── Contents.swift
│ │ ├── contents.xcplayground
│ │ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ └── andyron.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ │ └── timeline.xctimeline
└── Solution 2
│ └── BinarySearchTree.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ └── andyron.xcuserdatad
│ └── UserInterfaceState.xcuserstate
├── Binary Search
├── BinarySearch.playground
│ ├── Contents.swift
│ └── contents.xcplayground
├── README.markdown
└── README_en.markdown
├── Binary Tree
├── BinaryTree.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── Images
│ ├── BinaryTree.graffle
│ ├── BinaryTree.png
│ ├── Operations.graffle
│ └── Operations.png
├── README.markdown
└── README_en.markdown
├── Bit Set
├── BitSet.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── BitSet.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Bloom Filter
├── BloomFilter.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Bounded Priority Queue
├── BoundedPriorityQueue.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── BoundedPriorityQueue.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Boyer-Moore-Horspool
├── BoyerMooreHorspool.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Breadth-First Search
├── BreadthFirstSearch.playground
│ ├── Contents.swift
│ ├── Sources
│ │ ├── Edge.swift
│ │ ├── Graph.swift
│ │ ├── Node.swift
│ │ └── Queue.swift
│ └── contents.xcplayground
├── Images
│ ├── AnimatedExample.gif
│ ├── AnimatedExample.graffle
│ ├── AnimatedExample.psd
│ ├── TraversalTree.graffle
│ └── TraversalTree.png
├── README.markdown
└── README_en.markdown
├── Brute-Force String Search
├── BruteForceStringSearch.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ └── andyron.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── timeline.xctimeline
├── README.markdown
└── README_en.markdown
├── Bubble Sort
├── BubbleSort.playground
│ ├── Contents.swift
│ └── contents.xcplayground
├── README.markdown
└── README_en.markdown
├── CNAME
├── Comb Sort
├── README.markdown
└── README_en.markdown
├── Combinatorics
├── README.markdown
└── README_en.markdown
├── Convex Hull
├── README.md
└── README_en.md
├── Count Occurrences
├── CountOccurrences.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Counting Sort
├── CountingSort.playground
│ ├── Contents.swift
│ └── contents.xcplayground
├── README.markdown
└── README_en.markdown
├── Depth-First Search
├── DepthFirstSearch.playground
│ ├── Contents.swift
│ ├── Sources
│ │ ├── Edge.swift
│ │ ├── Graph.swift
│ │ └── Node.swift
│ └── contents.xcplayground
├── Images
│ ├── AnimatedExample.gif
│ ├── AnimatedExample.graffle
│ ├── AnimatedExample.psd
│ ├── TraversalTree.graffle
│ └── TraversalTree.png
├── README.markdown
└── README_en.markdown
├── Deque
├── Deque-Optimized.swift
├── Deque-Simple.swift
├── Deque.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Dijkstra Algorithm
├── Images
│ ├── DirectedGraph.png
│ ├── Vertices.png
│ ├── WeightedDirectedGraph.png
│ ├── WeightedDirectedGraphFinal.png
│ ├── WeightedUndirectedGraph.png
│ ├── image1.png
│ ├── image2.png
│ ├── image3.png
│ ├── image4.png
│ ├── image5.png
│ ├── image6.png
│ └── image7.png
├── README.md
└── README_en.md
├── DiningPhilosophers
└── README.md
├── Egg Drop Problem
├── README.markdown
└── images
│ ├── eggdrop.png
│ └── eggdrop2.png
├── Encode and Decode Tree
└── readme.md
├── Fixed Size Array
├── FixedSizeArray.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── Images
│ ├── FixedSizeArrays.graffle
│ ├── append.png
│ ├── array.png
│ ├── delete-no-copy.png
│ ├── delete.png
│ ├── indexing.png
│ └── insert.png
├── README.markdown
└── README_en.markdown
├── Fizz Buzz
└── README.markdown
├── GCD
├── GCD.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── GCD.swift
│ └── contents.xcplayground
├── README.markdown
└── README_en.markdown
├── Graph
├── Graph
│ ├── Graph.xcodeproj
│ │ ├── project.pbxproj
│ │ ├── project.xcworkspace
│ │ │ ├── contents.xcworkspacedata
│ │ │ ├── xcshareddata
│ │ │ │ └── IDEWorkspaceChecks.plist
│ │ │ └── xcuserdata
│ │ │ │ └── andyron.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcuserdata
│ │ │ └── andyron.xcuserdatad
│ │ │ ├── xcdebugger
│ │ │ └── Breakpoints_v2.xcbkptlist
│ │ │ └── xcschemes
│ │ │ └── xcschememanagement.plist
│ └── Graph
│ │ ├── AdjacencyListGraph.swift
│ │ ├── AdjacencyMatrixGraph.swift
│ │ ├── AppDelegate.swift
│ │ ├── Edge.swift
│ │ ├── Graph.entitlements
│ │ ├── Graph.swift
│ │ ├── Info.plist
│ │ ├── Vertex.swift
│ │ └── other
│ │ ├── Assets.xcassets
│ │ ├── AppIcon.appiconset
│ │ │ └── Contents.json
│ │ └── Contents.json
│ │ └── Base.lproj
│ │ └── MainMenu.xib
├── Images
│ ├── AdjacencyList.graffle
│ ├── AdjacencyList.png
│ ├── AdjacencyMatrix.graffle
│ ├── AdjacencyMatrix.png
│ ├── ChordMap.graffle
│ ├── ChordMap.png
│ ├── CoreData.graffle
│ ├── CoreData.png
│ ├── DAG.graffle
│ ├── DAG.png
│ ├── Demo1.graffle
│ ├── Demo1.png
│ ├── Flights.graffle
│ ├── Flights.png
│ ├── FlightsDirected.graffle
│ ├── FlightsDirected.png
│ ├── Graph.graffle
│ ├── Graph.png
│ ├── SocialNetwork.graffle
│ ├── SocialNetwork.png
│ ├── StateMachine.graffle
│ ├── StateMachine.png
│ ├── Tasks.graffle
│ ├── Tasks.png
│ ├── TreeAndList.graffle
│ └── TreeAndList.png
├── README.markdown
└── README_en.markdown
├── Hash Set
├── HashSet.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── HashSet.swift
│ └── contents.xcplayground
├── Images
│ ├── CombineSets.graffle
│ └── CombineSets.png
├── README.markdown
└── README_en.markdown
├── Hash Table
├── HashTable.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── HaversineDistance
├── HaversineDistance.playground
│ ├── Contents.swift
│ └── contents.xcplayground
├── README.md
└── README_en.md
├── Heap Sort
├── HeapSort.playground
│ ├── Contents.swift
│ └── contents.xcplayground
├── HeapSort.swift
├── Images
│ ├── MaxHeap.graffle
│ └── MaxHeap.png
├── README.markdown
└── README_en.markdown
├── Heap
├── Heap.swift
├── Images
│ ├── Array.graffle
│ ├── Array.png
│ ├── Heap1.graffle
│ ├── Heap1.png
│ ├── HeapShape.graffle
│ ├── HeapShape.png
│ ├── Insert1.graffle
│ ├── Insert1.png
│ ├── Insert2.graffle
│ ├── Insert2.png
│ ├── Insert3.graffle
│ ├── Insert3.png
│ ├── LargeHeap.graffle
│ ├── LargeHeap.png
│ ├── RegularTree.graffle
│ ├── RegularTree.png
│ ├── Remove1.graffle
│ ├── Remove1.png
│ ├── Remove2.graffle
│ ├── Remove2.png
│ ├── Remove3.graffle
│ ├── Remove3.png
│ ├── Remove4.graffle
│ ├── Remove4.png
│ ├── Remove5.graffle
│ ├── Remove5.png
│ ├── SortedArray.graffle
│ └── SortedArray.png
├── README.markdown
└── README_en.markdown
├── Huffman Coding
├── Images
│ ├── BuildTree.gif
│ ├── BuildTree.graffle
│ ├── BuildTree.psd
│ ├── Compression.graffle
│ ├── Compression.png
│ ├── Decompression.graffle
│ ├── Decompression.png
│ ├── Tree.graffle
│ └── Tree.png
├── README.markdown
└── README_en.markdown
├── Images
├── SwiftAlgorithm-410-transp.png
└── scheme-settings-for-travis.png
├── Insertion Sort
├── InsertionSort.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
├── README_en.markdown
└── Tests
│ ├── Info.plist
│ ├── InsertionSortTests.swift
│ └── Tests.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ ├── xcshareddata
│ └── xcschemes
│ │ └── Tests.xcscheme
│ └── xcuserdata
│ └── andyron.xcuserdatad
│ └── xcschemes
│ └── xcschememanagement.plist
├── Introsort
├── Introsort.playground
│ ├── Contents.swift
│ ├── Sources
│ │ ├── HeapSort.swift
│ │ ├── InsertionSort.swift
│ │ ├── Partition.swift
│ │ ├── Randomize.swift
│ │ └── Sort3.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ └── andyron.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── timeline.xctimeline
├── README.markdown
└── README_en.markdown
├── K-Means
├── Images
│ ├── k_means_bad1.png
│ ├── k_means_bad2.png
│ └── k_means_good.png
├── README.markdown
└── README_en.markdown
├── Karatsuba Multiplication
├── README.markdown
└── README_en.markdown
├── Knuth-Morris-Pratt
├── KnuthMorrisPratt.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Kth Largest Element
├── README.markdown
├── README_en.markdown
└── kthLargest.playground
│ ├── Contents.swift
│ ├── Sources
│ └── kthLargest.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ └── andyron.xcuserdatad
│ └── UserInterfaceState.xcuserstate
├── Linear Regression
├── Images
│ ├── graph1.png
│ ├── graph2.png
│ └── graph3.png
├── README.markdown
└── README_en.markdown
├── Linear Search
├── LinearSearch.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── LinearSearch.swift
├── README.markdown
└── README_en.markdown
├── Linked List
├── LinkedList.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Longest Common Subsequence
├── README.markdown
└── README_en.markdown
├── Merge Sort
├── MergeSort.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── MergeSort.swift
├── README.markdown
└── README_en.markdown
├── Miller-Rabin Primality Test
├── Images
│ └── img_pseudo.png
├── README.markdown
└── README_en.markdown
├── Minimum Spanning Tree(Unweighted)
├── Images
│ ├── Graph.png
│ ├── Graph.sketch
│ ├── MinimumSpanningTree.png
│ ├── MinimumSpanningTree.sketch
│ ├── Tree.graffle
│ └── Tree.png
├── MinimumSpanningTree(unweighted).playground
│ ├── Contents.swift
│ ├── Sources
│ │ ├── Edge.swift
│ │ ├── Graph.swift
│ │ ├── Node.swift
│ │ └── Queue.swift
│ └── contents.xcplayground
├── README.markdown
└── README_en.markdown
├── Minimum Spanning Tree
├── Images
│ ├── kruskal.png
│ └── prim.png
├── MinimumSpanningTree.playground
│ ├── Contents.swift
│ ├── Resources
│ │ └── mst.png
│ ├── Sources
│ │ ├── Graph.swift
│ │ ├── Heap.swift
│ │ ├── PriorityQueue.swift
│ │ └── UnionFind.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── MinimumCoinChange
├── README.md
├── README_en.md
└── eurocoins.gif
├── Monty Hall Problem
└── README.markdown
├── Multiset
├── Multiset.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── Multiset.swift
│ └── contents.xcplayground
├── README.markdown
└── README_en.markdown
├── Naive Bayes Classifier
├── README.md
├── README_en.md
└── images
│ ├── bayes.gif
│ ├── code_example.png
│ ├── mean.gif
│ ├── multinomial.gif
│ ├── normal_distribution.gif
│ ├── standard_deviation.gif
│ └── tennis_dataset.png
├── Octree
├── OcTree.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── OcTree.swift
│ └── contents.xcplayground
├── README.md
└── README_en.md
├── Ordered Array
├── OrderedArray.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── Ordered Set
├── OrderedSet.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── OrderedSet.swift
│ └── contents.xcplayground
├── README.markdown
└── README_en.markdown
├── Palindromes
└── README.markdown
├── Priority Queue
├── PriorityQueue.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── Heap.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── QuadTree
├── Images
│ └── quadtree.png
├── QuadTree.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── QuadTree.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.md
└── README_en.md
├── Queue
├── Queue-Optimized.swift
├── Queue-Simple.swift
├── Queue.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
├── README_en.markdown
└── Tests
│ ├── Info.plist
│ ├── QueueTests.swift
│ └── Tests.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ └── contents.xcworkspacedata
│ └── xcshareddata
│ └── xcschemes
│ └── Tests.xcscheme
├── Quicksort
├── Images
│ ├── Example.graffle
│ └── Example.png
├── Quicksort.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── README.markdown
└── README_en.markdown
├── README.markdown
├── Rabin-Karp
├── README.markdown
└── README_en.markdown
├── Radix Sort
├── README.md
├── README_en.md
├── RadixSort.playground
│ ├── Contents.swift
│ └── contents.xcplayground
└── image
│ └── radix_sort.jpg
├── Red-Black Tree
├── README.markdown
├── README_en.markdown
└── RedBlackTree.playground
│ ├── Contents.swift
│ ├── Sources
│ └── RedBlackTree.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── timeline.xctimeline
├── Ring Buffer
├── README.markdown
├── README_en.markdown
└── RingBuffer.playground
│ ├── Contents.swift
│ ├── Sources
│ └── RingBuffer.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ └── andyron.xcuserdatad
│ └── UserInterfaceState.xcuserstate
├── Rootish Array Stack
├── README.md
├── README_en.md
├── RootishArrayStack.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
└── images
│ ├── RootishArrayStackExample.png
│ ├── RootishArrayStackExample2.png
│ ├── RootishArrayStackIntro.png
│ ├── quadratic_equation.jpg
│ └── quadratic_formula.jpg
├── Run-Length Encoding
├── README.markdown
└── README_en.markdown
├── SUMMARY.md
├── Schedule.markdown
├── Segment Tree
├── Images
│ ├── EqualSegments.png
│ ├── LeftSegment.png
│ ├── MixedSegment.png
│ ├── RightSegment.png
│ └── Structure.png
├── LazyPropagation
│ ├── Images
│ │ ├── Segment-tree.png
│ │ ├── lazy-sample-2.png
│ │ ├── pushUp.png
│ │ └── pushdown.png
│ ├── README.markdown
│ └── README_en.markdown
├── README.markdown
├── README_en.markdown
└── SegmentTree.playground
│ ├── Contents.swift
│ └── contents.xcplayground
├── Select Minimum Maximum
├── README.markdown
├── README_en.markdown
└── SelectMinimumMaximum.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── timeline.xctimeline
├── Selection Sampling
├── README.markdown
├── README_en.markdown
└── SelectionSampling.playground
│ ├── Contents.swift
│ ├── Sources
│ └── SelectionSampling.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── timeline.xctimeline
├── Selection Sort
├── README.markdown
├── README_en.markdown
├── SelectionSort.playground
│ ├── Contents.swift
│ └── contents.xcplayground
└── SelectionSort.swift
├── Shell Sort
├── README.markdown
├── README_en.markdown
├── Shell Sort.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
└── image
│ ├── shell_sort_1.png
│ └── shell_sort_2.png
├── Shortest Path (Unweighted)
├── Images
│ ├── Graph.graffle
│ └── Graph.png
├── README.markdown
├── README_en.markdown
└── ShortestPath.playground
│ ├── Contents.swift
│ ├── Sources
│ ├── Edge.swift
│ ├── Graph.swift
│ ├── Node.swift
│ └── Queue.swift
│ └── contents.xcplayground
├── Shuffle
├── README.markdown
└── README_en.markdown
├── Shunting Yard
├── README.markdown
└── README_en.markdown
├── Simulated annealing
└── README.md
├── Single-Source Shortest Paths (Weighted)
├── README.markdown
├── README_en.markdown
└── img
│ ├── example_graph.png
│ └── negative_cycle_example.png
├── Skip-List
├── Images
│ ├── Insert1.png
│ ├── Insert10.png
│ ├── Insert11.png
│ ├── Insert12.png
│ ├── Insert2.png
│ ├── Insert3.png
│ ├── Insert4.png
│ ├── Insert5.png
│ ├── Insert6.png
│ ├── Insert8.png
│ ├── Insert9.png
│ ├── Intro.png
│ ├── Search1.png
│ └── insert7.png
├── README.md
├── README_en.md
└── SkipList.playground
│ ├── Contents.swift
│ ├── Sources
│ └── SkipList.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── timeline.xctimeline
├── Slow Sort
├── README.markdown
└── README_en.markdown
├── Sparse Table
├── Images
│ ├── idempotency.png
│ ├── query.png
│ ├── query_example.png
│ ├── recursion.png
│ ├── recursion_examples.png
│ ├── structure.png
│ └── structure_examples.png
├── README.markdown
└── README_en.markdown
├── Splay Tree
├── Images
│ ├── SplayTreesWorstCaseExamples.svg
│ ├── example-zigzig-1.png
│ ├── example-zigzig-2.png
│ ├── example-zigzig-3.png
│ ├── example-zigzig-4.png
│ ├── example-zigzig-5.png
│ ├── example1-1.png
│ ├── example1-2.png
│ ├── example1-3.png
│ ├── examplezig.svg
│ ├── examplezig1.png
│ ├── examplezig2.png
│ ├── examplezigzig.svg
│ ├── examplezigzig1.png
│ ├── examplezigzig2.png
│ ├── examplezigzig3.png
│ ├── worst-case-1.png
│ ├── worst-case-2.png
│ ├── worst-case-3.png
│ ├── worst-case-4.png
│ ├── worst-case-5.png
│ ├── worst-case-6.png
│ ├── zig.png
│ ├── zigzag1.png
│ ├── zigzag2.png
│ ├── zigzig-wrongrotated.png
│ ├── zigzig1.png
│ └── zigzig2.png
├── SplayTree.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── SplayTree.swift
│ └── contents.xcplayground
├── readme.md
└── readme_en.md
├── Stack
├── README.markdown
├── README_en.markdown
├── Stack.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
└── Tests
│ ├── Info.plist
│ ├── StackTests.swift
│ └── Tests.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ │ └── andyron.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── xcshareddata
│ └── xcschemes
│ └── Tests.xcscheme
├── Strassen Matrix Multiplication
├── README.markdown
└── README_en.markdown
├── Threaded Binary Tree
├── Images
│ ├── Base.png
│ ├── Full.png
│ ├── Insert1.png
│ ├── Insert2.png
│ ├── Insert3.png
│ ├── Partial.png
│ ├── Remove1.png
│ ├── Remove2.png
│ ├── Remove3.png
│ └── Remove4.png
├── README.markdown
├── README_en.markdown
├── ThreadedBinaryTree.playground
│ ├── Contents.swift
│ ├── Sources
│ │ └── ThreadedBinaryTree.swift
│ └── contents.xcplayground
└── ThreadedBinaryTree.swift
├── Topological Sort
├── Images
│ ├── Algorithms.graffle
│ ├── Algorithms.png
│ ├── Example.graffle
│ ├── Example.png
│ ├── Graph.graffle
│ ├── Graph.png
│ ├── GraphResult.graffle
│ ├── GraphResult.png
│ ├── InvalidSort.graffle
│ ├── InvalidSort.png
│ ├── TopologicalSort.graffle
│ └── TopologicalSort.png
├── README.markdown
└── README_en.markdown
├── Tree
├── Images
│ ├── Cycles.graffle
│ ├── Cycles.png
│ ├── Example.graffle
│ ├── Example.png
│ ├── ParentChildren.graffle
│ ├── ParentChildren.png
│ ├── Tree.graffle
│ └── Tree.png
├── README.markdown
├── README_en.markdown
└── Tree.playground
│ ├── Contents.swift
│ └── contents.xcplayground
├── Trie
├── README_en.md
├── ReadMe.md
├── Trie.swift
└── images
│ └── trie.png
├── Two-Sum Problem
├── README.markdown
└── README_en.markdown
├── Under Construction.markdown
├── Union-Find
├── Images
│ ├── AfterFind.png
│ ├── AfterUnion.png
│ ├── BeforeFind.png
│ └── BeforeUnion.png
├── README.markdown
├── README_en.markdown
└── UnionFind.playground
│ ├── Contents.swift
│ ├── Sources
│ ├── UnionFindQuickFind.swift
│ ├── UnionFindQuickUnion.swift
│ ├── UnionFindWeightedQuickFind.swift
│ ├── UnionFindWeightedQuickUnion.swift
│ └── UnionFindWeightedQuickUnionPathCompression.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ └── contents.xcworkspacedata
│ └── timeline.xctimeline
├── What are Algorithms.markdown
├── Why Algorithms.markdown
└── Z-Algorithm
├── README.markdown
└── README_en.markdown
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | MyREADME\.md
3 |
4 | MyREADME\.markdown
5 |
--------------------------------------------------------------------------------
/AVL Tree/AVLTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/AVL Tree/AVLTree.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/AVL Tree/AVLTree.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/AVL Tree/AVLTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/AVLTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/AVL Tree/Images/BalanceNotOK.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/BalanceNotOK.graffle
--------------------------------------------------------------------------------
/AVL Tree/Images/BalanceNotOK.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/BalanceNotOK.png
--------------------------------------------------------------------------------
/AVL Tree/Images/BalanceOK.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/BalanceOK.graffle
--------------------------------------------------------------------------------
/AVL Tree/Images/BalanceOK.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/BalanceOK.png
--------------------------------------------------------------------------------
/AVL Tree/Images/Balanced.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/Balanced.graffle
--------------------------------------------------------------------------------
/AVL Tree/Images/Balanced.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/Balanced.png
--------------------------------------------------------------------------------
/AVL Tree/Images/Height.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/Height.graffle
--------------------------------------------------------------------------------
/AVL Tree/Images/Height.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/Height.png
--------------------------------------------------------------------------------
/AVL Tree/Images/RotationStep0.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/RotationStep0.jpg
--------------------------------------------------------------------------------
/AVL Tree/Images/RotationStep1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/RotationStep1.jpg
--------------------------------------------------------------------------------
/AVL Tree/Images/RotationStep2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/RotationStep2.jpg
--------------------------------------------------------------------------------
/AVL Tree/Images/RotationStep3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/RotationStep3.jpg
--------------------------------------------------------------------------------
/AVL Tree/Images/Unbalanced.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/Unbalanced.graffle
--------------------------------------------------------------------------------
/AVL Tree/Images/Unbalanced.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/AVL Tree/Images/Unbalanced.png
--------------------------------------------------------------------------------
/Algorithm Design.markdown:
--------------------------------------------------------------------------------
1 | # 算法设计技巧
2 |
3 | 当你遇到新问题的时候,需要寻找新的算法。
4 |
5 | ### 是否有类似的其它问题?
6 |
7 | 如果您可以根据另一个更普遍的问题来构建解决你目前需要解决问题,那么您可以使用现有算法。 为什么重新发明轮子?
8 |
9 | 我喜欢Steven Skiena的[算法设计手册](http://www.algorist.com),它包含了一系列可以尝试的问题和解决方案。(另见Steven Skiena的[算法库](http://www3.cs.stonybrook.edu/~algorith/))
10 |
11 | > 译注:豆瓣 [算法设计手册](https://book.douban.com/subject/4048566/)
12 |
13 | ### 可以从暴力方案开始
14 |
15 | 原始而暴力的解决方案通常对实际使用而言太慢,但它们是一个很好的起点。 通过编写暴力解决方案,您将学习如何理解问题的真正含义。
16 |
17 | 一旦你有一个暴力实施方案,就可以使用它来验证你提出的任何改进是否正确的。
18 |
19 | 如果您只使用小型数据集,那么暴力方法实际上可能足够好。 不要陷入过早优化的陷阱!
20 |
21 | ### 分而治之
22 |
23 | > “当你改变你看待事物的方式时,你看到的东西会发生变化。”
24 | > —— 马克斯·普朗克,量子物理学家和诺贝尔奖获得者
25 |
26 | 分而治之是一种处理大问题的方法,将其分解成碎片并逐步向解决方案迈进。
27 |
28 | 不是将整个问题看作一个单一,庞大而复杂的任务,而是将问题分成相对较小的问题,这样问题更容易理解和处理。
29 |
30 | 您可以解决较小的问题并聚合解决方案,直到您只使用解决方案。 在每个步骤中,手头的问题都会缩小,解决方案会变得成熟,直到您拥有最终正确的解决方案。
31 | 将问题分解为规模更小的子问题,将这些规模更小的子问题逐个击破,然后将已解决的子问题合并,最终得出“母”问题的解。
32 |
33 | 将相同的解决方案重复地(或经常递归地)应用于解决较小的任务,从而在更短的时间内获得结果。
34 |
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/d0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/d0.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/d1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/d1.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/d2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/d2.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/d3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/d3.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/example_graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/example_graph.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/original_adjacency_matrix.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/original_adjacency_matrix.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/pi0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/pi0.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/pi1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/pi1.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/pi2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/pi2.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/pi3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/pi3.png
--------------------------------------------------------------------------------
/All-Pairs Shortest Paths/img/weight_comparison_formula.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/All-Pairs Shortest Paths/img/weight_comparison_formula.png
--------------------------------------------------------------------------------
/Array2D/Array2D.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/B-Tree/BTree.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import Foundation
2 |
3 | // last checked with Xcode 10.0
4 |
5 | let bTree = BTree(order: 1)!
6 |
7 | bTree.insert(1, for: 1)
8 | bTree.insert(2, for: 2)
9 | bTree.insert(3, for: 3)
10 | bTree.insert(4, for: 4)
11 |
12 | bTree.value(for: 3)
13 | bTree[3]
14 |
15 | bTree.remove(2)
16 |
17 | bTree.traverseKeysInOrder { key in
18 | print(key)
19 | }
20 |
21 | bTree.numberOfKeys
22 |
23 | bTree.order
24 |
25 | bTree.inorderArrayFromKeys
26 |
--------------------------------------------------------------------------------
/B-Tree/BTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/B-Tree/Images/BTree20.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/B-Tree/Images/BTree20.png
--------------------------------------------------------------------------------
/B-Tree/Images/InsertionSplit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/B-Tree/Images/InsertionSplit.png
--------------------------------------------------------------------------------
/B-Tree/Images/MergingNodes.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/B-Tree/Images/MergingNodes.png
--------------------------------------------------------------------------------
/B-Tree/Images/MovingKey.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/B-Tree/Images/MovingKey.png
--------------------------------------------------------------------------------
/B-Tree/Images/Node.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/B-Tree/Images/Node.png
--------------------------------------------------------------------------------
/Binary Search Tree/Images/DeleteLeaf.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/DeleteLeaf.graffle
--------------------------------------------------------------------------------
/Binary Search Tree/Images/DeleteLeaf.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/DeleteLeaf.png
--------------------------------------------------------------------------------
/Binary Search Tree/Images/DeleteOneChild.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/DeleteOneChild.graffle
--------------------------------------------------------------------------------
/Binary Search Tree/Images/DeleteOneChild.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/DeleteOneChild.png
--------------------------------------------------------------------------------
/Binary Search Tree/Images/DeleteTwoChildren.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/DeleteTwoChildren.graffle
--------------------------------------------------------------------------------
/Binary Search Tree/Images/DeleteTwoChildren.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/DeleteTwoChildren.png
--------------------------------------------------------------------------------
/Binary Search Tree/Images/MinimumMaximum.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/MinimumMaximum.graffle
--------------------------------------------------------------------------------
/Binary Search Tree/Images/MinimumMaximum.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/MinimumMaximum.png
--------------------------------------------------------------------------------
/Binary Search Tree/Images/Searching.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/Searching.graffle
--------------------------------------------------------------------------------
/Binary Search Tree/Images/Searching.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/Searching.png
--------------------------------------------------------------------------------
/Binary Search Tree/Images/Traversing.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/Traversing.graffle
--------------------------------------------------------------------------------
/Binary Search Tree/Images/Traversing.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/Traversing.png
--------------------------------------------------------------------------------
/Binary Search Tree/Images/Tree1.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/Tree1.graffle
--------------------------------------------------------------------------------
/Binary Search Tree/Images/Tree1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/Tree1.png
--------------------------------------------------------------------------------
/Binary Search Tree/Images/Tree2.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/Tree2.graffle
--------------------------------------------------------------------------------
/Binary Search Tree/Images/Tree2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Images/Tree2.png
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 1/BinarySearchTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 1/BinarySearchTree.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 1/BinarySearchTree.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 1/BinarySearchTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Solution 1/BinarySearchTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 1/BinarySearchTree.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 2/BinarySearchTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 2/BinarySearchTree.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 2/BinarySearchTree.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Binary Search Tree/Solution 2/BinarySearchTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Search Tree/Solution 2/BinarySearchTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Binary Search/BinarySearch.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Binary Tree/BinaryTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Binary Tree/BinaryTree.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Binary Tree/BinaryTree.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Binary Tree/BinaryTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Tree/BinaryTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Binary Tree/Images/BinaryTree.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Tree/Images/BinaryTree.graffle
--------------------------------------------------------------------------------
/Binary Tree/Images/BinaryTree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Tree/Images/BinaryTree.png
--------------------------------------------------------------------------------
/Binary Tree/Images/Operations.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Tree/Images/Operations.graffle
--------------------------------------------------------------------------------
/Binary Tree/Images/Operations.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Binary Tree/Images/Operations.png
--------------------------------------------------------------------------------
/Bit Set/BitSet.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 |
3 | var bits = BitSet(size: 140)
4 |
5 | //print(bits)
6 |
7 | print(~UInt64())
8 |
9 | (65 + 64)/64
10 |
--------------------------------------------------------------------------------
/Bit Set/BitSet.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Bit Set/BitSet.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Bit Set/BitSet.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Bit Set/BitSet.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Bit Set/BitSet.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Bloom Filter/BloomFilter.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Bloom Filter/BloomFilter.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Bloom Filter/BloomFilter.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Bloom Filter/BloomFilter.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Bloom Filter/BloomFilter.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Bounded Priority Queue/BoundedPriorityQueue.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Bounded Priority Queue/BoundedPriorityQueue.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Bounded Priority Queue/BoundedPriorityQueue.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Bounded Priority Queue/BoundedPriorityQueue.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Bounded Priority Queue/BoundedPriorityQueue.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Boyer-Moore-Horspool/BoyerMooreHorspool.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Boyer-Moore-Horspool/BoyerMooreHorspool.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Boyer-Moore-Horspool/BoyerMooreHorspool.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Boyer-Moore-Horspool/BoyerMooreHorspool.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Boyer-Moore-Horspool/BoyerMooreHorspool.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Breadth-First Search/BreadthFirstSearch.playground/Sources/Edge.swift:
--------------------------------------------------------------------------------
1 | public class Edge: Equatable {
2 | public var neighbor: Node
3 |
4 | public init(_ neighbor: Node) {
5 | self.neighbor = neighbor
6 | }
7 | }
8 |
9 | public func == (_ lhs: Edge, rhs: Edge) -> Bool {
10 | return lhs.neighbor == rhs.neighbor
11 | }
12 |
--------------------------------------------------------------------------------
/Breadth-First Search/BreadthFirstSearch.playground/Sources/Node.swift:
--------------------------------------------------------------------------------
1 | public class Node: CustomStringConvertible, Equatable {
2 | public var neighbors: [Edge]
3 |
4 | public private(set) var label: String
5 | public var distance: Int?
6 | public var visited: Bool
7 |
8 | public init(_ label: String) {
9 | self.label = label
10 | neighbors = []
11 | visited = false
12 | }
13 |
14 | public var description: String {
15 | if let distance = distance {
16 | return "Node(label: \(label), distance: \(distance))"
17 | }
18 | return "Node(label: \(label), distance: infinity)"
19 | }
20 |
21 | public var hasDistance: Bool {
22 | return distance != nil
23 | }
24 |
25 | public func remove(_ edge: Edge) {
26 | neighbors.remove(at: neighbors.index { $0 === edge }!)
27 | }
28 | }
29 |
30 | public func == (_ lhs: Node, rhs: Node) -> Bool {
31 | return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/Breadth-First Search/BreadthFirstSearch.playground/Sources/Queue.swift:
--------------------------------------------------------------------------------
1 | public struct Queue {
2 | private var array: [T]
3 |
4 | public init() {
5 | array = []
6 | }
7 |
8 | public var isEmpty: Bool {
9 | return array.isEmpty
10 | }
11 |
12 | public var count: Int {
13 | return array.count
14 | }
15 |
16 | public mutating func enqueue(_ element: T) {
17 | array.append(element)
18 | }
19 |
20 | public mutating func dequeue() -> T? {
21 | if isEmpty {
22 | return nil
23 | } else {
24 | return array.removeFirst()
25 | }
26 | }
27 |
28 | public func peek() -> T? {
29 | return array.first
30 | }
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/Breadth-First Search/BreadthFirstSearch.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Breadth-First Search/Images/AnimatedExample.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Breadth-First Search/Images/AnimatedExample.gif
--------------------------------------------------------------------------------
/Breadth-First Search/Images/AnimatedExample.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Breadth-First Search/Images/AnimatedExample.graffle
--------------------------------------------------------------------------------
/Breadth-First Search/Images/AnimatedExample.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Breadth-First Search/Images/AnimatedExample.psd
--------------------------------------------------------------------------------
/Breadth-First Search/Images/TraversalTree.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Breadth-First Search/Images/TraversalTree.graffle
--------------------------------------------------------------------------------
/Breadth-First Search/Images/TraversalTree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Breadth-First Search/Images/TraversalTree.png
--------------------------------------------------------------------------------
/Brute-Force String Search/BruteForceStringSearch.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 | extension String {
3 | func indexOf(_ pattern: String) -> String.Index? {
4 |
5 | for i in self.indices {
6 | var j = i
7 | var found = true
8 | for p in pattern.indices {
9 | if j == self.endIndex || self[j] != pattern[p] {
10 | found = false
11 | break
12 | } else {
13 | j = self.index(after: j)
14 | }
15 | }
16 | if found {
17 | return i
18 | }
19 | }
20 | return nil
21 | }
22 | }
23 |
24 |
25 | let s = "Hello, World"
26 | s.indexOf("orl")?.encodedOffset
27 |
28 | let animals = "🦍🐢🐡🐮🦖🐋🐶🐬🐠🐔🐷🐙🐮🦟🦂🦜🦢🐨🦇🐐🦓"
29 | animals.indexOf("🐮")?.encodedOffset
30 |
31 |
--------------------------------------------------------------------------------
/Brute-Force String Search/BruteForceStringSearch.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Brute-Force String Search/BruteForceStringSearch.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Brute-Force String Search/BruteForceStringSearch.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Brute-Force String Search/BruteForceStringSearch.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Brute-Force String Search/BruteForceStringSearch.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Brute-Force String Search/BruteForceStringSearch.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/Bubble Sort/BubbleSort.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 | func bubbleSort(_ numbers: [Int]) -> [Int]{
3 |
4 | var nums = numbers
5 | let n = nums.count
6 | for i in 0.. nums[j + 1] {
9 | nums.swapAt(j, j + 1)
10 | }
11 | }
12 | }
13 | return nums
14 | }
15 |
16 | let nums = [3,42,1,5,34,20,9]
17 | bubbleSort(nums)
18 |
--------------------------------------------------------------------------------
/Bubble Sort/BubbleSort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Bubble Sort/README.markdown:
--------------------------------------------------------------------------------
1 | # 冒泡排序(Bubble Sort)
2 |
3 |
4 | 冒泡排序是一种排序算法,它通过重复地走过过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来,这样做直到数组完全排序。 较小的项慢慢地“冒泡”到数组的开头。
5 |
6 |
7 | ##### Runtime:
8 |
9 | - 平均: O(N^2)
10 | - 最差: O(N^2)
11 |
12 | ##### 内存
13 |
14 | - O(1)
15 |
16 | ### 实现
17 |
18 | 平均和最差运行时间表明冒泡排序是一种非常低效的算法,因此不会显示实现。 但是,掌握这个概念将有助于您理解简单排序算法的基础知识。
19 |
20 | > 译注: [冒泡排序的动画](http://www.algomation.com/player?algorithm=5a1b2f0711aaf40400e46699)
21 | 原文没有对冒泡排序实现,处于学习的目的,我自己实现了一下:
22 |
23 | ```
24 |
25 | func bubbleSort(_ numbers: [Int]) -> [Int]{
26 |
27 | var nums = numbers
28 | let n = nums.count
29 | for i in 0.. nums[j + 1] {
32 | nums.swapAt(j, j + 1)
33 | }
34 | }
35 | }
36 | return nums
37 | }
38 |
39 | let nums = [3,42,1,5,34,20,9]
40 | bubbleSort(nums)
41 |
42 | ```
43 |
--------------------------------------------------------------------------------
/Bubble Sort/README_en.markdown:
--------------------------------------------------------------------------------
1 | # Bubble Sort
2 |
3 | Bubble sort is a sorting algorithm that is implemented by starting in the beginning of the array and swapping the first two elements only if the first element is greater than the second element. This comparison is then moved onto the next pair and so on and so forth. This is done until the array is completely sorted. The smaller items slowly “bubble” up to the beginning of the array.
4 |
5 | ##### Runtime:
6 | - Average: O(N^2)
7 | - Worst: O(N^2)
8 |
9 | ##### Memory:
10 | - O(1)
11 |
12 | ### Implementation:
13 |
14 | The implementation will not be shown as the average and worst runtimes show that this is a very inefficient algorithm. However, having a grasp of the concept will help you understand the basics of simple sorting algorithms.
15 |
--------------------------------------------------------------------------------
/CNAME:
--------------------------------------------------------------------------------
1 | hosting.gitbook.com
2 |
--------------------------------------------------------------------------------
/Count Occurrences/CountOccurrences.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | /*
2 | 用二分搜索分别查找左右边界(事先已经排过序)
3 | */
4 | func countOccurrencesOfKey(_ key: Int, inArray a: [Int]) -> Int {
5 | func leftBoundary() -> Int {
6 | var low = 0
7 | var high = a.count
8 | while low < high {
9 | let midIndex = low + (high - low)/2
10 | if a[midIndex] < key {
11 | low = midIndex + 1
12 | } else {
13 | high = midIndex
14 | }
15 | }
16 | return low
17 | }
18 |
19 | func rightBoundary() -> Int {
20 | var low = 0
21 | var high = a.count
22 | while low < high {
23 | let midIndex = low + (high - low)/2
24 | if a[midIndex] > key {
25 | high = midIndex
26 | } else {
27 | low = midIndex + 1
28 | }
29 | }
30 | return low
31 | }
32 |
33 | return rightBoundary() - leftBoundary()
34 | }
35 |
36 | let a = [ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11 ]
37 |
38 | countOccurrencesOfKey(11, inArray: a)
39 |
--------------------------------------------------------------------------------
/Count Occurrences/CountOccurrences.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Count Occurrences/CountOccurrences.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Count Occurrences/CountOccurrences.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Count Occurrences/CountOccurrences.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Count Occurrences/CountOccurrences.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Counting Sort/CountingSort.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 | enum CountingSortError: Error {
3 | case arrayEmpty
4 | }
5 |
6 | func countingSort(array: [Int]) throws -> [Int] {
7 | guard array.count > 0 else {
8 | throw CountingSortError.arrayEmpty
9 | }
10 |
11 | // Step 1 创建一个数组来存储每个元素的计数
12 | let maxElement = array.max() ?? 0
13 |
14 | var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
15 | for elemnt in array {
16 | countArray[elemnt] += 1
17 | }
18 |
19 | print(countArray)
20 |
21 | // Step 2 将每个值设置为前两个值的总和
22 | for index in 1 ..< countArray.count {
23 | let sum = countArray[index] + countArray[index - 1]
24 | countArray[index] = sum
25 | }
26 |
27 | print(countArray)
28 |
29 | // Step 3 根据元素前面的元素数将元素放在最终数组中
30 | var sortedArray = [Int](repeating: 0, count: array.count)
31 | for element in array {
32 | countArray[element] -= 1
33 | sortedArray[countArray[element]] = element
34 | }
35 | return sortedArray
36 | }
37 |
38 | let sortedArray = try countingSort(array: [ 10, 9, 8, 7, 1, 2, 7, 3 ])
39 |
40 | print(sortedArray)
41 |
--------------------------------------------------------------------------------
/Counting Sort/CountingSort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Depth-First Search/DepthFirstSearch.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | // last checked with Xcode 10.1
2 |
3 | func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
4 | var nodesExplored = [source.label]
5 | source.visited = true
6 |
7 | for edge in source.neighbors {
8 | if !edge.neighbor.visited {
9 | nodesExplored += depthFirstSearch(graph, source: edge.neighbor)
10 | }
11 | }
12 | return nodesExplored
13 | }
14 |
15 | let graph = Graph()
16 |
17 | let nodeA = graph.addNode("a")
18 | let nodeB = graph.addNode("b")
19 | let nodeC = graph.addNode("c")
20 | let nodeD = graph.addNode("d")
21 | let nodeE = graph.addNode("e")
22 | let nodeF = graph.addNode("f")
23 | let nodeG = graph.addNode("g")
24 | let nodeH = graph.addNode("h")
25 |
26 | graph.addEdge(nodeA, neighbor: nodeB)
27 | graph.addEdge(nodeA, neighbor: nodeC)
28 | graph.addEdge(nodeB, neighbor: nodeD)
29 | graph.addEdge(nodeB, neighbor: nodeE)
30 | graph.addEdge(nodeC, neighbor: nodeF)
31 | graph.addEdge(nodeC, neighbor: nodeG)
32 | graph.addEdge(nodeE, neighbor: nodeH)
33 | graph.addEdge(nodeE, neighbor: nodeF)
34 | graph.addEdge(nodeF, neighbor: nodeG)
35 |
36 | let nodesExplored = depthFirstSearch(graph, source: nodeA)
37 | print(nodesExplored)
38 |
39 |
--------------------------------------------------------------------------------
/Depth-First Search/DepthFirstSearch.playground/Sources/Edge.swift:
--------------------------------------------------------------------------------
1 | public class Edge: Equatable {
2 | public var neighbor: Node
3 |
4 | public init(_ neighbor: Node) {
5 | self.neighbor = neighbor
6 | }
7 | }
8 |
9 | public func == (_ lhs: Edge, rhs: Edge) -> Bool {
10 | return lhs.neighbor == rhs.neighbor
11 | }
12 |
--------------------------------------------------------------------------------
/Depth-First Search/DepthFirstSearch.playground/Sources/Node.swift:
--------------------------------------------------------------------------------
1 | public class Node: CustomStringConvertible, Equatable {
2 | public var neighbors: [Edge]
3 |
4 | public private(set) var label: String
5 | public var distance: Int?
6 | public var visited: Bool
7 |
8 | public init(_ label: String) {
9 | self.label = label
10 | neighbors = []
11 | visited = false
12 | }
13 |
14 | public var description: String {
15 | if let distance = distance {
16 | return "Node(label: \(label), distance: \(distance))"
17 | }
18 | return "Node(label: \(label), distance: infinity)"
19 | }
20 |
21 | public var hasDistance: Bool {
22 | return distance != nil
23 | }
24 |
25 | public func remove(_ edge: Edge) {
26 | neighbors.remove(at: neighbors.index { $0 === edge }!)
27 | }
28 | }
29 |
30 | public func == (_ lhs: Node, rhs: Node) -> Bool {
31 | return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/Depth-First Search/DepthFirstSearch.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Depth-First Search/Images/AnimatedExample.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Depth-First Search/Images/AnimatedExample.gif
--------------------------------------------------------------------------------
/Depth-First Search/Images/AnimatedExample.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Depth-First Search/Images/AnimatedExample.graffle
--------------------------------------------------------------------------------
/Depth-First Search/Images/AnimatedExample.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Depth-First Search/Images/AnimatedExample.psd
--------------------------------------------------------------------------------
/Depth-First Search/Images/TraversalTree.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Depth-First Search/Images/TraversalTree.graffle
--------------------------------------------------------------------------------
/Depth-First Search/Images/TraversalTree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Depth-First Search/Images/TraversalTree.png
--------------------------------------------------------------------------------
/Deque/Deque-Simple.swift:
--------------------------------------------------------------------------------
1 |
2 | /**
3 | * 简单的双端队列
4 | */
5 | public struct Deque {
6 | private var array = [T]()
7 |
8 | public var isEmpty: Bool {
9 | return array.isEmpty
10 | }
11 |
12 | public var count: Int {
13 | return array.count
14 | }
15 |
16 | public mutating func enqueue(_ element: T) {
17 | array.append(element)
18 | }
19 |
20 | public mutating func enqueueFront(_ element: T) {
21 | array.insert(element, at: 0)
22 | }
23 |
24 | public mutating func dequeue() -> T? {
25 | if isEmpty {
26 | return nil
27 | } else {
28 | return array.removeFirst()
29 | }
30 | }
31 |
32 | public mutating func dequeueBack() -> T? {
33 | if isEmpty {
34 | return nil
35 | } else {
36 | return array.removeLast()
37 | }
38 | }
39 |
40 | public func peekFront() -> T? {
41 | return array.first
42 | }
43 |
44 | public func peakBack() -> T? {
45 | return array.last
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Deque/Deque.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Deque/Deque.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Deque/Deque.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Deque/Deque.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Deque/Deque.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/DirectedGraph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/DirectedGraph.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/Vertices.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/Vertices.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/WeightedDirectedGraph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/WeightedDirectedGraph.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/WeightedDirectedGraphFinal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/WeightedDirectedGraphFinal.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/WeightedUndirectedGraph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/WeightedUndirectedGraph.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/image1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/image1.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/image2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/image2.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/image3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/image3.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/image4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/image4.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/image5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/image5.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/image6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/image6.png
--------------------------------------------------------------------------------
/Dijkstra Algorithm/Images/image7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Dijkstra Algorithm/Images/image7.png
--------------------------------------------------------------------------------
/Egg Drop Problem/images/eggdrop.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Egg Drop Problem/images/eggdrop.png
--------------------------------------------------------------------------------
/Egg Drop Problem/images/eggdrop2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Egg Drop Problem/images/eggdrop2.png
--------------------------------------------------------------------------------
/Fixed Size Array/FixedSizeArray.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | /**
2 | * 固定长度数组
3 | */
4 | struct FixedSizeArray {
5 | /// 数组最大存储元素数量
6 | private var maxSize: Int
7 | private var defaultValue: T
8 | private var array: [T]
9 | /// 数组当前包含的元素数量
10 | private(set) var count = 0
11 |
12 | init(maxSize: Int, defaultValue: T) {
13 | self.maxSize = maxSize
14 | self.defaultValue = defaultValue
15 | self.array = [T](repeating: defaultValue, count: maxSize)
16 | }
17 |
18 | subscript(index: Int) -> T {
19 | assert(index >= 0)
20 | assert(index < count)
21 | return array[index]
22 | }
23 |
24 | mutating func append(_ newElement: T) {
25 | assert(count < maxSize)
26 | array[count] = newElement
27 | count += 1
28 | }
29 |
30 | mutating func removeAt(index: Int) -> T {
31 | assert(index >= 0)
32 | assert(index < count)
33 | count -= 1
34 | let result = array[index]
35 | array[index] = array[count]
36 | array[count] = defaultValue
37 | return result
38 | }
39 |
40 | mutating func removeAll() {
41 | for i in 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Fixed Size Array/FixedSizeArray.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Fixed Size Array/FixedSizeArray.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Fixed Size Array/FixedSizeArray.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Fixed Size Array/FixedSizeArray.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Fixed Size Array/Images/FixedSizeArrays.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Fixed Size Array/Images/FixedSizeArrays.graffle
--------------------------------------------------------------------------------
/Fixed Size Array/Images/append.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Fixed Size Array/Images/append.png
--------------------------------------------------------------------------------
/Fixed Size Array/Images/array.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Fixed Size Array/Images/array.png
--------------------------------------------------------------------------------
/Fixed Size Array/Images/delete-no-copy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Fixed Size Array/Images/delete-no-copy.png
--------------------------------------------------------------------------------
/Fixed Size Array/Images/delete.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Fixed Size Array/Images/delete.png
--------------------------------------------------------------------------------
/Fixed Size Array/Images/indexing.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Fixed Size Array/Images/indexing.png
--------------------------------------------------------------------------------
/Fixed Size Array/Images/insert.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Fixed Size Array/Images/insert.png
--------------------------------------------------------------------------------
/GCD/GCD.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | gcd(52, 39) // 13
2 | gcd(228, 36) // 12
3 | gcd(51357, 3819) // 57
4 | gcd(841, 299) // 1
5 |
6 | gcd(52, 39, using: gcdRecursiveEuklid) // 13
7 | gcd(228, 36, using: gcdRecursiveEuklid) // 12
8 | gcd(51357, 3819, using: gcdRecursiveEuklid) // 57
9 | gcd(841, 299, using: gcdRecursiveEuklid) // 1
10 |
11 | gcd(52, 39, using: gcdBinaryRecursiveStein) // 13
12 | gcd(228, 36, using: gcdBinaryRecursiveStein) // 12
13 | gcd(51357, 3819, using: gcdBinaryRecursiveStein) // 57
14 | gcd(841, 299, using: gcdBinaryRecursiveStein) // 1
15 |
16 | do {
17 | try lcm(2, 3) // 6
18 | try lcm(10, 8, using: gcdRecursiveEuklid) // 40
19 | } catch {
20 | dump(error)
21 | }
22 |
--------------------------------------------------------------------------------
/GCD/GCD.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Graph/Graph/Graph.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Graph/Graph/Graph.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Graph/Graph/Graph.xcodeproj/project.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Graph/Graph.xcodeproj/project.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Graph/Graph/Graph.xcodeproj/xcuserdata/andyron.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
8 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Graph/Graph/Graph.xcodeproj/xcuserdata/andyron.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | Graph.xcscheme_^#shared#^_
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Graph/Graph/Graph/Graph.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Graph/Graph/Graph/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
11 | CFBundleIdentifier
12 | $(PRODUCT_BUNDLE_IDENTIFIER)
13 | CFBundleInfoDictionaryVersion
14 | 6.0
15 | CFBundleName
16 | $(PRODUCT_NAME)
17 | CFBundlePackageType
18 | APPL
19 | CFBundleShortVersionString
20 | 1.0
21 | CFBundleVersion
22 | 1
23 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018 Andy Ron. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/Graph/Graph/Graph/Vertex.swift:
--------------------------------------------------------------------------------
1 | //
2 | // Vertex.swift
3 | // Graph
4 | //
5 | // Created by Andy Ron on 2018/11/9.
6 | // Copyright © 2018 Andy Ron. All rights reserved.
7 | //
8 |
9 | import Foundation
10 |
11 | public struct Vertex: Equatable where T: Hashable {
12 |
13 | public var data: T
14 | public let index: Int
15 |
16 | }
17 |
18 | extension Vertex: CustomStringConvertible {
19 |
20 | public var description: String {
21 | return "\(index): \(data)"
22 | }
23 | }
24 |
25 | extension Vertex: Hashable {
26 |
27 | public var hashValue: Int {
28 | return "\(data)\(index)".hashValue
29 | }
30 | }
31 |
32 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool {
33 | guard lhs.index == rhs.index else {
34 | return false
35 | }
36 |
37 | guard lhs.data == rhs.data else {
38 | return false
39 | }
40 |
41 | return true
42 | }
43 |
--------------------------------------------------------------------------------
/Graph/Graph/Graph/other/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/Graph/Graph/Graph/other/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/Graph/Images/AdjacencyList.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/AdjacencyList.graffle
--------------------------------------------------------------------------------
/Graph/Images/AdjacencyList.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/AdjacencyList.png
--------------------------------------------------------------------------------
/Graph/Images/AdjacencyMatrix.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/AdjacencyMatrix.graffle
--------------------------------------------------------------------------------
/Graph/Images/AdjacencyMatrix.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/AdjacencyMatrix.png
--------------------------------------------------------------------------------
/Graph/Images/ChordMap.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/ChordMap.graffle
--------------------------------------------------------------------------------
/Graph/Images/ChordMap.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/ChordMap.png
--------------------------------------------------------------------------------
/Graph/Images/CoreData.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/CoreData.graffle
--------------------------------------------------------------------------------
/Graph/Images/CoreData.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/CoreData.png
--------------------------------------------------------------------------------
/Graph/Images/DAG.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/DAG.graffle
--------------------------------------------------------------------------------
/Graph/Images/DAG.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/DAG.png
--------------------------------------------------------------------------------
/Graph/Images/Demo1.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/Demo1.graffle
--------------------------------------------------------------------------------
/Graph/Images/Demo1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/Demo1.png
--------------------------------------------------------------------------------
/Graph/Images/Flights.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/Flights.graffle
--------------------------------------------------------------------------------
/Graph/Images/Flights.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/Flights.png
--------------------------------------------------------------------------------
/Graph/Images/FlightsDirected.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/FlightsDirected.graffle
--------------------------------------------------------------------------------
/Graph/Images/FlightsDirected.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/FlightsDirected.png
--------------------------------------------------------------------------------
/Graph/Images/Graph.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/Graph.graffle
--------------------------------------------------------------------------------
/Graph/Images/Graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/Graph.png
--------------------------------------------------------------------------------
/Graph/Images/SocialNetwork.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/SocialNetwork.graffle
--------------------------------------------------------------------------------
/Graph/Images/SocialNetwork.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/SocialNetwork.png
--------------------------------------------------------------------------------
/Graph/Images/StateMachine.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/StateMachine.graffle
--------------------------------------------------------------------------------
/Graph/Images/StateMachine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/StateMachine.png
--------------------------------------------------------------------------------
/Graph/Images/Tasks.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/Tasks.graffle
--------------------------------------------------------------------------------
/Graph/Images/Tasks.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/Tasks.png
--------------------------------------------------------------------------------
/Graph/Images/TreeAndList.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/TreeAndList.graffle
--------------------------------------------------------------------------------
/Graph/Images/TreeAndList.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Graph/Images/TreeAndList.png
--------------------------------------------------------------------------------
/Hash Set/HashSet.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | var set = HashSet()
2 |
3 | set.insert("one")
4 | set.insert("two")
5 | set.insert("three")
6 | set.allElements()
7 |
8 | set.insert("two")
9 | set.allElements()
10 |
11 | set.contains("one")
12 | set.remove("one")
13 | set.allElements()
14 | set.contains("one")
15 |
16 | /* Union */
17 |
18 | var setA = HashSet()
19 | setA.insert(1)
20 | setA.insert(2)
21 | setA.insert(3)
22 | setA.insert(4)
23 |
24 | var setB = HashSet()
25 | setB.insert(3)
26 | setB.insert(4)
27 | setB.insert(5)
28 | setB.insert(6)
29 |
30 | let union = setA.union(setB)
31 | union.allElements() // [5, 6, 2, 3, 1, 4]
32 |
33 | /* Intersection */
34 |
35 | let intersection = setA.intersect(setB)
36 | intersection.allElements() // [3, 4]
37 |
38 | /* Difference */
39 |
40 | let difference1 = setA.difference(setB)
41 | difference1.allElements() // [2, 1]
42 |
43 | let difference2 = setB.difference(setA)
44 | difference2.allElements() // [5, 6]
45 |
--------------------------------------------------------------------------------
/Hash Set/HashSet.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Hash Set/Images/CombineSets.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Hash Set/Images/CombineSets.png
--------------------------------------------------------------------------------
/Hash Table/HashTable.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Hash Table/HashTable.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Hash Table/HashTable.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Hash Table/HashTable.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Hash Table/HashTable.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/HaversineDistance/HaversineDistance.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import UIKit
2 |
3 |
4 | func HaversineDistance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double {
5 |
6 | let haversine = { (angle: Double) -> Double in
7 | return (1 - cos(angle))/2
8 | }
9 |
10 | let ahaversin = { (angle: Double) -> Double in
11 | return 2*asin(sqrt(angle))
12 | }
13 |
14 | let dToR = { (angle: Double) -> Double in
15 | return (angle / 360) * 2 * .pi
16 | }
17 |
18 | let lat1 = dToR(la1)
19 | let lon1 = dToR(lo1)
20 | let lat2 = dToR(la2)
21 | let lon2 = dToR(lo2)
22 |
23 | return radius * ahaversin(haversine(lat2 - lat1) + cos(lat1) * cos(lat2) * haversine(lon2 - lon1))
24 | }
25 |
26 |
27 | let amsterdam = (52.3702, 4.8952)
28 | let newYork = (40.7128, -74.0059)
29 |
30 | HaversineDistance(la1: amsterdam.0, lo1: amsterdam.1, la2: newYork.0, lo2: newYork.1)
31 |
32 |
--------------------------------------------------------------------------------
/HaversineDistance/HaversineDistance.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/HaversineDistance/README_en.md:
--------------------------------------------------------------------------------
1 | # Haversine Distance
2 |
3 | Calculates the distance on a sphere between two points given in latitude and longitude using the haversine formula.
4 |
5 | The haversine formula can be found on [Wikipedia](https://en.wikipedia.org/wiki/Haversine_formula)
6 |
7 | The Haversine Distance is implemented as a function as a class would be kind of overkill.
8 |
9 | `haversineDinstance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double`
10 |
11 | - `la1` is the latitude of point 1 in degrees.
12 | - `lo1` is the longitude of point 1 in degrees.
13 | - `la2` is the latitude of point 2 in degrees.
14 | - `lo2` is the longitude of point 2 in degrees.
15 | - `radius` is the radius of the sphere considered in meters, which defaults to the mean radius of the earth (from [WolframAlpha](http://www.wolframalpha.com/input/?i=earth+radius)).
16 |
17 | The function contains 3 closures in order to make the code more readable and comparable to the Haversine formula given by the Wikipedia page mentioned above.
18 |
19 | 1. `haversine` implements the haversine, a trigonometric function.
20 | 2. `ahaversine` the inverse function of the haversine.
21 | 3. `dToR` a closure converting degrees to radians.
22 |
23 | The result of `haversineDistance` is returned in meters.
24 |
25 | *Written for Swift Algorithm Club by Jaap Wijnen.*
26 |
--------------------------------------------------------------------------------
/Heap Sort/HeapSort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Heap Sort/HeapSort.swift:
--------------------------------------------------------------------------------
1 | /// Heap 见 https://github.com/andyRon/swift-algorithm-club-cn/blob/master/Heap/Heap.swift
2 | extension Heap {
3 | public mutating func sort() -> [T] {
4 | for i in stride(from: (nodes.count - 1), through: 1, by: -1) {
5 | nodes.swaptAt(0, i)
6 | shiftDown(from: 0, until: i)
7 | }
8 | return nodes
9 | }
10 | }
11 |
12 | public func heapsort(_ a: [T], _ sort: @escaping (T, T) -> Bool) -> [T] {
13 | let reverseOrder = {i1, i2 in sort(i2, i1) }
14 | var h = Heap(array: a, sort: reverseOrder)
15 | return h.sort()
16 | }
17 |
--------------------------------------------------------------------------------
/Heap Sort/Images/MaxHeap.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap Sort/Images/MaxHeap.graffle
--------------------------------------------------------------------------------
/Heap Sort/Images/MaxHeap.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap Sort/Images/MaxHeap.png
--------------------------------------------------------------------------------
/Heap/Images/Array.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Array.graffle
--------------------------------------------------------------------------------
/Heap/Images/Array.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Array.png
--------------------------------------------------------------------------------
/Heap/Images/Heap1.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Heap1.graffle
--------------------------------------------------------------------------------
/Heap/Images/Heap1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Heap1.png
--------------------------------------------------------------------------------
/Heap/Images/HeapShape.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/HeapShape.graffle
--------------------------------------------------------------------------------
/Heap/Images/HeapShape.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/HeapShape.png
--------------------------------------------------------------------------------
/Heap/Images/Insert1.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Insert1.graffle
--------------------------------------------------------------------------------
/Heap/Images/Insert1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Insert1.png
--------------------------------------------------------------------------------
/Heap/Images/Insert2.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Insert2.graffle
--------------------------------------------------------------------------------
/Heap/Images/Insert2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Insert2.png
--------------------------------------------------------------------------------
/Heap/Images/Insert3.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Insert3.graffle
--------------------------------------------------------------------------------
/Heap/Images/Insert3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Insert3.png
--------------------------------------------------------------------------------
/Heap/Images/LargeHeap.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/LargeHeap.graffle
--------------------------------------------------------------------------------
/Heap/Images/LargeHeap.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/LargeHeap.png
--------------------------------------------------------------------------------
/Heap/Images/RegularTree.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/RegularTree.graffle
--------------------------------------------------------------------------------
/Heap/Images/RegularTree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/RegularTree.png
--------------------------------------------------------------------------------
/Heap/Images/Remove1.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove1.graffle
--------------------------------------------------------------------------------
/Heap/Images/Remove1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove1.png
--------------------------------------------------------------------------------
/Heap/Images/Remove2.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove2.graffle
--------------------------------------------------------------------------------
/Heap/Images/Remove2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove2.png
--------------------------------------------------------------------------------
/Heap/Images/Remove3.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove3.graffle
--------------------------------------------------------------------------------
/Heap/Images/Remove3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove3.png
--------------------------------------------------------------------------------
/Heap/Images/Remove4.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove4.graffle
--------------------------------------------------------------------------------
/Heap/Images/Remove4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove4.png
--------------------------------------------------------------------------------
/Heap/Images/Remove5.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove5.graffle
--------------------------------------------------------------------------------
/Heap/Images/Remove5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/Remove5.png
--------------------------------------------------------------------------------
/Heap/Images/SortedArray.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/SortedArray.graffle
--------------------------------------------------------------------------------
/Heap/Images/SortedArray.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Heap/Images/SortedArray.png
--------------------------------------------------------------------------------
/Huffman Coding/Images/BuildTree.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/BuildTree.gif
--------------------------------------------------------------------------------
/Huffman Coding/Images/BuildTree.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/BuildTree.graffle
--------------------------------------------------------------------------------
/Huffman Coding/Images/BuildTree.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/BuildTree.psd
--------------------------------------------------------------------------------
/Huffman Coding/Images/Compression.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/Compression.graffle
--------------------------------------------------------------------------------
/Huffman Coding/Images/Compression.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/Compression.png
--------------------------------------------------------------------------------
/Huffman Coding/Images/Decompression.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/Decompression.graffle
--------------------------------------------------------------------------------
/Huffman Coding/Images/Decompression.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/Decompression.png
--------------------------------------------------------------------------------
/Huffman Coding/Images/Tree.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/Tree.graffle
--------------------------------------------------------------------------------
/Huffman Coding/Images/Tree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Huffman Coding/Images/Tree.png
--------------------------------------------------------------------------------
/Images/SwiftAlgorithm-410-transp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Images/SwiftAlgorithm-410-transp.png
--------------------------------------------------------------------------------
/Images/scheme-settings-for-travis.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Images/scheme-settings-for-travis.png
--------------------------------------------------------------------------------
/Insertion Sort/InsertionSort.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | /*
2 | 插入排序
3 | */
4 | public func insertionSort(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
5 | var a = array
6 | for x in 1.. 0 && isOrderedBefore(temp, a[y-1]) {
10 | a[y] = a[y-1]
11 | y -= 1
12 | }
13 | a[y] = temp
14 | }
15 | return a
16 | }
17 |
18 |
19 | let numbers = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
20 | insertionSort(numbers, <)
21 | insertionSort(numbers, >)
22 |
23 | let strings = [ "b", "a", "d", "c", "e" ]
24 | insertionSort(strings, <)
25 |
--------------------------------------------------------------------------------
/Insertion Sort/InsertionSort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Insertion Sort/InsertionSort.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Insertion Sort/InsertionSort.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Insertion Sort/InsertionSort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Insertion Sort/InsertionSort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Insertion Sort/Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Insertion Sort/Tests/InsertionSortTests.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 |
3 | class InsertionSortTests: XCTestCase {
4 | func testInsertionSort() {
5 | checkSortAlgorithm(insertionSort)
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/Insertion Sort/Tests/Tests.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Insertion Sort/Tests/Tests.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Insertion Sort/Tests/Tests.xcodeproj/project.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Insertion Sort/Tests/Tests.xcodeproj/project.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Insertion Sort/Tests/Tests.xcodeproj/xcuserdata/andyron.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | Tests.xcscheme_^#shared#^_
8 |
9 | orderHint
10 | 11
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Introsort/Introsort.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 |
3 | func introsort(_ array: inout [T], by areInIncreasingOrder: (T, T) -> Bool) {
4 |
5 | }
6 |
7 | private func introSortImplementation(for array: inout [T], range: Range, depthLimit: Int, by areInIncreasingOrder: (T, T) -> Bool) {
8 |
9 | if array.distance(from: range.lowerBound, to: range.upperBound) < 20 {
10 |
11 | insertionSort(for: &array, range: range, by: areInIncreasingOrder)
12 | } else if depthLimit == 0 {
13 |
14 | heapsort(for: &array, range: range, by: areInIncreasingOrder)
15 | } else {
16 |
17 | let partIdx = partitionIndex(for: &array, subRange: range, by: areInIncreasingOrder)
18 |
19 | introSortImplementation(for: &array, range: range.lowerBound..(for array: inout [T], range: Range, by areInIncreasingOrder: (T, T) -> Bool) {
4 | guard !range.isEmpty else { return }
5 |
6 | let start = range.lowerBound
7 | var sortedEnd = start
8 |
9 | array.formIndex(after: &sortedEnd)
10 | while sortedEnd != range.upperBound {
11 | let x = array[sortedEnd]
12 |
13 | var i = sortedEnd
14 | repeat {
15 | let predecessor = array[array.index(before: i)]
16 |
17 | guard areInIncreasingOrder(x, predecessor) else { break }
18 | array[i] = predecessor
19 | array.formIndex(before: &i)
20 | } while i != start
21 |
22 | if i != sortedEnd {
23 | array[i] = x
24 | }
25 | array.formIndex(after: &sortedEnd)
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/Introsort/Introsort.playground/Sources/Randomize.swift:
--------------------------------------------------------------------------------
1 | import Foundation
2 |
3 | public func randomize(n: Int) -> [Int] {
4 | var unsorted = [Int]()
5 | for _ in 0..(in array: inout [T], a: Int, b: Int, c: Int, by areInIncreasingOrder: (T, T) -> Bool) {
4 | switch (areInIncreasingOrder(array[b], array[a]),
5 | areInIncreasingOrder(array[c], array[b])) {
6 | case (false, false): break
7 | case (true, true): array.swapAt(a, c)
8 | case (true, false):
9 | array.swapAt(a, b)
10 |
11 | if areInIncreasingOrder(array[c], array[b]) {
12 | array.swapAt(b, c)
13 | }
14 | case (false, true):
15 | array.swapAt(b, c)
16 |
17 | if areInIncreasingOrder(array[b], array[a]) {
18 | array.swapAt(a, b)
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Introsort/Introsort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Introsort/Introsort.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Introsort/Introsort.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Introsort/Introsort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Introsort/Introsort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Introsort/Introsort.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/K-Means/Images/k_means_bad1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/K-Means/Images/k_means_bad1.png
--------------------------------------------------------------------------------
/K-Means/Images/k_means_bad2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/K-Means/Images/k_means_bad2.png
--------------------------------------------------------------------------------
/K-Means/Images/k_means_good.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/K-Means/Images/k_means_good.png
--------------------------------------------------------------------------------
/Knuth-Morris-Pratt/KnuthMorrisPratt.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Knuth-Morris-Pratt/KnuthMorrisPratt.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Knuth-Morris-Pratt/KnuthMorrisPratt.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Knuth-Morris-Pratt/KnuthMorrisPratt.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Knuth-Morris-Pratt/KnuthMorrisPratt.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Kth Largest Element/kthLargest.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Kth Largest Element/kthLargest.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Kth Largest Element/kthLargest.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Kth Largest Element/kthLargest.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Kth Largest Element/kthLargest.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Kth Largest Element/kthLargest.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Linear Regression/Images/graph1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Linear Regression/Images/graph1.png
--------------------------------------------------------------------------------
/Linear Regression/Images/graph2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Linear Regression/Images/graph2.png
--------------------------------------------------------------------------------
/Linear Regression/Images/graph3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Linear Regression/Images/graph3.png
--------------------------------------------------------------------------------
/Linear Search/LinearSearch.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 |
3 | func linearSearch(_ array: [T], _ object: T) -> Int? {
4 | for (index, obj) in array.enumerated() where obj == object {
5 | return index
6 | }
7 | return nil
8 | }
9 | /*
10 | 使用尾随闭包简写
11 | */
12 | func linearSearch1(_ array: [T], _ object: T) -> Int? {
13 | return array.index { $0 == object }
14 | }
15 |
16 | linearSearch([2,5,3,12,7,8], 12)
17 |
18 | linearSearch1([2,5,3,12,7,8], 7)
19 |
--------------------------------------------------------------------------------
/Linear Search/LinearSearch.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Linear Search/LinearSearch.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Linear Search/LinearSearch.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Linear Search/LinearSearch.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Linear Search/LinearSearch.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Linear Search/LinearSearch.swift:
--------------------------------------------------------------------------------
1 | func linearSearch(_ array: [T], _ object: T) -> Int? {
2 | for (index, obj) in array.enumerated() where obj == object {
3 | return index
4 | }
5 | return nil
6 | }
7 |
8 | func linearSearch1(_ array: [T], _ object: T) -> Array.Index? {
9 | return array.index { $0 == object }
10 | }
11 |
--------------------------------------------------------------------------------
/Linear Search/README.markdown:
--------------------------------------------------------------------------------
1 | # 线性搜索(Linear Search)
2 |
3 | 目标:在数组中查找特定值。
4 |
5 |
6 | 我们有一组通用对象。 通过线性搜索,我们迭代数组中的所有对象,并将每个对象与我们正在寻找的对象进行比较。 如果两个对象相等,我们停止并返回当前对象在数组中的索引。 如果不相等,只要数组中还有对象,我们就会继续寻找下一个。
7 |
8 | ## 一个例子
9 |
10 | 假设我们有一个数组`[5,2,4,7]`,我们想检查数组是否包含数字`2`。
11 |
12 | 我们首先将数组中的第一个数字`5`与我们正在寻找的数字`2`进行比较。 它们显然不一样,所以我们继续比较下一个数组元素。
13 |
14 | 我们将数组中的数字`2`与数字`2`进行比较,注意到它们是相等的。 现在我们可以停止迭代并返回1,这是数组中数字`2`的索引。
15 |
16 | ## 代码
17 |
18 | 这是Swift线性搜索的简单实现:
19 |
20 | ```swift
21 | func linearSearch(_ array: [T], _ object: T) -> Int? {
22 | for (index, obj) in array.enumerated() where obj == object {
23 | return index
24 | }
25 | return nil
26 | }
27 | ```
28 |
29 | 将此代码放在playground里测试:
30 |
31 | ```swift
32 | let array = [5, 2, 4, 7]
33 | linearSearch(array, 2) // This will return 1
34 | ```
35 |
36 | ## 性能
37 |
38 | 线性搜索性能是**O(n)** 。它将我们要查找的对象与数组中的每个对象进行比较,因此它所花费的时间与数组长度成正比。在最坏的情况下,我们需要查看数组中的所有元素。
39 |
40 | 最好的情况是 **O(1)**,但这种情况很少见,因为我们要查找的对象必须位于数组的开头才能立即找到。你可能会很幸运,但大部分时间你都不会。平均而言,线性搜索需要查看数组中对象的一半。
41 |
42 | ## 扩展阅读
43 |
44 | [线性搜索的维基百科](https://en.wikipedia.org/wiki/Linear_search)
45 |
46 | *作者:[Patrick Balestra](http://www.github.com/BalestraPatrick)*
47 | *翻译:[Andy Ron](https://github.com/andyRon)*
48 | *校对:[Andy Ron](https://github.com/andyRon)*
49 |
50 |
--------------------------------------------------------------------------------
/Linked List/LinkedList.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Linked List/LinkedList.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Linked List/LinkedList.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Linked List/LinkedList.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Linked List/LinkedList.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Merge Sort/MergeSort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Merge Sort/MergeSort.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Merge Sort/MergeSort.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Merge Sort/MergeSort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Merge Sort/MergeSort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Miller-Rabin Primality Test/Images/img_pseudo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Miller-Rabin Primality Test/Images/img_pseudo.png
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/Images/Graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree(Unweighted)/Images/Graph.png
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/Images/Graph.sketch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree(Unweighted)/Images/Graph.sketch
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/Images/MinimumSpanningTree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree(Unweighted)/Images/MinimumSpanningTree.png
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/Images/MinimumSpanningTree.sketch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree(Unweighted)/Images/MinimumSpanningTree.sketch
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/Images/Tree.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree(Unweighted)/Images/Tree.graffle
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/Images/Tree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree(Unweighted)/Images/Tree.png
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/MinimumSpanningTree(unweighted).playground/Sources/Edge.swift:
--------------------------------------------------------------------------------
1 | public class Edge: Equatable {
2 | public var neighbor: Node
3 |
4 | public init(neighbor: Node) {
5 | self.neighbor = neighbor
6 | }
7 | }
8 |
9 | public func == (lhs: Edge, rhs: Edge) -> Bool {
10 | return lhs.neighbor == rhs.neighbor
11 | }
12 |
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/MinimumSpanningTree(unweighted).playground/Sources/Node.swift:
--------------------------------------------------------------------------------
1 | public class Node: CustomStringConvertible, Equatable {
2 | public var neighbors: [Edge]
3 |
4 | public private(set) var label: String
5 | public var distance: Int?
6 | public var visited: Bool
7 |
8 | public init(label: String) {
9 | self.label = label
10 | neighbors = []
11 | visited = false
12 | }
13 |
14 | public var description: String {
15 | if let distance = distance {
16 | return "Node(label: \(label), distance: \(distance))"
17 | }
18 | return "Node(label: \(label), distance: infinity)"
19 | }
20 |
21 | public var hasDistance: Bool {
22 | return distance != nil
23 | }
24 |
25 | public func remove(_ edge: Edge) {
26 | neighbors.remove(at: neighbors.index { $0 === edge }!)
27 | }
28 | }
29 |
30 | public func == (lhs: Node, rhs: Node) -> Bool {
31 | return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
32 | }
33 |
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/MinimumSpanningTree(unweighted).playground/Sources/Queue.swift:
--------------------------------------------------------------------------------
1 | public struct Queue {
2 | private var array: [T]
3 |
4 | public init() {
5 | array = []
6 | }
7 |
8 | public var isEmpty: Bool {
9 | return array.isEmpty
10 | }
11 |
12 | public var count: Int {
13 | return array.count
14 | }
15 |
16 | public mutating func enqueue(_ element: T) {
17 | array.append(element)
18 | }
19 |
20 | public mutating func dequeue() -> T? {
21 | if isEmpty {
22 | return nil
23 | } else {
24 | return array.removeFirst()
25 | }
26 | }
27 |
28 | public func peek() -> T? {
29 | return array.first
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Minimum Spanning Tree(Unweighted)/MinimumSpanningTree(unweighted).playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Minimum Spanning Tree/Images/kruskal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree/Images/kruskal.png
--------------------------------------------------------------------------------
/Minimum Spanning Tree/Images/prim.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree/Images/prim.png
--------------------------------------------------------------------------------
/Minimum Spanning Tree/MinimumSpanningTree.playground/Resources/mst.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree/MinimumSpanningTree.playground/Resources/mst.png
--------------------------------------------------------------------------------
/Minimum Spanning Tree/MinimumSpanningTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Minimum Spanning Tree/MinimumSpanningTree.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Minimum Spanning Tree/MinimumSpanningTree.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Minimum Spanning Tree/MinimumSpanningTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Minimum Spanning Tree/MinimumSpanningTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/MinimumCoinChange/eurocoins.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/MinimumCoinChange/eurocoins.gif
--------------------------------------------------------------------------------
/Multiset/Multiset.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 | var set = Multiset()
3 |
4 | set.add("Foo")
5 | set.add("Foo")
6 | set.add("Bar")
7 | set.add("Baz")
8 |
9 | set.count
10 | set.count(for: "Foo")
11 |
12 | set.allItems
13 |
14 | var set2 = Multiset()
15 | set2.add("Foo")
16 | set2.add("Foo")
17 |
18 | set2.isSubSet(of: set) // true
19 | set.isSubSet(of: set2) // false
20 |
21 | // Convenience constructor: pass a Collection of Hashables to the constructor
22 | var cacti = Multiset("cacti")
23 | cacti.allItems
24 | var tactical = Multiset("tactical")
25 | cacti.isSubSet(of: tactical) // true
26 | tactical.isSubSet(of: cacti) // false
27 |
28 | // Test ExpressibleByArrayLiteral protocol
29 | let set3: Multiset = ["foo", "bar"]
30 | set3.count(for: "foo")
31 |
32 | // Test Equatable protocol
33 | let set4 = Multiset(set3.allItems)
34 | set4 == set3 // true
35 | set4 == set // false
36 |
--------------------------------------------------------------------------------
/Multiset/Multiset.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Naive Bayes Classifier/images/bayes.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Naive Bayes Classifier/images/bayes.gif
--------------------------------------------------------------------------------
/Naive Bayes Classifier/images/code_example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Naive Bayes Classifier/images/code_example.png
--------------------------------------------------------------------------------
/Naive Bayes Classifier/images/mean.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Naive Bayes Classifier/images/mean.gif
--------------------------------------------------------------------------------
/Naive Bayes Classifier/images/multinomial.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Naive Bayes Classifier/images/multinomial.gif
--------------------------------------------------------------------------------
/Naive Bayes Classifier/images/normal_distribution.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Naive Bayes Classifier/images/normal_distribution.gif
--------------------------------------------------------------------------------
/Naive Bayes Classifier/images/standard_deviation.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Naive Bayes Classifier/images/standard_deviation.gif
--------------------------------------------------------------------------------
/Naive Bayes Classifier/images/tennis_dataset.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Naive Bayes Classifier/images/tennis_dataset.png
--------------------------------------------------------------------------------
/Octree/OcTree.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import UIKit
2 | import simd
3 |
4 | let boxMin = vector_double3(0, 2, 6)
5 | let boxMax = vector_double3(5, 10, 9)
6 | let box = Box(boxMin: boxMin, boxMax: boxMax)
7 | var octree = Octree(boundingBox: box, minimumCellSize: 5.0)
8 | var five = octree.add(5, at: vector_double3(3,4,8))
9 | octree.add(8, at: vector_double3(3,4,8.2))
10 | octree.add(10, at: vector_double3(3,4,8.2))
11 | octree.add(7, at: vector_double3(2,5,8))
12 | octree.add(2, at: vector_double3(1,6,7))
13 |
14 | var cont = octree.elements(at: vector_double3(3,4,8.2))
15 | octree.remove(8)
16 | octree.elements(at: vector_double3(3,4,8))
17 |
18 | let boxMin2 = vector_double3(1,3,7)
19 | let boxMax2 = vector_double3(4,9,8)
20 | let box2 = Box(boxMin: boxMin2, boxMax: boxMax2)
21 | box.isContained(in: box2)
22 | box.intersects(box2)
23 |
24 | let boxMin3 = vector_double3(3,8,8)
25 | let boxMax3 = vector_double3(10,12,20)
26 | let box3 = Box(boxMin: boxMin3, boxMax: boxMax3)
27 | box3.intersects(box3)
28 |
29 | octree.elements(in: box)
30 | octree.elements(in: box2)
31 | print(octree)
32 |
--------------------------------------------------------------------------------
/Octree/OcTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Octree/README.md:
--------------------------------------------------------------------------------
1 | # 八叉树(Octree)
2 |
3 | 八叉树是[树](../Tree),其中每个内部(非叶节点)节点有八个子节点。 例如,通常用于游戏中的碰撞检测。
4 |
5 | ### 问题
6 |
7 |
8 | 考虑以下问题:您需要在3D空间中存储多个对象(每个对象在某个位置使用`X`,`Y`和`Z`坐标表示)然后您需要回答哪些对象位于某个3D区域。 一个天真的解决方案是将点存储在一个数组中,然后迭代这些点并分别检查每个点。 该解决方案花费O(n)。
9 |
10 | ### 更好的方法
11 |
12 | 八叉树最常用于通过递归地将其细分为8个区域来划分三维空间。 让我们看看我们如何使用八叉树来存储一些值。
13 |
14 | 树中的每个节点代表一个类似盒子的区域。 叶节点在该区域中存储单个点,并为该点分配一组对象。
15 |
16 | 一旦添加了同一区域内(但在不同点)的对象,叶节点就会变成一个内部节点,并向它添加8个子节点(叶节点)。 先前包含在节点中的所有点都将传递给其对应的子节点并进行存储。 因此,只有叶子包含实际的点和值。
17 |
18 | 为了找到位于给定区域中的点,我们现在可以从上到下遍历树并从节点收集合适的点。
19 |
20 | 在最坏的情况下,添加点和搜索仍然可以占用O(n),因为树不以任何方式平衡。 但是,平均而言,它的运行速度明显更快(与O(log n)相当)。
21 |
22 | ### 扩展阅读
23 |
24 | [八叉树的维基百科](https://en.wikipedia.org/wiki/Octree)
25 | 苹果公司的八叉树实现[GKOctree](https://developer.apple.com/documentation/gameplaykit/gkoctree)
26 |
27 |
28 | *作者:Jaap Wijnen*
29 | *灵感来自于Timur Galimov和苹果公司的八叉树实现*
30 | *翻译:[Andy Ron](https://github.com/andyRon)*
31 | *校对:[Andy Ron](https://github.com/andyRon)*
32 |
33 |
--------------------------------------------------------------------------------
/Ordered Array/OrderedArray.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Ordered Array/OrderedArray.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Ordered Array/OrderedArray.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Ordered Array/OrderedArray.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Ordered Array/OrderedArray.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Ordered Set/OrderedSet.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Ordered Set/OrderedSet.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Priority Queue/PriorityQueue.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 |
3 | public struct PriorityQueue {
4 | fileprivate var heap: Heap
5 |
6 | public init(sort: @escaping (T, T) -> Bool) {
7 | heap = Heap(sort: sort)
8 | }
9 |
10 | public var isEmpty: Bool {
11 | return heap.isEmpty
12 | }
13 |
14 | public var count: Int {
15 | return heap.count
16 | }
17 |
18 | public func peak() -> T? {
19 | return heap.peek()
20 | }
21 |
22 | public mutating func enqueue(_ element: T) {
23 | heap.insert(element)
24 | }
25 |
26 | public mutating func dequeue() -> T? {
27 | return heap.remove()
28 | }
29 |
30 | public mutating func changePriority(index i: Int, value: T) {
31 | return heap.replace(index: i, value: value)
32 | }
33 | }
34 |
35 | extension PriorityQueue where T: Equatable {
36 | public func index(of element: T) -> Int? {
37 | return heap.index(of: element)
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Priority Queue/PriorityQueue.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Priority Queue/PriorityQueue.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Priority Queue/PriorityQueue.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Priority Queue/PriorityQueue.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Priority Queue/PriorityQueue.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/QuadTree/Images/quadtree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/QuadTree/Images/quadtree.png
--------------------------------------------------------------------------------
/QuadTree/QuadTree.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import Foundation
2 |
3 | let tree = QuadTree(rect: Rect(origin: Point(0, 0), size: Size(xLength: 10, yLength: 10)))
4 |
5 | for _ in 0..<40 {
6 | let randomX = Double(arc4random_uniform(100)) / 10
7 | let randomY = Double(arc4random_uniform(100)) / 10
8 | let point = Point(randomX, randomY)
9 | tree.add(point: point)
10 | }
11 |
12 | print(tree)
13 | print(tree.points(inRect: Rect(origin: Point(1, 1), size: Size(xLength: 5, yLength: 5))))
14 |
--------------------------------------------------------------------------------
/QuadTree/QuadTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/QuadTree/QuadTree.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/QuadTree/QuadTree.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/QuadTree/QuadTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/QuadTree/QuadTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Queue/Queue-Optimized.swift:
--------------------------------------------------------------------------------
1 | /*
2 | First-in first-out queue (FIFO)
3 |
4 | New elements are added to the end of the queue. Dequeuing pulls elements from
5 | the front of the queue.
6 |
7 | Enqueuing and dequeuing are O(1) operations.
8 | */
9 | public struct Queue {
10 | fileprivate var array = [T?]()
11 | fileprivate var head = 0
12 |
13 | public var isEmpty: Bool {
14 | return count == 0
15 | }
16 |
17 | public var count: Int {
18 | return array.count - head
19 | }
20 |
21 | public mutating func enqueue(_ element: T) {
22 | array.append(element)
23 | }
24 |
25 | public mutating func dequeue() -> T? {
26 | guard head < array.count, let element = array[head] else { return nil }
27 |
28 | array[head] = nil
29 | head += 1
30 |
31 | let percentage = Double(head)/Double(array.count)
32 | if array.count > 50 && percentage > 0.25 {
33 | array.removeFirst(head)
34 | head = 0
35 | }
36 |
37 | return element
38 | }
39 |
40 | public var front: T? {
41 | if isEmpty {
42 | return nil
43 | } else {
44 | return array[head]
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Queue/Queue-Simple.swift:
--------------------------------------------------------------------------------
1 | /*
2 | First-in first-out queue (FIFO)
3 |
4 | New elements are added to the end of the queue. Dequeuing pulls elements from
5 | the front of the queue.
6 |
7 | Enqueuing is an O(1) operation, dequeuing is O(n). Note: If the queue had been
8 | implemented with a linked list, then both would be O(1).
9 | */
10 | public struct Queue {
11 | fileprivate var array = [T]()
12 |
13 | public var count: Int {
14 | return array.count
15 | }
16 |
17 | public var isEmpty: Bool {
18 | return array.isEmpty
19 | }
20 |
21 | public mutating func enqueue(_ element: T) {
22 | array.append(element)
23 | }
24 |
25 | public mutating func dequeue() -> T? {
26 | if isEmpty {
27 | return nil
28 | } else {
29 | return array.removeFirst()
30 | }
31 | }
32 |
33 | public var front: T? {
34 | return array.first
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Queue/Queue.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Queue/Queue.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Queue/Queue.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Queue/Queue.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Queue/Queue.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Queue/Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Queue/Tests/Tests.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Quicksort/Images/Example.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Quicksort/Images/Example.graffle
--------------------------------------------------------------------------------
/Quicksort/Images/Example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Quicksort/Images/Example.png
--------------------------------------------------------------------------------
/Quicksort/Quicksort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Quicksort/Quicksort.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Quicksort/Quicksort.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Quicksort/Quicksort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Quicksort/Quicksort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Radix Sort/RadixSort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Radix Sort/image/radix_sort.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Radix Sort/image/radix_sort.jpg
--------------------------------------------------------------------------------
/Red-Black Tree/RedBlackTree.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import Foundation
2 |
3 | let redBlackTree = RedBlackTree()
4 |
5 | let randomMax = Double(0x10000000)
6 | var values = [Double]()
7 | for i in 0..<1000 {
8 | let value = Double(arc4random()) / randomMax
9 | values.append(value)
10 | redBlackTree.insert(key: value)
11 |
12 | if i % 100 == 0 {
13 | redBlackTree.verify()
14 | }
15 | }
16 | redBlackTree.verify()
17 |
18 | for i in 0..<1000 {
19 | let rand = arc4random_uniform(UInt32(values.count))
20 | let index = Int(rand)
21 | let value = values.remove(at: index)
22 | redBlackTree.delete(key: value)
23 |
24 | if i % 100 == 0 {
25 | redBlackTree.verify()
26 | }
27 | }
28 | redBlackTree.verify()
29 |
--------------------------------------------------------------------------------
/Red-Black Tree/RedBlackTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Red-Black Tree/RedBlackTree.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Red-Black Tree/RedBlackTree.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Red-Black Tree/RedBlackTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Red-Black Tree/RedBlackTree.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Red-Black Tree/RedBlackTree.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Ring Buffer/RingBuffer.playground/Sources/RingBuffer.swift:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Ring Buffer/RingBuffer.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Ring Buffer/RingBuffer.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Ring Buffer/RingBuffer.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Ring Buffer/RingBuffer.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Ring Buffer/RingBuffer.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Rootish Array Stack/RootishArrayStack.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Rootish Array Stack/RootishArrayStack.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Rootish Array Stack/RootishArrayStack.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Rootish Array Stack/RootishArrayStack.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Rootish Array Stack/RootishArrayStack.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Rootish Array Stack/images/RootishArrayStackExample.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Rootish Array Stack/images/RootishArrayStackExample.png
--------------------------------------------------------------------------------
/Rootish Array Stack/images/RootishArrayStackExample2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Rootish Array Stack/images/RootishArrayStackExample2.png
--------------------------------------------------------------------------------
/Rootish Array Stack/images/RootishArrayStackIntro.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Rootish Array Stack/images/RootishArrayStackIntro.png
--------------------------------------------------------------------------------
/Rootish Array Stack/images/quadratic_equation.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Rootish Array Stack/images/quadratic_equation.jpg
--------------------------------------------------------------------------------
/Rootish Array Stack/images/quadratic_formula.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Rootish Array Stack/images/quadratic_formula.jpg
--------------------------------------------------------------------------------
/SUMMARY.md:
--------------------------------------------------------------------------------
1 |
2 | # Swift 算法俱乐部
3 |
4 | * [双端队列](Deque/README.md)
5 | * [二维数组](Array2D/README.md)
6 | * [图](Graph/README.md)
7 |
8 | ## 数据结构
9 |
10 | ### 数组变体
11 | - [二维数组](Array2D/)
12 | - [固定大小数组](Fixed%20Size%20Array/)
13 |
14 | ### 队列
15 | - [栈](Stack/)
16 | - [队列](Queue/)
17 |
18 | ## 算法列表
19 |
20 |
21 | ### 搜索算法
22 | - [线性搜索](Linear%20Search/)
23 | - [二分搜索](Binary%20Search/README.md)
24 |
--------------------------------------------------------------------------------
/Segment Tree/Images/EqualSegments.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/Images/EqualSegments.png
--------------------------------------------------------------------------------
/Segment Tree/Images/LeftSegment.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/Images/LeftSegment.png
--------------------------------------------------------------------------------
/Segment Tree/Images/MixedSegment.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/Images/MixedSegment.png
--------------------------------------------------------------------------------
/Segment Tree/Images/RightSegment.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/Images/RightSegment.png
--------------------------------------------------------------------------------
/Segment Tree/Images/Structure.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/Images/Structure.png
--------------------------------------------------------------------------------
/Segment Tree/LazyPropagation/Images/Segment-tree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/LazyPropagation/Images/Segment-tree.png
--------------------------------------------------------------------------------
/Segment Tree/LazyPropagation/Images/lazy-sample-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/LazyPropagation/Images/lazy-sample-2.png
--------------------------------------------------------------------------------
/Segment Tree/LazyPropagation/Images/pushUp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/LazyPropagation/Images/pushUp.png
--------------------------------------------------------------------------------
/Segment Tree/LazyPropagation/Images/pushdown.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Segment Tree/LazyPropagation/Images/pushdown.png
--------------------------------------------------------------------------------
/Segment Tree/SegmentTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Select Minimum Maximum/SelectMinimumMaximum.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Select Minimum Maximum/SelectMinimumMaximum.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Selection Sampling/SelectionSampling.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import UIKit
2 |
3 | var str = "Hello, playground"
4 |
--------------------------------------------------------------------------------
/Selection Sampling/SelectionSampling.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Selection Sampling/SelectionSampling.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Selection Sampling/SelectionSampling.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Selection Sampling/SelectionSampling.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Selection Sampling/SelectionSampling.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Selection Sampling/SelectionSampling.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Selection Sort/SelectionSort.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 | func selectionSort(_ array: [Int]) -> [Int] {
3 | guard array.count > 1 else {
4 | return array
5 | }
6 |
7 | var a = array
8 |
9 | for x in 0 ..< a.count - 1 {
10 | var lowest = x
11 | for y in x+1 ..< a.count {
12 | if a[y] < a[lowest] {
13 | lowest = y
14 | }
15 | }
16 |
17 | if x != lowest {
18 | a.swapAt(x, lowest)
19 | }
20 | }
21 | return a
22 | }
23 |
24 | let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
25 | selectionSort(list)
26 |
27 | var l = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
28 | l.swapAt(1, 1)
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Selection Sort/SelectionSort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Selection Sort/SelectionSort.swift:
--------------------------------------------------------------------------------
1 |
2 | public func selectionSort(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
3 | guard array.count > 1 else { return array }
4 |
5 | var a = array
6 | for x in 0 ..< a.count - 1 {
7 |
8 | // Find the lowest value in the rest of the array.
9 | var lowest = x
10 | for y in x + 1 ..< a.count {
11 | if isOrderedBefore(a[y], a[lowest]) {
12 | lowest = y
13 | }
14 | }
15 |
16 | // Swap the lowest value with the current array index.
17 | if x != lowest {
18 | a.swapAt(x, lowest)
19 | }
20 | }
21 | return a
22 | }
23 |
--------------------------------------------------------------------------------
/Shell Sort/Shell Sort.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 |
3 | public func insertSort(_ list: inout[Int], start: Int, gap: Int) {
4 | for i in stride(from: (start + gap), to: list.count, by: gap) {
5 | let currentValue = list[i]
6 | var pos = i
7 | while pos >= gap && list[pos - gap] > currentValue {
8 | list[pos] = list[pos - gap]
9 | pos -= gap
10 | }
11 | list[pos] = currentValue
12 | }
13 | }
14 |
15 | public func shellSort(_ list: inout [Int]) {
16 | var sublistCount = list.count / 2
17 | while sublistCount > 0 {
18 | for pos in 0 ..< sublistCount {
19 | insertSort(&list, start: pos, gap: sublistCount)
20 | }
21 | sublistCount = sublistCount / 2
22 | }
23 | }
24 |
25 | var arr = [34, 32, 50, 23, 73, 2, -1, 42, 31, 5]
26 |
27 | shellSort(&arr)
28 |
29 | print(arr)
30 |
--------------------------------------------------------------------------------
/Shell Sort/Shell Sort.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Shell Sort/Shell Sort.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Shell Sort/Shell Sort.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Shell Sort/Shell Sort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Shell Sort/Shell Sort.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Shell Sort/image/shell_sort_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Shell Sort/image/shell_sort_1.png
--------------------------------------------------------------------------------
/Shell Sort/image/shell_sort_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Shell Sort/image/shell_sort_2.png
--------------------------------------------------------------------------------
/Shortest Path (Unweighted)/Images/Graph.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Shortest Path (Unweighted)/Images/Graph.graffle
--------------------------------------------------------------------------------
/Shortest Path (Unweighted)/Images/Graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Shortest Path (Unweighted)/Images/Graph.png
--------------------------------------------------------------------------------
/Shortest Path (Unweighted)/ShortestPath.playground/Sources/Edge.swift:
--------------------------------------------------------------------------------
1 | public class Edge: Equatable {
2 | public var neighbor: Node
3 |
4 | public init(neighbor: Node) {
5 | self.neighbor = neighbor
6 | }
7 | }
8 |
9 | public func == (lhs: Edge, rhs: Edge) -> Bool {
10 | return lhs.neighbor == rhs.neighbor
11 | }
12 |
13 |
--------------------------------------------------------------------------------
/Shortest Path (Unweighted)/ShortestPath.playground/Sources/Node.swift:
--------------------------------------------------------------------------------
1 | public class Node: CustomStringConvertible, Equatable {
2 | public var neighbors: [Edge]
3 |
4 | public private(set) var label: String
5 | public var distance: Int?
6 | public var visited: Bool
7 |
8 | public init(label: String) {
9 | self.label = label
10 | neighbors = []
11 | visited = false
12 | }
13 |
14 | public var description: String {
15 | if let distance = distance {
16 | return "Node(label: \(label), distance: \(distance))"
17 | }
18 | return "Node(label: \(label), distance: infinity)"
19 | }
20 |
21 | public var hasDistance: Bool {
22 | return distance != nil
23 | }
24 |
25 | public func remove(edge: Edge) {
26 | neighbors.remove(at: neighbors.index { $0 === edge }!)
27 | }
28 | }
29 |
30 | public func == (lhs: Node, rhs: Node) -> Bool {
31 | return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/Shortest Path (Unweighted)/ShortestPath.playground/Sources/Queue.swift:
--------------------------------------------------------------------------------
1 | public struct Queue {
2 | private var array: [T]
3 |
4 | public init() {
5 | array = []
6 | }
7 |
8 | public var isEmpty: Bool {
9 | return array.isEmpty
10 | }
11 |
12 | public var count: Int {
13 | return array.count
14 | }
15 |
16 | public mutating func enqueue(element: T) {
17 | array.append(element)
18 | }
19 |
20 | public mutating func dequeue() -> T? {
21 | if isEmpty {
22 | return nil
23 | } else {
24 | return array.removeFirst()
25 | }
26 | }
27 |
28 | public func peek() -> T? {
29 | return array.first
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Shortest Path (Unweighted)/ShortestPath.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Single-Source Shortest Paths (Weighted)/img/example_graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Single-Source Shortest Paths (Weighted)/img/example_graph.png
--------------------------------------------------------------------------------
/Single-Source Shortest Paths (Weighted)/img/negative_cycle_example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Single-Source Shortest Paths (Weighted)/img/negative_cycle_example.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert1.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert10.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert11.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert12.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert2.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert3.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert4.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert5.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert6.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert8.png
--------------------------------------------------------------------------------
/Skip-List/Images/Insert9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Insert9.png
--------------------------------------------------------------------------------
/Skip-List/Images/Intro.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Intro.png
--------------------------------------------------------------------------------
/Skip-List/Images/Search1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/Search1.png
--------------------------------------------------------------------------------
/Skip-List/Images/insert7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/Images/insert7.png
--------------------------------------------------------------------------------
/Skip-List/SkipList.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import UIKit
2 |
3 | var str = "Hello, playground"
4 |
--------------------------------------------------------------------------------
/Skip-List/SkipList.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Skip-List/SkipList.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Skip-List/SkipList.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Skip-List/SkipList.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Skip-List/SkipList.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Skip-List/SkipList.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Slow Sort/README.markdown:
--------------------------------------------------------------------------------
1 | # 慢排序(Slow Sort)
2 |
3 |
4 | 目标:将数字数组从低到高(或从高到低)排序。
5 |
6 | 您将获得一系列数字,需要按正确的顺序排列。 插入排序算法【??】的工作原理如下:
7 |
8 | 我们可以分解按升序排序n个数字的问题
9 |
10 | 1. 找到最大数字
11 | 1. 找到前n/2个元素的最大值
12 | 2. 找到剩余n/2个元素的最大值
13 | 3. 找到这两个最大值中的最大值
14 | 2. 对其余的进行排序
15 |
16 | ## 代码
17 |
18 | 这是慢速排序在Swift中的实现:
19 |
20 | ```swift
21 | public func slowsort(_ i: Int, _ j: Int) {
22 | if i>=j {
23 | return
24 | }
25 | let m = (i+j)/2
26 | slowsort(i,m)
27 | slowsort(m+1,j)
28 | if numberList[j] < numberList[m] {
29 | let temp = numberList[j]
30 | numberList[j] = numberList[m]
31 | numberList[m] = temp
32 | }
33 | slowsort(i,j-1)
34 | }
35 | ```
36 |
37 | ## 性能
38 |
39 | | Case | 性能 |
40 | |:-------------: |:---------------:|
41 | | 最差 | slow |
42 | | 最好 | O(n^(log(n)/(2+e)))) |
43 | | 平均 | O(n^(log(n)/2)) |
44 |
45 | ## 扩展阅读
46 |
47 | [慢排序的详细说明](http://c2.com/cgi/wiki?SlowSort)
48 |
49 |
50 | *作者:Lukas Schramm*
51 | *翻译:[Andy Ron](https://github.com/andyRon)*
52 | *校对:[Andy Ron](https://github.com/andyRon)*
53 |
54 | (used the Insertion Sort Readme as template)
55 |
--------------------------------------------------------------------------------
/Sparse Table/Images/idempotency.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Sparse Table/Images/idempotency.png
--------------------------------------------------------------------------------
/Sparse Table/Images/query.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Sparse Table/Images/query.png
--------------------------------------------------------------------------------
/Sparse Table/Images/query_example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Sparse Table/Images/query_example.png
--------------------------------------------------------------------------------
/Sparse Table/Images/recursion.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Sparse Table/Images/recursion.png
--------------------------------------------------------------------------------
/Sparse Table/Images/recursion_examples.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Sparse Table/Images/recursion_examples.png
--------------------------------------------------------------------------------
/Sparse Table/Images/structure.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Sparse Table/Images/structure.png
--------------------------------------------------------------------------------
/Sparse Table/Images/structure_examples.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Sparse Table/Images/structure_examples.png
--------------------------------------------------------------------------------
/Splay Tree/Images/example-zigzig-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/example-zigzig-1.png
--------------------------------------------------------------------------------
/Splay Tree/Images/example-zigzig-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/example-zigzig-2.png
--------------------------------------------------------------------------------
/Splay Tree/Images/example-zigzig-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/example-zigzig-3.png
--------------------------------------------------------------------------------
/Splay Tree/Images/example-zigzig-4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/example-zigzig-4.png
--------------------------------------------------------------------------------
/Splay Tree/Images/example-zigzig-5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/example-zigzig-5.png
--------------------------------------------------------------------------------
/Splay Tree/Images/example1-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/example1-1.png
--------------------------------------------------------------------------------
/Splay Tree/Images/example1-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/example1-2.png
--------------------------------------------------------------------------------
/Splay Tree/Images/example1-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/example1-3.png
--------------------------------------------------------------------------------
/Splay Tree/Images/examplezig1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/examplezig1.png
--------------------------------------------------------------------------------
/Splay Tree/Images/examplezig2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/examplezig2.png
--------------------------------------------------------------------------------
/Splay Tree/Images/examplezigzig1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/examplezigzig1.png
--------------------------------------------------------------------------------
/Splay Tree/Images/examplezigzig2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/examplezigzig2.png
--------------------------------------------------------------------------------
/Splay Tree/Images/examplezigzig3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/examplezigzig3.png
--------------------------------------------------------------------------------
/Splay Tree/Images/worst-case-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/worst-case-1.png
--------------------------------------------------------------------------------
/Splay Tree/Images/worst-case-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/worst-case-2.png
--------------------------------------------------------------------------------
/Splay Tree/Images/worst-case-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/worst-case-3.png
--------------------------------------------------------------------------------
/Splay Tree/Images/worst-case-4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/worst-case-4.png
--------------------------------------------------------------------------------
/Splay Tree/Images/worst-case-5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/worst-case-5.png
--------------------------------------------------------------------------------
/Splay Tree/Images/worst-case-6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/worst-case-6.png
--------------------------------------------------------------------------------
/Splay Tree/Images/zig.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/zig.png
--------------------------------------------------------------------------------
/Splay Tree/Images/zigzag1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/zigzag1.png
--------------------------------------------------------------------------------
/Splay Tree/Images/zigzag2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/zigzag2.png
--------------------------------------------------------------------------------
/Splay Tree/Images/zigzig-wrongrotated.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/zigzig-wrongrotated.png
--------------------------------------------------------------------------------
/Splay Tree/Images/zigzig1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/zigzig1.png
--------------------------------------------------------------------------------
/Splay Tree/Images/zigzig2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Splay Tree/Images/zigzig2.png
--------------------------------------------------------------------------------
/Splay Tree/SplayTree.playground/Contents.swift:
--------------------------------------------------------------------------------
1 |
2 | let splayTree = SplayTree(value: 1)
3 | splayTree.insert(value: 2)
4 | splayTree.insert(value: 10)
5 | splayTree.insert(value: 6)
6 |
7 | splayTree.remove(value: 10)
8 | splayTree.remove(value: 6)
9 |
10 | splayTree.insert(value: 55)
11 | splayTree.insert(value: 559)
12 | splayTree.remove(value: 2)
13 | splayTree.remove(value: 1)
14 | splayTree.remove(value: 55)
15 | splayTree.remove(value: 559)
16 |
17 | splayTree.insert(value: 1843000)
18 | splayTree.insert(value: 1238)
19 | splayTree.insert(value: -1)
20 | splayTree.insert(value: 87)
21 |
22 | splayTree.minimum()
23 | splayTree.maximum()
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/Splay Tree/SplayTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Stack/Stack.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Stack/Stack.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Stack/Stack.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Stack/Stack.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Stack/Stack.playground/playground.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Stack/Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Stack/Tests/Tests.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Stack/Tests/Tests.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Stack/Tests/Tests.xcodeproj/project.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Stack/Tests/Tests.xcodeproj/project.xcworkspace/xcuserdata/andyron.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Base.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Base.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Full.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Full.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Insert1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Insert1.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Insert2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Insert2.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Insert3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Insert3.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Partial.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Partial.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Remove1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Remove1.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Remove2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Remove2.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Remove3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Remove3.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/Images/Remove4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Threaded Binary Tree/Images/Remove4.png
--------------------------------------------------------------------------------
/Threaded Binary Tree/ThreadedBinaryTree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Topological Sort/Images/Algorithms.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/Algorithms.graffle
--------------------------------------------------------------------------------
/Topological Sort/Images/Algorithms.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/Algorithms.png
--------------------------------------------------------------------------------
/Topological Sort/Images/Example.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/Example.graffle
--------------------------------------------------------------------------------
/Topological Sort/Images/Example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/Example.png
--------------------------------------------------------------------------------
/Topological Sort/Images/Graph.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/Graph.graffle
--------------------------------------------------------------------------------
/Topological Sort/Images/Graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/Graph.png
--------------------------------------------------------------------------------
/Topological Sort/Images/GraphResult.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/GraphResult.graffle
--------------------------------------------------------------------------------
/Topological Sort/Images/GraphResult.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/GraphResult.png
--------------------------------------------------------------------------------
/Topological Sort/Images/InvalidSort.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/InvalidSort.graffle
--------------------------------------------------------------------------------
/Topological Sort/Images/InvalidSort.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/InvalidSort.png
--------------------------------------------------------------------------------
/Topological Sort/Images/TopologicalSort.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/TopologicalSort.graffle
--------------------------------------------------------------------------------
/Topological Sort/Images/TopologicalSort.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Topological Sort/Images/TopologicalSort.png
--------------------------------------------------------------------------------
/Tree/Images/Cycles.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Tree/Images/Cycles.graffle
--------------------------------------------------------------------------------
/Tree/Images/Cycles.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Tree/Images/Cycles.png
--------------------------------------------------------------------------------
/Tree/Images/Example.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Tree/Images/Example.graffle
--------------------------------------------------------------------------------
/Tree/Images/Example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Tree/Images/Example.png
--------------------------------------------------------------------------------
/Tree/Images/ParentChildren.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Tree/Images/ParentChildren.graffle
--------------------------------------------------------------------------------
/Tree/Images/ParentChildren.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Tree/Images/ParentChildren.png
--------------------------------------------------------------------------------
/Tree/Images/Tree.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Tree/Images/Tree.graffle
--------------------------------------------------------------------------------
/Tree/Images/Tree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Tree/Images/Tree.png
--------------------------------------------------------------------------------
/Tree/Tree.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Trie/images/trie.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Trie/images/trie.png
--------------------------------------------------------------------------------
/Under Construction.markdown:
--------------------------------------------------------------------------------
1 | # Under Construction :construction:
2 |
3 | Here you'll find algorithms that are currently under construction. Suggestions and feedback is welcome!
4 |
5 | ### Sorting
6 |
7 | Special-purpose sorts:
8 | - [Radix Sort](Radix%20Sort/)
9 |
10 | ### Special-purpose sorts:
11 |
12 | - [Bucket Sort](Bucket%20Sort/)
13 |
14 | ### Queues
15 |
16 | - [Bounded Priority Queue](Bounded%20Priority%20Queue). A queue that is bounded to have a limited number of elements.
17 |
18 | ### Trees
19 |
20 | - [AVL Tree](AVL%20Tree/). A binary search tree that balances itself using rotations.
21 | - [Red-Black Tree](Red-Black%20Tree/)
22 | - [Threaded Binary Tree](Threaded%20Binary%20Tree/)
23 | - [Ternary Search Tree](Ternary%20Search%20Tree/)
24 | - [Trie](Trie/)
25 | - [Radix Tree](Radix%20Tree/)
26 |
27 | ### Miscellaneous
28 |
29 | - [Minimum Edit Distance](Minimum%20Edit%20Distance/). Measure the similarity of two strings by counting the number of operations required to transform one string into the other.
30 | - [Treap](Treap/)
31 | - [Set Cover (Unweighted)](Set%20Cover%20(Unweighted)/)
32 |
--------------------------------------------------------------------------------
/Union-Find/Images/AfterFind.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Union-Find/Images/AfterFind.png
--------------------------------------------------------------------------------
/Union-Find/Images/AfterUnion.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Union-Find/Images/AfterUnion.png
--------------------------------------------------------------------------------
/Union-Find/Images/BeforeFind.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Union-Find/Images/BeforeFind.png
--------------------------------------------------------------------------------
/Union-Find/Images/BeforeUnion.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andyRon/swift-algorithm-club-cn/0b16e61811ecab462b67a80d3592aa55b144ed03/Union-Find/Images/BeforeUnion.png
--------------------------------------------------------------------------------
/Union-Find/UnionFind.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import UIKit
2 |
3 | var str = "Hello, playground"
4 |
--------------------------------------------------------------------------------
/Union-Find/UnionFind.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Union-Find/UnionFind.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/Union-Find/UnionFind.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/What are Algorithms.markdown:
--------------------------------------------------------------------------------
1 | # 什么是算法和数据结构?
2 |
3 | 一个算法就像一张教计算机“烹饪”的“食谱”。如果你了解做菜的过程,你就能理解算法的定义。
4 |
5 | 下面有一张关于[制作薄饼的食谱](http://allrecipes.com/recipe/21014/good-old-fashioned-pancakes/):
6 |
7 | 1. 在一个大碗里,将面粉,发酵粉,盐和糖过筛。
8 | 2. 加入牛奶、鸡蛋和融化了的奶油。
9 | 3. 搅拌混合直至丝柔顺滑。
10 | 4. 中火加热平底锅。
11 | 5. 将搅拌好的面糊缓慢倒入锅中,每个薄饼大概用1/4杯的面糊即可。
12 | 6. 薄饼两面都呈黄褐色即可出锅享用了。
13 |
14 | 这张食谱由一系列的步骤组成,你只需一步接一步按照指令来就可以了。算法也是如此,只不过它的指令是交给计算机去执行的,而不是厨师。
15 |
16 | 这些原料(面粉、牛奶、鸡蛋、黄油等)相当于算法中的待处理数据。这些原始数据(各种不同的原料)作为算法的输入,输出的数据(好吃的薄饼)即是结果。
17 |
18 | 那么数据结构是什么?它们是在算法运行时保存数据的容器。例如,在煎饼配方中,数据结构是用于盛放面粉的袋子,是用于将所有东西组合在一起的搅拌碗,是用于煎饼的煎锅,是最后用于盛放成品煎饼的盘子。
--------------------------------------------------------------------------------
/Why Algorithms.markdown:
--------------------------------------------------------------------------------
1 | # 为什么要学习算法与数据结构?
2 |
3 | 如果你已经写过一些代码,你也许就会好奇学习算法和数据结构的意义何在,特别是如果你没有接受过计算机科学专业的高等教育。
4 |
5 | 毕竟,在平时的编码工作中,到底会有多少机会需要自己亲自编写一个链表或排序算法呢?答案是:可能永远都不会。
6 |
7 | #### **然而...**
8 |
9 | 了解一点现有算法解决问题时所用的奇技淫巧可能会给予你启发,让你能更好地优化自己的代码。
10 |
11 | 除了自带的标准数组和字典之外,了解一些其它的数据结构让你在构建自己的 App 时拥有更多的选择。
12 |
13 | 学习算法和数据结构会助你成为更好的开发者!(当然也就意味了可以赚到更多的$$$)
14 |
15 | #### 运用算法能让你编写出其它方式编写不出的软件
16 |
17 | 过去曾经在编写 App 时遇到过瓶颈,无法继续编写下去,因为我陷入到了一些基础的问题中,无法自拔。
18 |
19 | 通常的瓶颈都是运行速度的问题,我的 App 不够快。现在回想起来,原因多是我在解决问题是选用了错误的算法。如果我当时知道 **O(n)** 和 **O(n^2)** 之间的区别,或许就能克服这些瓶颈了。
20 |
21 | 对于小规模的数据量简单粗暴的方法往往能很好的完成工作,但事情并不总是如人所愿。对于大数据量,你需要使用更加聪明的算法。
22 |
23 | 有些时候,我对自己所面对的问题根本就手足无措,甚至写不出来一个运行较慢的正确算法,不知道从何处下手。这时候,如果了解一些算法理论,就会有更多的方法可供尝试。
24 |
25 | #### 不要死记硬背算法实现
26 |
27 | 本文的目的不在死记硬背算法实现。你应该试着去理解不同的算法是如何解决不同的问题的,而不是死记硬背。
28 |
29 | 学习一些算法技术,例如:分而治之、动态规划、贪婪算法等,可以助你更好地理解算法之间的快慢差异,并学会在算法对时间和空间的使用上做出平衡。
30 |
31 | 所以本项目的主要目的是教会你如何更好的跟计算机打交道。
32 |
33 | #### 放轻松,算法并没有听起来那么可怕
34 |
35 | 很多算法书开篇就摆上来很多数学理论,数学公式固然有用,但初学并不需要。所以不要被那些公式吓到了。只要写过代码,你就能很好地理解那些神奇的算法和数据结构背后的原理。
36 |
37 | 相信我,算法是很有趣的。:-)
--------------------------------------------------------------------------------