To make it a pure function don't you need a deep copy, not a shallow copy as you've said? EDIT: it seems primitive data types are copied by value not reference when using slice, whereas objects are copied by reference and therefore would stop this from being a pure function!
11:03, I was wondering how you are making changes to the arr variable when you're not specifying it... ? Specifically you're doing [arr[x], arr[x]] = [arr[x], arr[e]]. Aren't you supposed to reassign at least?
Hello Kim, Hope you are doing great. I have understood the selection sort and I have managed to make it work by setting the min variable to the index rather than the actual value, however, I do not understand why the alternative approach which is what I came up with on my first try, does not work. If you have the time, please point out the mistake causing the following code to not work for selection sorting: function selectionSort(array) { for(var i = 0; i < array.length; i++){ var min = array[i]; for(var j = i+1; j < array.length; j++){ if(array[j] < min){ min = array[j]; } }
Even though it's been a year I will answer you are setting min to array[i] (array[i] = min;) and thats why you get wrong index, just add one more temporary variable after var temp. Something like const minIndex = array.indexOf(min);
@@rokassimkus2397 thanks a lot for the help :) it's funny how quickly a year has passed. I was watching this when I was getting started as a self taught dev. I'm working full time as full stack js dev today 😁👍
@@hebanano I'm glad you found it motivating. Good luck on your journey and don't forget these two key things that I learnt: -keep building something/avoid tutorial hell -dont be afraid to apply for job (if that is your goal). Don't think your "not good enough yet"
Thank you for this awesome tutorial, Justin!!
Good explanation, thank you
I wrote it this way, way less variables.
selectionSort = (array) => {
if(array.length === 0) return;
if(array.length === 1) return array;
for(let i=0; i < array.length; i++){
for(let j = i + 1; j < array.length;j++){
if(array[i] > array[j]){
[array[i],[array[j]]] = [array[j],[array[i]]]
}
}
}
return array;
}
console.log(selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]))
Thank you!
Thank you!!!!
💯
Great explanation
Love this content !
thank you!! i really like your video~~~~~~!!!!~!~!~
Great
Please don't stop uploading
To make it a pure function don't you need a deep copy, not a shallow copy as you've said?
EDIT: it seems primitive data types are copied by value not reference when using slice, whereas objects are copied by reference and therefore would stop this from being a pure function!
11:03, I was wondering how you are making changes to the arr variable when you're not specifying it... ? Specifically you're doing [arr[x], arr[x]] = [arr[x], arr[e]]. Aren't you supposed to reassign at least?
That's JavaScript array assignment, you can even assign variables similarly.
Hello Kim,
Hope you are doing great. I have understood the selection sort and I have managed to make it work by setting the min variable to the index rather than the actual value, however, I do not understand why the alternative approach which is what I came up with on my first try, does not work. If you have the time, please point out the mistake causing the following code to not work for selection sorting:
function selectionSort(array) {
for(var i = 0; i < array.length; i++){
var min = array[i];
for(var j = i+1; j < array.length; j++){
if(array[j] < min){
min = array[j];
}
}
var temp = array[i];
array[i] = min;
array[array.indexOf(min)] = temp;
}
return array;
}
console.log(selectionSort([5,2,6,1,10]));
Even though it's been a year I will answer you are setting min to array[i] (array[i] = min;) and thats why you get wrong index, just add one more temporary variable after var temp. Something like const minIndex = array.indexOf(min);
@@rokassimkus2397 thanks a lot for the help :) it's funny how quickly a year has passed. I was watching this when I was getting started as a self taught dev. I'm working full time as full stack js dev today 😁👍
@@nonipaify I'm self learning too, came across this channel today to learn Data Structures and Algorithms. Your comments is so motivating.
@@hebanano I'm glad you found it motivating. Good luck on your journey and don't forget these two key things that I learnt:
-keep building something/avoid tutorial hell
-dont be afraid to apply for job (if that is your goal). Don't think your "not good enough yet"
heyy, what does j++/i++ stand for? ty for your answer
You are incrementing those variables by 1. I suggest checking out intro to JavaScript tutorials on the “for” loops!
Example : j++ is short hand for j=j+1
terrible teaching, didnt learn anything