Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

A star/Marcovchain/Combosort java script algorithm #1054

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Binary file not shown.
51 changes: 51 additions & 0 deletions src/algorithms/sorting/Combosort.js
Original file line number Diff line number Diff line change
@@ -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 changes: 51 additions & 0 deletions src/algorithms/statistics/weighted-random/MarkovChain. Js
Original file line number Diff line number Diff line change
@@ -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}`}