Skip to content

Commit f397df3

Browse files
authored
Merge pull request kelvins#48 from ViniciusAlberkovics/master
[JS] HeapSort, Fibonacci and readme
2 parents db1efe4 + 8818ea3 commit f397df3

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
2121
| [Exponenciação Recursiva][14] | [C/C++](/src/c/ExponenciacaoRecursiva.c) | Java | [Python](/src/python/exponenciacao_recursiva.py) | Go | [Ruby](/src/ruby/ExponenciacaoRecursiva.rb) | [Javascript](/src/javascript/ExponentiationRecursive.js) | Pascal |
2222
| [Fatorial][15] | [C/C++](/src/c/Fatorial.c) | [Java](/src/java/Fatorial.java) | [Python](/src/python/fatorial.py) | [Go](/src/go/fatorial/fatorial.go) | [Ruby](/src/ruby/Fatorial.rb) | [Javascript](/src/javascript/Factorial.js) | [Pascal](/src/pascal/fatorial.py) |
2323
| [Fatorial Recursiva][16] | [C/C++](/src/c/FatorialRecursiva.c) | [Java](/src/java/FatorialRecursiva.java) | [Python](/src/python/fatorial_recursiva.py) | Go | [Ruby](/src/ruby/Fatorial.rb) | [Javascript](/src/javascript/FactorialRecursive.js) | [Pascal](src/pascal/fatorial-recusiva.pas) |
24-
| [Fibonacci][17] | [C/C++](/src/c/Fibonacci.cpp) | [Java](/src/java/Fibonacci.java) | [Python](/src/python/fibonacci.py) | [Go](/src/go/fibonacci/fibonacci.go) | [Ruby](/src/ruby/Fibonacci.rb) | Javascript | Pascal |
24+
| [Fibonacci][17] | [C/C++](/src/c/Fibonacci.cpp) | [Java](/src/java/Fibonacci.java) | [Python](/src/python/fibonacci.py) | [Go](/src/go/fibonacci/fibonacci.go) | [Ruby](/src/ruby/Fibonacci.rb) | [Javascript](/src/javascript/Fibonacci.js) | Pascal |
2525
| [Máximo Recursivo][26] | [C/C++](/src/c/MaxRecursivo.c) | Java | Python | Go | Ruby | [Javascript](/src/javascript/MaxRecursive.js) | Pascal |
2626
| [Mínimo e Máximo Iterativo][27] | C/C++ | [Java](/src/java/MaxMinArray.java) | [Python](/src/python/min_max_iterativo.py) | Go | Ruby | [Javascript](/src/javascript/IterativeMinAndMax.js) | Pascal |
2727
| [Mínimo e Máximo Recursivo][28] | [C/C++](/src/c/MaxMinRecursivo.c) | Java | [Python](/src/python/maximo_minimo_recursivo.py) | [Go](/src/go/maximominimo/MaximoMinimo.go) | Ruby | [Javascript](/src/javascript/RecursiveMinAndMax.js) | Pascal |
@@ -55,7 +55,7 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
5555
| [Comb Sort][38] | C/C++ | Java | Python | [Go](/src/go/combsort/combsort.go) | Ruby | [Javascript](/src/javascript/CombSort.js) | Pascal |
5656
| [Counting Sort][39] | C/C++ | Java | Python | [Go](/src/go/countingsort/countingsort.go) | [Ruby](/src/ruby/count_sort.rb) | Javascript | Pascal |
5757
| [Gnome Sort][40] | C/C++ | Java | Python | [Go](/src/go/gnomesort/gnomesort.go) | Ruby | Javascript | Pascal |
58-
| [Heapsort][41] | C/C++ | [Java](/src/java/HeapSort.java) | Python | [Go](/src/go/heapsort/heap_sort.go) | [Ruby](/src/ruby/heap_sort.rb) | Javascript | [Pascal](/src/pascal/heapsort.pas) |
58+
| [Heapsort][41] | C/C++ | [Java](/src/java/HeapSort.java) | Python | [Go](/src/go/heapsort/heap_sort.go) | [Ruby](/src/ruby/heap_sort.rb) | [Javascript](/src/javascript/HeapSort.js) | [Pascal](/src/pascal/heapsort.pas) |
5959
| [Insertion Sort][42] | [C/C++](/src/c/InsertionSort.cpp) | [Java](/src/java/InsertionSort.java) | [Python](/src/python/insertion_sort_iterativo.py) | [Go](/src/go/insertionsort/insertionsort.go) | [Ruby](/src/ruby/insertion_sort.rb) | [Javascript](/src/javascript/InsertionSort.js) | Pascal |
6060
| Insertion Sort Recursivo | C/C++ | Java | [Python](/src/python/insertion_sort_recursivo.py) | Go | Ruby | Javascript | Pascal |
6161
| [Merge Sort][44] | [C/C++](/src/c/MergeSort.c) | [Java](/src/java/Mergesort.java) | Python | [Go](/src/go/mergesort/mergesort.go) | [Ruby](/src/ruby/merge_sort.rb) | [Javascript](/src/javascript/MergeSort.js) | [Pascal](/src/pascal/sort/mergesort.pas) |

src/javascript/Fibonacci.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
*
3+
* @param {number} n
4+
*/
5+
function fibonacci(n) {
6+
return n < 2 ? n : (fibonacci(n - 1) + fibonacci(n - 2));
7+
}
8+
9+
function main(){
10+
console.log('fibonacci 30 -> ', fibonacci(30))
11+
}
12+
13+
main();

src/javascript/HeapSort.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const MAX_LENGTH = 10;
2+
3+
/**
4+
* @param {number[]} vector
5+
*/
6+
function heapSort(vector){
7+
let length = vector.length;
8+
let i = parseInt(vector.length / 2);
9+
let temp, father, children;
10+
11+
while(true) {
12+
if(i > 0){
13+
i--;
14+
temp = vector[i];
15+
} else {
16+
length--;
17+
if (length <= 0) return vector;
18+
19+
temp = vector[length];
20+
vector[length] = vector[0];
21+
}
22+
23+
father = i;
24+
children = parseInt((i * 2) + 1);
25+
26+
while(children < length) {
27+
if (children + 1 < length && vector[children + 1] > vector[children])
28+
children++;
29+
30+
if (vector[children] > temp) {
31+
vector[father] = vector[children];
32+
father = children;
33+
children = parseInt(father * 2 + 1);
34+
} else break;
35+
}
36+
37+
vector[father] = temp;
38+
}
39+
}
40+
41+
function main(){
42+
let vector = [];
43+
let values = "[";
44+
for (let index = 0; index < MAX_LENGTH; index++) {
45+
let value = Math.floor(Math.random() * 99); // 0 - 99
46+
vector.push(value);
47+
values += `${value}, `;
48+
}
49+
values += "]"
50+
51+
console.log("Initial -> ", values);
52+
53+
console.log("Final Vector")
54+
console.table(heapSort(vector));
55+
}
56+
57+
main();

0 commit comments

Comments
 (0)