Published
Edited
Mar 19, 2019
Insert cell
Insert cell
Insert cell
function findMin (n, inputArray) {
var sortedArray = [];
let iter = 0;
let maxIndex = 0;
const inputArrayLength = inputArray.length;
// Copy first n values to the sorted array
while (iter < n) {
sortedArray.push(inputArray[iter]);
if (inputArray[maxIndex] < inputArray[iter]) {
maxIndex = iter
}
iter++
}
while (iter < inputArrayLength) {
if (inputArray[iter] < sortedArray[maxIndex]) {
sortedArray[maxIndex] = inputArray[iter]
// Find the new max in the sorted array
for (var i = 0; i < n; i++) {
if (sortedArray[i] > sortedArray[maxIndex]) {
maxIndex = i
}
}
}
iter++
}
return sortedArray[maxIndex]
}
Insert cell
findMin(3, [0,7,8,1,2,6]);

Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

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