Sort Three Numbers | C Programming Example

Поделиться
HTML-код
  • Опубликовано: 11 сен 2024
  • Example of sorting three numbers using C, in both ascending and descending order. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

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

  • @olamidecooldude
    @olamidecooldude 2 года назад +3

    Thank you for this, was trying to wrap my head around this sorting lol
    I need to dip my head in ice

  • @tanjinai8211
    @tanjinai8211 2 года назад +3

    Wow this is a great explanation! Thank you!

  • @youssefahmed-io1ko
    @youssefahmed-io1ko 17 дней назад

    Thank you sir 🙏❤️

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

    Excellent explanation sirrr

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

    Very nice explanation sir🙏

  • @morisra12
    @morisra12 Год назад

    thank you! :)

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

    What if there are 4 integers can you do it pls

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +5

      The program would start to become very, very large if we keep doing it this way. Even with only 4 numbers, if we were to keep using this approach, we would need another "outer if-statement" that would then contain the code to sort the remaining 3 integers in each branch (about the same size as the code in this example, but repeated 4x). Because the problem becomes too big to solve effectively this way, at this point we would use an array of data and what's called a sorting algorithm to sort the data. One sorting algorithm that people often learned first is called bubble sort, I've made a video on that one: ruclips.net/video/YqzNgaFQEh8/видео.html As well as on selection sort, which is a different way of sorting numbers in an array: ruclips.net/video/YepJ7fDmyjI/видео.html

    • @prathamreigns2095
      @prathamreigns2095 5 месяцев назад

      // Online C compiler to run C program online
      #include
      int sor(int arr[],int n){
      int temp,i,j;
      for(int i=0;i

  • @b_jamorolluismiguelr3781
    @b_jamorolluismiguelr3781 Год назад

    How about negative numbers?

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      This code should work with negative numbers too. :-)

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

    Thank you very much ... So what if i want descending order only sorting 3 numbers again

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

      Great question Trevor! 🙂 The numbers are printed out in descending order at the end of the program right now. So you could just do it that way... by sorting them 'ascending' we've also figured out their order in 'descending' order too... it's just that we swap highest and lowest. You could also go through the code and change all the = in the conditions, that would sort 3 numbers in descending order (but you would want to swap the variable names of 'lowest' to 'highest' too in order to make the code make sense).

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

      I've been trying to re-do the same program there that u just did the same way but im having an error on the first *else* I don't know if im making a mistake myself or there's something wrong

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

      @@trevormotsi8561 The code is available here, it will work: github.com/portfoliocourses/c-example-code/blob/main/sort_three_numbers.c

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

    How can someone write this in pseudocode please?

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

      Pseudocode has no official rules, it's generally speaking a lot like regular code but with less focus on following specific syntax and more flexibility to write whatever makes sense: en.wikipedia.org/wiki/Pseudocode#Syntax. So a pseudocode version of this would be very, very similar to this code, but the syntax would not need to be nearly as precise... brackets, semicolons, even types, etc, could all be left out.

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

      @@PortfolioCourses thanks

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

      @@Atrainn234 You're welcome! 😀

  • @nideareyanne5340
    @nideareyanne5340 Год назад

    how can I sort 5 numbers?

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

      Great question Nidea! :-) If you have 5 numbers, you might want to use something like Insertion Sort: ruclips.net/video/Tz7vBodZqo8/видео.html. Or Selection Sort: ruclips.net/video/YepJ7fDmyjI/видео.html. You could technically implement something like in this video, but you would have more and more nested if-statements with more and more branches and it would start to get pretty complicated, so I would suggest using a sorting algorithm instead.

    • @nideareyanne5340
      @nideareyanne5340 Год назад

      @@PortfolioCourses I am looking for a video that is not using an Array while sorting 5 numbers.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      @@nideareyanne5340 That is a difficult question, I don't really know of any easy way to do it or any videos that can help. 😞 The answers I've seen online aren't very easy or 'elegant', like this one: stackoverflow.com/a/64016787. Or this one: stackoverflow.com/a/25070688.

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

    Wow this is a great explanation! Thank you!