Skip to content

Commit 8ee3c76

Browse files
authored
Merge branch 'master' into gaussian_elimination_julia
2 parents ffc0198 + 4017a1e commit 8ee3c76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3047
-337
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Contributing
2+
3+
A contribution guide on how to contribute to the Arcane Algorithm Archive (AAA) can be found on this Wiki page:
4+
https://github.com/algorithm-archivists/algorithm-archive/wiki/How-to-Contribute
5+
6+
The community member Buttercak3 also created a video series, explaining the contribution process of the AAA.
7+
You can find a playlist with all videos here:
8+
https://www.youtube.com/playlist?list=PL5NSPcN6fRq2vwgdb9noJacF945CeBk8x

CONTRIBUTORS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
This file lists everyone, who contributed to this repo and wanted to show up here. If you're looking for information on contribution, please check the `CONTRIBUTING.md` out.
2+
3+
# Contributors
4+
15
James Schloss
26
<br>
37
Nicole Mazzuca
@@ -44,3 +48,10 @@ Max Weinstein
4448
<br>
4549
Gibus Wearing Brony
4650
<br>
51+
Gorzoid
52+
<br>
53+
Arun Sahadeo
54+
<br>
55+
NIFR91
56+
<br>
57+
Michal Hanajik

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and live coded on Twitch: https://www.twitch.tv/simuleios.
1515
If you would like to communicate more directly, please feel free to go to our discord: https://discord.gg/Pr2E9S6.
1616

1717

18-
Note that the this project is is essentially a book about algorithms collaboratively written by an online community.
18+
Note that the this project is essentially a book about algorithms collaboratively written by an online community.
1919
Fortunately, there are a lot of algorithms out there, which means that there is a lot of content material available.
2020
Unfortunately, this means that we will probably never cover every algorithm ever created and instead need to focus on what the community sees as useful and necessary.
2121
That said, we'll still cover a few algorithms for fun that have very little, if any practical purpose.

book.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
"name": "Julia",
3434
"default": true
3535
},
36-
{
37-
"lang": "pseudo",
38-
"name": "Pseudocode"
39-
},
4036
{
4137
"lang": "cs",
4238
"name": "C#"
@@ -116,6 +112,18 @@
116112
{
117113
"lang": "ti83b",
118114
"name": "TI-83 Basic"
115+
},
116+
{
117+
"lang": "lua",
118+
"name": "Lua"
119+
},
120+
{
121+
"lang": "crystal",
122+
"name": "Crystal"
123+
},
124+
{
125+
"lang": "php",
126+
"name": "PHP"
119127
}
120128
]
121129
}

contents/bogo_sort/bogo_sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ In code, it looks something like this:
3636
{% sample lang="rs" %}
3737
[import:16-20, lang:"rust"](code/rust/bogosort.rs)
3838
{% sample lang="swift" %}
39-
[import:32-39, lang:"swift"](code/swift/bogosort.swift)
39+
[import:25-31, lang:"swift"](code/swift/bogosort.swift)
4040
{% endmethod %}
4141

4242
That's it.

contents/bogo_sort/code/swift/bogosort.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
import Foundation
22

3-
43
func isSorted(inputArray: [Int]) -> Bool {
5-
64
for i in 0..<inputArray.count-1 {
75
if inputArray[i] > inputArray[i+1] {
86
return false
97
}
108
}
11-
9+
1210
return true
1311
}
1412

15-
16-
1713
func shuffle(inputArray: inout [Int]) -> [Int] {
18-
1914
var shuffledArray = [Int]()
2015

2116
for _ in 0..<inputArray.count {
2217
let rand = Int(arc4random_uniform(UInt32(inputArray.count)))
2318
shuffledArray.append(inputArray[rand])
2419
inputArray.remove(at: rand)
2520
}
26-
21+
2722
return shuffledArray
2823
}
2924

30-
31-
3225
func bogoSort(sortArray: inout [Int]) -> [Int] {
33-
3426
while(!isSorted(inputArray: sortArray)) {
3527
sortArray = shuffle(inputArray: &sortArray)
3628
}
37-
29+
3830
return sortArray
3931
}
4032

