forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"How to contribute" and "Big-O" have their own pages Add page on why algorithms are awesome
- Loading branch information
Showing
4 changed files
with
109 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# A note on Big-O notation | ||
|
||
It's useful to know how fast an algorithm is and how much space it needs. This allows you to pick the right algorithm for the job. | ||
|
||
Big-O notation gives you a rough indication of the running time of an algorithm and the amount of memory it uses. When someone says, "This algorithm has worst-case running time of **O(n^2)** and uses **O(n)** space," they mean it's kinda slow but doesn't need lots of extra memory. | ||
|
||
Figuring out the Big-O of an algorithm is usually done through mathematical analysis. We're skipping the math here, but it's useful to know what the different values mean, so here's a handy table. **n** refers to the number of data items that you're processing. For example, when sorting an array of 100 items, **n = 100**. | ||
|
||
Big-O | Name | Description | ||
------| ---- | ----------- | ||
**O(1)** | constant | **This is the best.** The algorithm always takes the same amount of time, regardless of how much data there is. Example: looking up an element of an array by its index. | ||
**O(log n)** | logarithmic | **Pretty great.** These kinds of algorithms halve the amount of data with each iteration. If you have 100 items, it takes about 7 steps to find the answer. With 1,000 items, it takes 10 steps. And 1,000,000 items only take 20 steps. This is super fast even for large amounts of data. Example: binary search. | ||
**O(n)** | linear | **Good performance.** If you have 100 items, this does 100 units of work. Doubling the number of items makes the algorithm take exactly twice as long (200 units of work). Example: sequential search. | ||
**O(n log n)** | "linearithmic" | **Decent performance.** This is slightly worse than linear but not too bad. Example: the fastest general-purpose sorting algorithms. | ||
**O(n^2)** | quadratic | **Kinda slow.** If you have 100 items, this does 100^2 = 10,000 units of work. Doubling the number of items makes it four times slower (because 2 squared equals 4). Example: algorithms using nested loops, such as insertion sort. | ||
**O(n^3)** | cubic | **Poor performance.** If you have 100 items, this does 100^3 = 1,000,000 units of work. Doubling the input size makes it eight times slower. Example: matrix multiplication. | ||
**O(2^n)** | exponential | **Very poor performance.** You want to avoid these kinds of algorithms, but sometimes you have no choice. Example: traveling salesperson problem. | ||
**O(n!)** | factorial | **Intolerably slow.** It literally takes a million years to do anything. | ||
|
||
Often you don't need math to figure out what the Big-O of an algorithm is but you can simply use your intuition. If your code uses a single loop that looks at all **n** elements of your input, the algorithm is **O(n)**. If the code has two nested loops, it is **O(n^2)**. Three nested loops gives **O(n^3)**, and so on. | ||
|
||
Note that Big-O notation is an estimate and is only really useful for large values of **n**. For example, the worst-case running time for the [insertion sort](Insertion Sort/) algorithm is **O(n^2)**. In theory that is worse than the running time for [merge sort](Merge Sort/), which is **O(n log n)**. But for small amounts of data, insertion sort is actually faster, especially if the array is partially sorted already! | ||
|
||
If you find this confusing, don't let this Big-O stuff bother you too much. It's mostly useful when comparing two algorithms to figure out which one is better. But in the end you still want to test in practice which one really is the best. And if the amount of data is relatively small, then even a slow algorithm will be fast enough for practical use. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# How to contribute | ||
|
||
Want to help out with the Swift Algorithm Club? Great! | ||
|
||
## What sort of things can you contribute? | ||
|
||
Take a look at the [list](README.markdown). Any algorithms or data structures that don't have a link yet are up for grabs. | ||
|
||
New algorithms and data structures are always welcome (even if they aren't on the list). | ||
|
||
We're always interested in improvements to existing implementations and better explanations. Suggestions for making the code more Swift-like or to make it fit better with the standard library. | ||
|
||
Unit tests. Fixes for typos. No contribution is too small. :-) | ||
|
||
## Please follow this process | ||
|
||
To keep this a high quality repo, please follow this process when submitting your contribution: | ||
|
||
1. Create a pull request to "claim" an algorithm or data structure. Just so multiple people don't work on the same thing. | ||
2. Use this [style guide](https://github.com/raywenderlich/swift-style-guide) for writing code (more or less). | ||
3. Write an explanation of how the algorithm works. Include **plenty of examples** for readers to follow along. Pictures are good. Take a look at [the explanation of quicksort](Quicksort/) to get an idea. | ||
4. Include your name in the explanation, something like *Written by Your Name* at the end of the document. If you wrote it, you deserve the credit and fame. | ||
5. Add a playground and/or unit tests. | ||
|
||
Just so you know, I will probably edit your text and code for grammar etc, just to ensure a certain level of polish. | ||
|
||
## Want to chat? | ||
|
||
This isn't just a repo with a bunch of code... If you want to learn more about how an algorithm works or want to discuss better ways of solving problems, then open a [Github issue](https://github.com/hollance/swift-algorithm-club/issues) and we'll talk! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Why learn algorithms and data structures? | ||
|
||
If you've been coding for while you may wonder what the point is of learning about algorithms and data structures, especially if you don't have a formal computer science or engineering background. | ||
|
||
After all, how often do you actually have to use a linked list or write your own sort routine when you're making apps? The answer is: almost never. | ||
|
||
#### **However...** | ||
|
||
Knowing a little bit about the strategies used by algorithms to solve tricky problems gives you ideas for improvements you can make to your own code. | ||
|
||
Knowing more data structures than just the standard array and dictionary gives you a bigger collection of tools you can use to build your own apps. | ||
|
||
It will make you a better developer! (And better developers make more $$$.) | ||
|
||
#### Algorithms lets you build software you couldn't otherwise build | ||
|
||
There have been apps that I've been unable to create in the past because I got stuck on fundamental issues. | ||
|
||
Often it was a matter of speed: I just couldn't make the program go fast enough. Thinking back on this now, I had chosen the wrong algorithms for these problems. If I had known more about the difference between **O(n)** and **O(n^2)**, then maybe I would have had better luck. | ||
|
||
Naive brute-force solutions work fine for small amounts of data, but sometimes you need to deal with lots of data. And then you need smarter algorithms. | ||
|
||
There were also times I wasn't able to solve my programming problems at all, not even slowly. I simply didn't know where to begin. Understanding a bit of algorithm theory gives you various tactics you can try. | ||
|
||
#### Don't spend any time memorizing algorithms | ||
|
||
That's not the point. Instead, try to understand how different algorithms approach different problems. | ||
|
||
Learn about techniques such as divide-and-conquer, dynamic programming, greedy algorithms. See what makes one approach slow and another fast, and learn what the tradeoffs are. | ||
|
||
The key thing here is to get insight in how we can make computers do things. | ||
|
||
#### It's not as scary as it sounds | ||
|
||
A lot of algorithm textbooks start with a bunch of math. Truth is, the math is useful but most of the time you won't need it. So don't let that scare you. If you can write code, you can also understand all these fancy algorithms and data structures. | ||
|
||
Trust me, algorithms are fun. :-) |