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!
Thank you for this, was trying to wrap my head around this sorting lol
I need to dip my head in ice
You're welcome Olamide! :-D
Wow this is a great explanation! Thank you!
You're welcome! 😀
Thank you sir 🙏❤️
Excellent explanation sirrr
I’m glad you enjoyed the explanation Razi! :-)
Very nice explanation sir🙏
Thank you! :-)
thank you! :)
You're welcome! :-)
What if there are 4 integers can you do it pls
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
// Online C compiler to run C program online
#include
int sor(int arr[],int n){
int temp,i,j;
for(int i=0;i
How about negative numbers?
This code should work with negative numbers too. :-)
Thank you very much ... So what if i want descending order only sorting 3 numbers again
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).
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
@@trevormotsi8561 The code is available here, it will work: github.com/portfoliocourses/c-example-code/blob/main/sort_three_numbers.c
How can someone write this in pseudocode please?
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.
@@PortfolioCourses thanks
@@Atrainn234 You're welcome! 😀
how can I sort 5 numbers?
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.
@@PortfolioCourses I am looking for a video that is not using an Array while sorting 5 numbers.
@@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.
Wow this is a great explanation! Thank you!
You’re welcome! :-)