Published
Edited
Jun 29, 2019
1 fork
Importers
2 stars
Insert cell
Insert cell
Insert cell
function factorial(number) {
return number < 2 ? 1 : number * factorial(number - 1)
}
Insert cell
Insert cell
function sockMerchant(n, ar) {
const colors = {}
let matches = 0
for (let i = 0; i < n; i++) {
if (colors[ar[i]]) {
matches++;
colors[ar[i]] = 0
} else {
colors[ar[i]] = 1
}
}
return matches;

}
Insert cell
sockMerchant(9,
[10, 20, 20, 10, 10, 20, 50, 10, 20])
Insert cell
factorial(6)
Insert cell
Insert cell
function _fibonacci(element) {
const series = [1, 1];

for (let i = 2; i < element; i++) {
const a = series[i - 1];
const b = series[i - 2];
series.push(a + b);
}

return series[element - 1];
}

Insert cell
_fibonacci(15)
Insert cell
_fibonacci(3)
Insert cell
_fibonacci(4)
Insert cell
_fibonacci(5)
Insert cell
Insert cell
Insert cell
function bubbleSort(array) {
let swapped;
do {
swapped = false;
array.forEach((number, index) => {
if (number > array[index + 1]) {
[array[index], array[index + 1]] = [array[index + 1], array[index]];
swapped = true;
}
});
} while (swapped);
return array;
}
Insert cell
bubbleSort([1, 3, 6, 83, 29, 20, 11, 10, 13])
Insert cell
Insert cell
Insert cell
function insertionSort(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < i; j++) {
if (array[i] < array[j]) array.splice(j, 0, array.splice(i, 1)[0]);
}
}
return array;
}
Insert cell
insertionSort([1, 3, 6, 83, 29, 20, 11, 10, 13])
Insert cell
Insert cell
Insert cell
function selectionSort(array) {
for (let i = 0; i < array.length; i++) {
let indexOfMin = i;

for (let j = i + 1; j < array.length; j++)
if (array[j] < array[indexOfMin]) indexOfMin = j;

if (indexOfMin !== i) {
let less = array[indexOfMin];
array[indexOfMin] = array[i];
array[i] = less;
}
}

return array;
}
Insert cell
selectionSort([1, 3, 6, 83, 29, 20, 11, 10, 13])
Insert cell
Insert cell
Insert cell
function quickSort (array){
if (array.length < 2){
return array
}

const pivot = array[array.length - 1];
const left = [],
right = []

for (let i = 0; i < array.length - 1; i++) {
if (array[i] < pivot) {
left.push(array[i])
} else {
right.push(array[i])
}
}

return [...quickSort(left), pivot, ...quickSort(right)]
}
Insert cell
quickSort([1, 3, 6, 83, 29, 20, 11, 10, 13])
Insert cell
Insert cell
Insert cell
function _merge(left, right) {
const results = []

while (left.length && right.length) {
if (left[0] < right[0]){
results.push(left.shift())
} else {
results.push(right.shift())
}
}

return [...results, ...left, ...right]
}
Insert cell
function _mergeSort(array) {
if (array.length === 1){
return array
}

const center = Math.floor(array.length / 2)
const left = array.slice(0, center)
const right = array.slice(center)

return _merge(_mergeSort(left), _mergeSort(right))
}


Insert cell
_mergeSort([1, 3, 6, 83, 29, 20, 11, 10, 13])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more