mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Merge 7881f4f67d
into ca3d16dcce
This commit is contained in:
commit
d1d8cd6942
Binary file not shown.
51
src/algorithms/sorting/Combosort.js
Normal file
51
src/algorithms/sorting/Combosort.js
Normal file
@ -0,0 +1,51 @@
|
||||
function combsort(arr)
|
||||
|
||||
{
|
||||
|
||||
function is_array_sorted(arr) {
|
||||
|
||||
var sorted = true;
|
||||
|
||||
for (var i = 0; i < arr.length - 1; i++) {
|
||||
|
||||
if (arr[i] > arr[i + 1]) {
|
||||
|
||||
sorted = false;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return sorted;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
var iteration_count = 0;
|
||||
|
||||
var gap = arr.length - 2;
|
||||
|
||||
var decrease_factor = 1.25;
|
||||
|
||||
|
||||
|
||||
// Repeat iterations Until array is not sorted
|
||||
|
||||
|
||||
|
||||
|
||||
while (!is_array_sorted(arr))
|
||||
|
||||
{
|
||||
|
||||
// If not first gap Calculate gap
|
||||
|
||||
if (iteration_count > 0)
|
||||
|
||||
gap = (gap == 1) ? gap : Math.floor(gap / decrease_factor);
|
||||
|
||||
|
||||
|
51
src/algorithms/statistics/weighted-random/MarkovChain. Js
Normal file
51
src/algorithms/statistics/weighted-random/MarkovChain. Js
Normal file
@ -0,0 +1,51 @@
|
||||
function markovChainGenerator(text) {
|
||||
|
||||
const textArr = text.split(' ')
|
||||
|
||||
const markovChain = {};
|
||||
|
||||
for (let i = 0; i < textArr.length; i++)
|
||||
|
||||
{
|
||||
|
||||
let word = textArr[i].toLowerCase().replace(/[\W_]/, "")
|
||||
|
||||
if (!markovChain[word])
|
||||
|
||||
{
|
||||
|
||||
markovChain[word] = []
|
||||
|
||||
}
|
||||
|
||||
if (textArr[i + 1])
|
||||
|
||||
{
|
||||
|
||||
markovChain[word].push(textArr[i +1].toLowerCase().replace(/[\W_]/, ""));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return markovChain
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function likelyhood(chain, prev, word)
|
||||
|
||||
{
|
||||
|
||||
const prevArr = chain[prev]
|
||||
|
||||
const num = prevArr.filter
|
||||
|
||||
(w => w === word).length
|
||||
|
||||
return `The likelihood that ${word} will come after ${prev} is ${num} out of ${prevArr.length}`}
|
||||
|
Loading…
Reference in New Issue
Block a user