contents/bubble_sort/bubble_sort.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
2525
{% sample lang="hs" %}
2626
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
2727
{% sample lang="cpp" %}
28-
[import:33-56, lang:"c_cpp"](code/c++/bubblesort.cpp)
28+
[import:13-23, lang:"c_cpp"](code/c++/bubblesort.cpp)
2929
{% sample lang="rs" %}
3030
[import:6-16, lang:"rust"](code/rust/bubble_sort.rs)
3131
{% sample lang="d" %}
@@ -35,9 +35,15 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
3535
{% sample lang="racket" %}
3636
[import:6-19, lang:"racket"](code/racket/bubbleSort.rkt)
3737
{% sample lang="swift" %}
38-
[import:1-12, lang:"swift"](code/swift/bubblesort.swift)
38+
[import:1-13, lang:"swift"](code/swift/bubblesort.swift)
3939
{% sample lang="ti83b" %}
4040
[import:2-13, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
41+
{% sample lang="ruby" %}
42+
[import:3-13, lang:"ruby"](code/ruby/bubble.rb)
43+
{% sample lang="crystal" %}
44+
[import:1-11, lang:"crystal"](code/crystal/bubble.cr)
45+
{% sample lang="php" %}
46+
[import:3-15, lang:"php"](code/php/bubble_sort.php)
4147
{% endmethod %}
4248

4349
... And that's it for the simplest bubble sort method.
@@ -81,6 +87,12 @@ Program.cs
8187
[import, lang:"swift"](code/swift/bubblesort.swift)
8288
{% sample lang="ti83b" %}
8389
[import, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
90+
{% sample lang="ruby" %}
91+
[import, lang:ruby"](code/ruby/bubble.rb)
92+
{% sample lang="crystal" %}
93+
[import, lang:"crystal"](code/crystal/bubble.cr)
94+
{% sample lang="php" %}
95+
[import, lang:"php"](code/php/bubble_sort.php)
8496
{% endmethod %}
8597

8698
<script>

contents/bubble_sort/code/c++/bubblesort.cpp

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,34 @@
22
#include <cstddef>
33
#include <iostream>
44
#include <iterator>
5-
#include <random>
6-
7-
using std::begin;
8-
using std::end;
9-
10-
template <class Rng>
11-
std::vector<float> generate_input(std::size_t size, Rng& rng) {
12-
auto dist = std::uniform_real_distribution<>(0.0, 1.0);
13-
14-
auto ret = std::vector<float>();
15-
std::generate_n(std::back_inserter(ret), size,
16-
[&rng, &dist] { return dist(rng); });
17-
18-
return ret;
19-
}
205

216
template <class Iter>
22-
void print_range(std::ostream& os, Iter const first, Iter const last) {
23-
os << '{';
24-
25-
if (first != last) {
26-
os << *first;
27-
std::for_each(first + 1, last, [&os] (double d) { os << ", " << d; });
28-
}
29-
30-
os << "}\n";
7+
void print_range(Iter first, Iter last) {
8+
for (auto it = first; it != last; ++it)
9+
std::cout << *it << " ";
10+
std::cout << std::endl;
3111
}
3212

3313
template <class Iter>
34-
void bubble_sort(Iter const first, Iter const last) {
35-
if (first == last) {
36-
// the empty range is vacuously sorted
37-
return;
38-
}
39-
40-
for (;;) {
41-
bool is_sorted = true;
42-
43-
for (auto it = first; it + 1 != last; ++it) {
14+
void bubble_sort(Iter first, Iter last) {
15+
for (auto it1 = first; it1 != last; ++it1) {
16+
for (auto it2 = first; it2 + 1 != last; ++it2) {
4417
// these are unsorted! gotta swap 'em
45-
if (*(it + 1) < *it) {
46-
using std::swap;
47-
swap(*it, *(it + 1));
48-
is_sorted = false;
18+
if (*(it2 + 1) < *it2) {
19+
std::iter_swap(it2, it2 + 1);
4920
}
5021
}
51-
52-
if (is_sorted) {
53-
break;
54-
}
5522
}
5623
}
5724

5825
int main() {
59-
std::random_device random_device;
60-
auto rng = std::mt19937(random_device());
61-
62-
auto input = generate_input(10, rng);
26+
int input[] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
6327

64-
std::cout << "before sorting:\n";
65-
print_range(std::cout, begin(input), end(input));
28+
std::cout << "Unsorted array:\n";
29+
print_range(std::begin(input), std::end(input));
6630

67-
bubble_sort(begin(input), end(input));
31+
bubble_sort(std::begin(input), std::end(input));
6832

69-
std::cout << "\nafter sorting:\n";
70-
print_range(std::cout, begin(input), end(input));
33+
std::cout << "\nSorted array:\n";
34+
print_range(std::begin(input), std::end(input));
7135
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def bubble_sort(arr)
2+
arr = arr.dup
3+
(0 ... arr.size).each do
4+
(0 ... arr.size-1).each do |k|
5+
if arr[k] > arr[k+1]
6+
arr[k+1],arr[k] = arr[k],arr[k+1]
7+
end
8+
end
9+
end
10+
arr
11+
end
12+
13+
def main
14+
number = 10.times.map{rand(0..1_000)}.to_a
15+
pp "The array before sorting is #{number}"
16+
number = bubble_sort number
17+
pp "The array after sorting is #{number}"
18+
end
19+
20+
main
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
function bubble_sort(array $arr): array
4+
{
5+
for ($i = 0, $length = count($arr); $i < $length; $i++) {
6+
for ($j = 1; $j < $length; $j++) {
7+
if ($arr[$j - 1] > $arr[$j]) {
8+
$tmp = $arr[$j - 1];
9+
$arr[$j - 1] = $arr[$j];
10+
$arr[$j] = $tmp;
11+
}
12+
}
13+
}
14+
return $arr;
15+
}
16+
17+
$unsorted = [1, 2, 6, 47, 4, 9, 3, 7, 8, 23, 15];
18+
$bubble_sorted = bubble_sort($unsorted);
19+
20+
echo sprintf('Unsorted: %s%s', implode(',', $unsorted), PHP_EOL);
21+
echo sprintf('Sorted: %s%s', implode(',', $bubble_sorted), PHP_EOL);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env ruby
2+
3+
def bubble_sort(arr)
4+
(0..arr.length - 1).each do
5+
(0..arr.length - 2).each do |k|
6+
if arr[k] > arr[k + 1]
7+
arr[k + 1], arr[k] = arr[k], arr[k + 1]
8+
end
9+
end
10+
end
11+
12+
return arr
13+
end
14+
15+
def main
16+
range = [200, 79, 69, 45, 32, 5, 15, 88, 620, 125]
17+
puts "The range before sorting is #{range}"
18+
range = bubble_sort(range)
19+
puts "The range after sorting is #{range}"
20+
end
21+
22+
main()

contents/bubble_sort/code/swift/bubblesort.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
func bubbleSort(sortArray: inout [Int]) -> [Int] {
2-
32
for i in (1..<sortArray.count).reversed() {
43
for j in 0..<i {
54
if sortArray[j] > sortArray[j+1] {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function euclidSub (a, b)
2+
local a = math.abs(a)
3+
local b = math.abs(b)
4+
5+
while a ~= b do
6+
if a > b then
7+
a = a-b
8+
else
9+
b = b-a
10+
end
11+
end
12+
13+
return a
14+
end
15+
16+
function euclidMod (a, b)
17+
local a = math.abs(a)
18+
local b = math.abs(b)
19+
20+
while b ~= 0 do
21+
a, b = b, a%b
22+
end
23+
24+
return a
25+
end
26+
27+
function main ()
28+
print(euclidSub(128 * 12, 128 * 77))
29+
print(euclidMod(64 * 67, 64 * 81))
30+
end
31+
32+
main()

contents/euclidean_algorithm/code/pseudo/euclidean.pseudo

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)