Selection Sort | C++ Example

Sdílet
Vložit
  • čas přidán 12. 02. 2023
  • An example of implementing the selection sort algorithm in C++. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Komentáře • 25

  • @rafa-bd7es
    @rafa-bd7es Před 2 měsíci

    I'm Brazilian, and I'm here to thank you. In my language (Portuguese), I couldn't find an explanation for this topic. But on your channel, I found the whole matter

  • @Dmitri_17
    @Dmitri_17 Před rokem +3

    Hey! thank you so much for your work, I've finally understand how selection sort works!)

  • @Phaytann
    @Phaytann Před rokem +3

    This was a great explanation, thank you!

  • @hannah8466
    @hannah8466 Před rokem

    Thank you so much for these videos

  • @SID-TV_MW
    @SID-TV_MW Před 9 měsíci

    Well explained

  • @WinwinDesu
    @WinwinDesu Před 13 dny

    Wakoy nasabtan🎉

  • @markopolo2224
    @markopolo2224 Před rokem

    very helpful!

  • @MUHAMMADRASHID-fj4gi
    @MUHAMMADRASHID-fj4gi Před rokem

    Nice explanation sir👍

  • @vanthomias5538
    @vanthomias5538 Před 6 dny

    Is it better for performance if I use the " if (min_pos != i)" ?
    Or is it better for performances if I dont?

  • @tfac277
    @tfac277 Před rokem +2

    i dont know if my opinion is wrong but i think in the swap step, we don't really need to add more if statement , we can write it imediately after min_pos = j ( in the same if statement) because when min_pos = j , it's mean min_pos absolutely different from i, and we need swap when min_pos = j ( sorry for my bad english)

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +4

      I'm not sure if I understand your comment correctly or not. But in some sense that last if condition is optional, yes. We could take it out and just do the swap. And if min_pos == i then the algorithm will still work, we will just be (sometimes possibly) swapping an element with itself. The reason we don't do this is that swaps/assignments are much more "expensive" operations for the CPU to do than comparisons. It's more efficient to compare that min_pos != i before doing the swap than to just do the swap every time without checking, because comparisons are "fast" for the CPU to do. So you'll find all the sources online will implement the algorithm in this way. Hopefully this helps! :-)

    • @tfac277
      @tfac277 Před rokem

      @@PortfolioCourses Thanks! it's help me so much. But i think, when we use more if statement, it'll be Comparison + assignment operation ( cause it's If+ Swap ), but when we swap in the same if statement, i'll be just assignment. I think We must swap when min_pos = j so we don't need comparison. Sorry if i may have made some mistakes

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      I'm not sure I understand, but I can say that we don't always "need" to swap, when min_pos == i, we don't need to swap because we are just swapping the element with itself at that point.

    • @tfac277
      @tfac277 Před rokem

      @@PortfolioCourses oh. Thank you so much

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      You're welcome! :-)

  • @bkly_7772
    @bkly_7772 Před rokem +1

    Hey portfolio, I have an assignment whre i have to take peoples names and salary from a .txt file and orientate it as first name, last name, and their new salary increase by what ever percent increase it is and put it into a new file. Im having trouble figuring out how to use a while loop so i dont have to go one by one inputting each name and each salary increase. If you have any previous videos or know anywhere that could help me with this problem I would greatly appreciate it. Thanks!!!

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      I have a bunch of C++ file access videos each with related code that might give you some ideas: www.youtube.com/@PortfolioCourses/search?query=C%2B%2B%20file. Some of those examples include using a loop to read each line of the file... you could take the number, multiple it by the percentage increase, and write it to the other file in the loop body (if I'm understanding the problem correctly). But I don't have a solution to this specific problem or know where you might find one. Good luck on your assignment! :-)

    • @bkly_7772
      @bkly_7772 Před rokem

      @Portfolio Courses thank you you really are a blessing 🙌 🙏

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      You're welcome! :-)

  • @sammyngwenya1199
    @sammyngwenya1199 Před rokem +1

    can you please start commenting your final code as well

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +2

      I don't comment the code in the videos because that would make the videos too long, but if you click the link in the description of the videos the code is commented on GitHub where it is stored: github.com/portfoliocourses/cplusplus-example-code/blob/main/selection_sort.cpp. :-)