Skip to content

Commit f009f67

Browse files
Merge pull request #15 from avillega/master
Update Readme.md
2 parents c7447ef + 4dfa69f commit f009f67

File tree

2 files changed

+205
-1
lines changed

2 files changed

+205
-1
lines changed

README.md

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,167 @@
1-
# Go
1+
# The Algorithms - Go
2+
3+
### All algorithms implemented in Go (for education)
4+
5+
These are for demonstration purposes only. There are many implementations of sorts in the Go standard library that are much better for performance reasons.
6+
7+
## Sort Algorithms
8+
9+
10+
### Bubble
11+
![alt text][bubble-image]
12+
13+
From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
14+
15+
__Properties__
16+
* Worst case performance O(n^2)
17+
* Best case performance O(n)
18+
* Average case performance O(n^2)
19+
20+
###### View the algorithm in [action][bubble-toptal]
21+
22+
23+
24+
### Insertion
25+
![alt text][insertion-image]
26+
27+
From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
28+
29+
__Properties__
30+
* Worst case performance O(n^2)
31+
* Best case performance O(n)
32+
* Average case performance O(n^2)
33+
34+
###### View the algorithm in [action][insertion-toptal]
35+
36+
37+
### Merge
38+
![alt text][merge-image]
39+
40+
From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.
41+
42+
__Properties__
43+
* Worst case performance O(n log n)
44+
* Best case performance O(n)
45+
* Average case performance O(n)
46+
47+
48+
###### View the algorithm in [action][merge-toptal]
49+
50+
### Quick
51+
![alt text][quick-image]
52+
53+
From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.
54+
55+
__Properties__
56+
* Worst case performance O(n^2)
57+
* Best case performance O(n log n) or O(n) with three-way partition
58+
* Average case performance O(n^2)
59+
60+
###### View the algorithm in [action][quick-toptal]
61+
62+
### Selection
63+
![alt text][selection-image]
64+
65+
From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
66+
67+
__Properties__
68+
* Worst case performance O(n^2)
69+
* Best case performance O(n^2)
70+
* Average case performance O(n^2)
71+
72+
###### View the algorithm in [action][selection-toptal]
73+
74+
### Shell
75+
![alt text][shell-image]
76+
77+
From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted.
78+
79+
__Properties__
80+
* Worst case performance O(nlog2 2n)
81+
* Best case performance O(n log n)
82+
* Average case performance depends on gap sequence
83+
84+
###### View the algorithm in [action][shell-toptal]
85+
86+
### Time-Compexity Graphs
87+
88+
Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort)
89+
90+
[Complexity Graphs](https://github.com/prateekiiest/Python/blob/master/sorts/sortinggraphs.png)
91+
92+
----------------------------------------------------------------------------------
93+
94+
## Search Algorithms
95+
96+
### Linear
97+
![alt text][linear-image]
98+
99+
From [Wikipedia][linear-wiki]: linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.
100+
Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list.
101+
102+
__Properties__
103+
* Worst case performance O(n)
104+
* Best case performance O(1)
105+
* Average case performance O(n)
106+
* Worst case space complexity O(1) iterative
107+
108+
### Binary
109+
![alt text][binary-image]
110+
111+
From [Wikipedia][binary-wiki]: Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful.
112+
113+
__Properties__
114+
* Worst case performance O(log n)
115+
* Best case performance O(1)
116+
* Average case performance O(log n)
117+
* Worst case space complexity O(1)
118+
119+
----------------------------------------------------------------------------------------------------------------------
120+
121+
## Ciphers
122+
123+
### Caesar
124+
![alt text][caesar]<br>
125+
In cryptography, a **Caesar cipher**, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.<br>
126+
It is **a type of substitution cipher** in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. <br>
127+
The method is named after **Julius Caesar**, who used it in his private correspondence.<br>
128+
The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security.
129+
###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Caesar_cipher)
130+
131+
### Transposition
132+
In cryptography, a **transposition cipher** is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered).<br>
133+
Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt.
134+
###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher)
135+
136+
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
137+
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
138+
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
139+
140+
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
141+
[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort
142+
[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"
143+
144+
[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort
145+
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
146+
[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort"
147+
148+
[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort
149+
[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort
150+
[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort"
151+
152+
[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort
153+
[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort
154+
[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort"
155+
156+
[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort
157+
[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort
158+
[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort"
159+
160+
[linear-wiki]: https://en.wikipedia.org/wiki/Linear_search
161+
[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif
162+
163+
[binary-wiki]: https://en.wikipedia.org/wiki/Binary_search_algorithm
164+
[binary-image]: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png
165+
166+
167+
[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg

eratosthenesSieve.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
//Only works for primes smaller or equal to 10e7
8+
func sieve(upperBound int64) []int64 {
9+
_sieveSize := upperBound + 10
10+
//Creates set to mark wich numbers are primes and wich are not
11+
//true: not primes, false: primes
12+
//this to favor default initialization of arrays in go
13+
var bs [10000010]bool
14+
//creates a slice to save the primes it finds
15+
primes := make([]int64, 0, 1000)
16+
17+
bs[0] = true
18+
bs[1] = true
19+
//iterate over the numbers set
20+
for i := int64(0); i <= _sieveSize; i++ {
21+
//if find one number that is not marked as a compund number, mark all its multiples
22+
if !bs[i] {
23+
for j := i * i; j <= _sieveSize; j += i {
24+
bs[j] = true
25+
}
26+
//Add the prime you just find to the slice of primes
27+
primes = append(primes, i)
28+
}
29+
}
30+
return primes
31+
}
32+
33+
func main() {
34+
//prints first N primes into console
35+
N := 100
36+
primes := sieve(N)
37+
fmt.Println(primes)
38+
}

0 commit comments

Comments
 (0)