Javascript Freecodecamp Algorithm #32: Implement Selection Sort

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024

Комментарии • 23

  • @GraceandWisdom
    @GraceandWisdom 2 года назад +1

    Thank you for this awesome tutorial, Justin!!

  • @masikajenniffer6473
    @masikajenniffer6473 Год назад +1

    Good explanation, thank you

  • @adamdreier
    @adamdreier 3 года назад +1

    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]))

  • @andreluizmontanari7886
    @andreluizmontanari7886 2 года назад +1

    Thank you!

  • @franciscogarrido9742
    @franciscogarrido9742 2 года назад +1

    Thank you!!!!

  • @cmdv42
    @cmdv42 2 года назад

    💯

  • @hangor3620
    @hangor3620 2 года назад

    Great explanation

  • @julienaz2274
    @julienaz2274 4 года назад

    Love this content !

  • @user-gx6mo6nj5i
    @user-gx6mo6nj5i 3 года назад

    thank you!! i really like your video~~~~~~!!!!~!~!~

  • @cinquain0
    @cinquain0 2 года назад

    Great

  • @hellofriend3091
    @hellofriend3091 3 года назад

    Please don't stop uploading

  • @kevin-johar
    @kevin-johar 2 года назад

    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!

  • @wonjaehwang7670
    @wonjaehwang7670 3 года назад

    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?

    • @rutwickgangurde3247
      @rutwickgangurde3247 2 года назад

      That's JavaScript array assignment, you can even assign variables similarly.

  • @nonipaify
    @nonipaify 4 года назад

    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]));

    • @rokassimkus2397
      @rokassimkus2397 2 года назад

      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);

    • @nonipaify
      @nonipaify 2 года назад +2

      @@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
      @hebanano 2 года назад

      @@nonipaify I'm self learning too, came across this channel today to learn Data Structures and Algorithms. Your comments is so motivating.

    • @nonipaify
      @nonipaify 2 года назад +1

      @@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"

  • @322fra5
    @322fra5 4 года назад

    heyy, what does j++/i++ stand for? ty for your answer

    • @AngleCoding
      @AngleCoding  4 года назад

      You are incrementing those variables by 1. I suggest checking out intro to JavaScript tutorials on the “for” loops!

    • @navjotsingh2251
      @navjotsingh2251 2 года назад

      Example : j++ is short hand for j=j+1

  • @lolonoob9874
    @lolonoob9874 2 года назад

    terrible teaching, didnt learn anything