I have array like this
value = [250, 200, 300, 150, 300]
I use this code.
for (var j = 0; j < value.length - 1; j += 1)
{
if (value[j] > value[j + 1])
{
var temp = value[j + 1];
value[j + 1] = value[j];
value[j] = temp;
}
}
But,it's not working. It's results value = [200, 250, 150, 300, 300]
I want to acheive this without using inbuilt function.
use the below code.
var value = [250, 200, 300, 150, 300];
for (var i = 0; i < value.length; i++) {
var swapped = false
for (var j = 0; j < value.length; j++) {
if (value[j] > value[j + 1]) {
temp = value[j + 1];
value[j + 1] = value[j];
value[j] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
console.log(value)
var numbers = [10, 3, 5, 1, 88, 6, 12, 28, 16]
for (var i = 0; i < numbers.length; i++) {
for (var j = 0; j < numbers.length; j++) {
if (numbers[i] < numbers[j]) {
var temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
}
}
console.log(numbers);
You should have two loops one inside other to sort array
value = [250, 200, 300, 150, 300]
for (var i = 0; i < value.length; i++)
for (var j = i; j < value.length - 1; j++) {
if (value[i] > value[j]) {
var temp = value[j];
value[j] = value[i];
value[i] = temp;
}
}
console.log(value)
Related
I am writing a program to calculate euclidean distance and then display the lines based, with the below code:
function discreteFrechet(X, Y) {
var M = X.length;
var N = Y.length;
var S = [
[],
[]
];
var backpointers = [
[],
[]
];
var backpaths = [];
var idx;
var path = [];
var paths;
var back = [
[],
[]
];
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
S[i][j] = 0;
backpointers[i][j] = 0;
}
} /* populates S array */
/*sanity check*/
S[0, 0] = euclidian(X, Y, 0, 0);
opt1 = [-1, 0];
opt2 = [0, -1];
opt3 = [-1, -1];
backpaths.push(opt1);
backpaths.push(opt2);
backpaths.push(opt3);
/*backpaths populated*/
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
options = [];
if (i != 0 || j != 0) {
if (i > 0) {
options[0] = S[i - 1, j];
}
if (j > 0) {
options[1] = S[i - 1, j];
}
if (i > 0 && j > 0) {
options[2] = S[i - 1, j];
}
idx = Math.min(options);
backpointers[i][j] = idx;
S[i][j] = Math.max(options[idx], euclidian(X, Y, i, j));
}
}
}
console.log(S);
paths = [
[M - 1, N - 1]
];
path = [
[],
[]
];
path.push(paths);
//Create "path"
i = M - 1;
j = N - 1;
count = 0;
while ((path[path.length - 1][0] != 0) || (path[0][path[1].length - 1] != 0)) {
back[0][1] = backpaths[backpointers[i], [j]];
i += back[0];
j += back[1];
path.push([i, j]);
if (count > 1000) {
console.log("too many loops");
break;
}
count += 1;
}
path.push([0, 0]);
path.reverse();
//returns bottleneck and the path
}
As I am testing, I am running into a problem with an infinite while loop (hence the break statement) any help or suggestions would be greatly appreciated! The goal is to append indicies into the path element, such that I can then take those path indicies and the bottleneck and use them to plot with d3.
I want to get the sum of the matrix elements, except for those over which the number 0, and get error:
Uncaught TypeError: Cannot set property '0' of undefined
function matrixElementsSum(matrix) {
let s = 0;
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j <= matrix.length; j++) {
if (matrix[i][j] == 0) {
matrix[i+1][j] = 0;
}
s += matrix[i][j]
}
}
return s
}
console.log(matrixElementsSum([[0, 1, 2, 0],
[0, 3, 2, 1],
[2, 0, 2, 3]]))
Your loop counter i should iterate over number of rows and j should iterate over number of columns. Also before setting matrix[i+i][j] to 0, you should also check if i+1 < matrix.length (number of rows)
function matrixElementsSum(matrix) {
let s = 0;
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0 && i+1 < matrix.length) {
matrix[i+1][j] = 0;
}
s += matrix[i][j]
}
}
return s
}
Here is my solution to this problem. Hope it would help someone.
function matrixElementsSum(matrix) {
let susp = [];
return matrix.reduce((t, arr, i)=>{
return arr.reduce((tt, val, ii)=>{
if (val === 0) susp.push(ii);
if (susp.includes(ii)) {
return tt;
} else {
return tt+val;
}
}, 0) + t;
}, 0);
}
Here is my solution with Python:
def solution(matrix):
row_len = len(matrix)
col_len = len(matrix[0])
sum = 0
for c_index in range(col_len):
for r_index in range(row_len):
if matrix[r_index][c_index] == 0:
break
else:
sum += matrix[r_index][c_index]
return sum
function solution(matrix) {
let sum= 0;
for (let i = 0; i < matrix[0].length; i++) {
for (let j = 0; j < matrix.length; j++) {
if (matrix[j][i] === 0) break;
sum+= matrix[j][i];
}
}
return sum;
}
function solution(matrix) {
for(var r=0,j=0;j<matrix[0].length;j++){
for(var i=0;i<matrix.length;i++){
if(matrix[i][j]===0) break
else r+=matrix[i][j]
}
}
return r
}
It took me some time to solve this exercise. I see it was not very complicated.
function bubble(arr) {
var len = arr.length;
for (var i = 0; i < len; i++) {
for (var j = 0; j < len; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = arr[temp];
}
}
}
return arr;
}
console.log(bubble([5, 12, 2, 4, 10]));
My output is
[2, undefined, undefined, 4, 10]
I do not know why I am getting undefined instead of sorted array. I looked into some other code as well but did not work out.
You need to take temp for swapping. See comment.
function bubble(arr) {
var len = arr.length;
for (var i = 0; i < len; i++) {
for (var j = 0; j < len; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp; // take just temp
}
}
}
return arr;
}
console.log(bubble([5, 12, 2, 4, 10]));
I want to count how many times needed for an array to be sorted
var array = [4,2,3,1]
var yourCounter = 0;
for (var i = 0; i < array.length; i++) {
for (var j = 1; j < array.length-j; j++)
if (array[j - 1] > array[j]) {
yourCounter++;
} }
it will return 4 , it should be 5
but if I input array [1,2,3] will correctly return 0 , and if I input array [3,2,1] it will correctly return 3
You could take the given code and swap the values while counting.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
// Swap adjacent elements if they are in decreasing order
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
}
}
}
var array = [4, 2, 3, 1],
counter = 0,
i, j, n = array.length;
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1; j++) {
if (array[j] > array[j + 1]) {
[array[j + 1], array[j]] = [array[j], array[j + 1]];
++counter;
}
}
}
console.log(counter);
console.log(array);
I found the solution
var a = [4,2,3,1]
function sortArray(a){
let swapCount = 0;
let swapOccurred = true;
let index = 0;
while (swapOccurred == true && index < a.length){
swapOccurred == false;
if (a[index] > a[index+1]){
let holder = a[index]
a[index] = a[index+1];
a[index+1] = holder;
swapOccurred == true;
swapCount ++;
index = -1;
}
index ++
}
function countSwaps(a) {
let swapCount = 0;
[a, swapCount] = sortArray(a)
console.log(swapCount)
}
return [a, swapCount]
}
I'm trying to write a shellsort on Node.js like on the book Algorithms, 4th ed. Sedgewick, Wayne. There all the examples written on Java.
Here is my module:
"use strict";
module.exports = (function() {
function sort(array) {
let size = array.length;
let h = 1;
while(h < size/3) {
h = h * 3 + 1;
}
while(h >= 1) {
for(let i = h; i < size; i++) {
for(let j = i; j >= h && less(array, j, j-h); j = j-h) {
exch(array, j, j-h);
}
}
h = h/3;
}
}
function less(array, i, min) {
return array[i] < array[min];
}
function exch(array, i, min) {
let temp = array[i];
array[i] = array[min];
array[min] = temp;
}
return {
sort: sort
};
})();
I use mocha and chai for testing with this function:
function isSorted(array) {
for(let i = 1, size = array.length; i < size; i++) {
if (array[i] < array[i-1]) {
return false;
}
}
return true;
}
and shell sort not working: mocha test screenshot
Your h may become a floating point number if you use h = h / 3. Try h = Math.floor(h / 3); instead.
Implementation of shell sort algorithm in Javascript
function shellSort(arr){
var len = arr.length;
var gapSize = Math.floor(len/2);
while(gapSize > 0){
for(var i = gapSize; i < len; i++) {
var temp = arr[i];
var j = i;
while(j >= gapSize && arr[j - gapSize] > temp) {
arr[j] = arr[j - gapSize];
j -= gapSize;
}
arr[j] = temp;
}
gapSize = Math.floor(gapSize/2);
}
return arr;
}
function isSorted(array) {
for(let i = 1, size = array.length; i < size; i++) {
if (array[i] < array[i-1]) {
return false;
}
}
return true;
}
var randomArray = [45,86,3,5,97,95,4,24,7,88,93,27,45,90,54,89,74,5,90,73,74,857,834,8394,4231,485,32,54,674,12];
var mySortedArray = shellSort(randomArray);
console.log(mySortedArray);
console.log(isSorted(mySortedArray));
Output:
[3, 4, 5, 5, 7, 12, 24, 27, 32, 45, 45, 54, 54, 73, 74, 74, 86, 88, 89, 90, 90, 93, 95, 97, 485, 674, 834, 857, 4231, 8394]
true