How to improve selection sort? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is more of an academic/homework question?
Would it be better to change
if (index_outer !== index_min) {
$P.swap(arr, index_outer, index_min);
}
to
$P.swap(arr, index_outer, index_min);
and always swap, as this is the special case when index_outer does have the smallest value? It would be a swap that does nothing, but at the same time it would not break anything. Because this does not happen often I suppose, it would reduce the amount of times an if check was used.
$P.swap = function (arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
};
$P.selectionSort = function (arr) {
var index_outer,
index_inner,
index_min,
length = arr.length;
for (index_outer = 0; index_outer < length; index_outer++) {
index_min = index_outer;
for (index_inner = index_outer + 1; index_inner < length; index_inner++) {
if (arr[index_inner] < arr[index_min]) {
index_min = index_inner;
}
}
if (index_outer !== index_min) {
$P.swap(arr, index_outer, index_min);
}
}
return arr;
};

I don't think it would always be a good idea. What if array was partially/fully sorted, you would be wasting a call to $P.swap().
As for improving selection sort try sorting the array from both ends simultaneously by taking index_min and index_max. Though number of comparisons would remain same, number of passes would decrease hence decreasing total run time.

Related

How do I get results? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am a beginner in JavaScript and I write the following code, I also write console.log(), and I do not get results, can someone help me and explain to me why I can't get results?
function integers() {
let MyArray;
MyArray = [];
for (let i = 2; i <= 10; i++) {
MyArray.push(i);
}
console.log(MyArray);
}
The javascript functions are not being called automatically. If you need to do so, You can create an auto call function - https://stackoverflow.com/a/10704006/7078456
Or you can manually call your function to get the output of your function.
function integers() {
let myArray = [];
for (let i = 2; i <= 10; i++) {
myArray.push(i);
}
console.log(myArray);
}
integers()

Use array length to push items in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an items array with specific length depending on how many items in an order there are.
I have to calculate the packaging for it, for every 2 items I use the large box and the single item goes in a small box.
So if there are 5 items its 2 large boxes and 1 small box.
How do I do this using the array length? or any other way?
Use the modulo operator:
let numBigBoxes = 0;
let numSmallBoxes = 0;
if (items.length === 1) {
numSmallBoxes = 1;
} else if (items.length % 2 === 0) {
numBigBoxes = items.length / 2;
} else {
numBigBoxes = (items.length - 1) / 2;
numSmallBoxes = 1;
}

Increment all values of [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have that variable. var count_br = "4,7,10"
var count_ = count_br.split(",");
var i;
for (i = 0; i < count_.length; ++i) {
/// Increment here each values of count_br
// I want to get 8,11,14
//for the next loop I want to get 12,15,18
//etc
}
Each time if i > 0 I want to increment by 4 all the values of count_br.
How could i do to increment by 4 all the values .
thanks.
var count_br = "4,7,10";
var count_ = count_br.split(",");
var i;
for (i = 0; i < count_.length; ++i) {
for(let idx in count_) count_[idx] = (parseInt(count_[idx])+4).toString()
}
console.log(count_)
you should take the length of the variable... in this case 4... less 1 is 3 and use it on a for to go trougth all the positions of you array and for each iteration you add 4

How to declare a json object or array collection with variable size in javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create an array or json that can be filled when detects the counter like an auxiliarJson with variable size but i dont know how can i do it
TypeError: lAttrsPorDia is undefined
lAttrsPorDia[j] = __oATTRS[i];
var lAttrsPorDia;
var j = 0;
for (var i = 0; i < __oATTRS.length; i++) {
if (__oATTRS[i].Dia == counter) {
lAttrsPorDia[j] = __oATTRS[i];
j++;
alert(JSON.stringify(lAttrsPorDia));
}
}
JavaScript arrays already do have variable size:
var arr = [];
arr.push('Hello');

Sum of elements, differences between code [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have two pieces of code both calculating the sum of the elements of an array:
var sum = array.reduce(function(previousValue, currentValue) {
return previousValue + currentValue;
}, 0);
or
var sum = 0;
array.forEach(function(e) {
sum += e;
});
Are there any differences between them beside different implementations? When is it better to use which one?
Besides your personal style preference, there could be also difference in actual performance. Those two look to perform similarly however.
If you're doing this operation a lot (or for large arrays) consider using the third way:
var sum = 0;
for (l = array.length; l--; ) { sum += array[l]; }
This will be way faster. Check this performance test for actual results.
Note: you will gain some speed if you cache array length. So instead of doing this:
for (var i = 0; i < array.length; i++) {...}
Do this:
var l = array.length;
for (; l--; ) { ... }
or this:
for (l = array.length; l--;) { ... }
First one is slightly heavier than the second one.
The fastest way is to avoid calling functions for each step and use loops like for.
var sum = 0;
for(var i=0, len=array.length; i<len; i++){
sum += array[i];
}
as using both of these function involves executing the callback function for each element that would incur the function calling overhead (so both re not efficent), using loop will give better performance.

Categories