Using qsort() To Sort An Array | C Programming Example

  Рет қаралды 27,752

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 65
@AfifaSadiq
@AfifaSadiq 5 ай бұрын
The compare function is so well explained.
@pratham8275
@pratham8275 7 ай бұрын
Thanks for explaining qsort
@idkzsh
@idkzsh Жыл бұрын
you explained this so much better than my prof. prof puts me to sleep.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Hahaha I'm sorry your prof puts you to sleep! I'm glad this video was able to help you out! :-)
@madmademoiselle097
@madmademoiselle097 Жыл бұрын
thank you, a clear, easy to understand tutorial!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome, I'm glad you found it easy to understand! :-)
@gammyhorse
@gammyhorse 2 жыл бұрын
So basically, if I understood correctly, the 4th argument of qsort is a function pointer returning an int, which is very interesting if this is the case. Function pointers are powerful and I would like to know more about their use in practice. Thank you.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Yes, exactly, it's a pointer to a function which returns an int. :-) You may have already seen this video on function pointers but I thought I'd share it just in case: kzbin.info/www/bejne/nJDYiIKNi86MmZI
@gammyhorse
@gammyhorse 2 жыл бұрын
@@PortfolioCourses Thank you very much!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@johngormley1394 That's very cool John, thanks for sharing, definitely a good application of function pointers. 🙂
@decumon
@decumon Жыл бұрын
thanks from russia, first video about comparator that i found
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@ongayijohnian8787
@ongayijohnian8787 Жыл бұрын
I still dont get how qsort works with the x and y, does it iterate in the loop assigning a digit x and its successor y?
@akshatkhatri1167
@akshatkhatri1167 Жыл бұрын
great video so if we want to use it with array of structures instead of integer array; how exaclty will the casting be done i m a bit confused
@PortfolioCourses
@PortfolioCourses Жыл бұрын
It’s done pretty much the same way, maybe the example in this link can help you out? :-) iq.opengenus.org/qsort-in-c/amp/
@ismbks
@ismbks 2 ай бұрын
does the compare function has to take const void pointer as parameters? or could it also take another type like const int pointer, so we don't need to use pointer magic and keep the code a bit more simple?
@philipphortnagl2486
@philipphortnagl2486 Жыл бұрын
I don't get how the compare function works without giving any arguments. I mean you say, we assume the numbers "2" and "5" from the array, but how is this actually working in the function without explicitly giving any arguments for the function?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Great question Philipp! :-) So when we pass compare to qsort() like this: qsort(a, length, sizeof(int), compare); We aren't actually *calling* compare() in that moment. We're actually passing the function itself as an argument to qsort(). And then at a later point the qsort() function will be what actually calls compare()! It will call it and provide it with the arguments to compare, and it will use the return value of the compare() function to help sort the array. We call this a "callback function": en.wikipedia.org/wiki/Callback_(computer_programming). It is a bit unusual because normally we pass values to functions, like 2.5, etc. Technically what is being passed is the memory address of the compare function, and qsort() is able to use that to call the function. In C we call that a function pointer, there is a video on the concept here: kzbin.info/www/bejne/nJDYiIKNi86MmZI.
@pousemad
@pousemad Жыл бұрын
amazing video, thank you so much!!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome, I'm very glad you enjoyed it! :-)
@kianoushmehrandish7818
@kianoushmehrandish7818 2 жыл бұрын
have you created a same video for sorting structs?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
No, but that's a great idea Kianoush, maybe I can do a video on that one day though. :-) It would be very, very similar to what we've done in this video though. The difference would be here: int x = *(int *)x_void; ...instead of a type cast using int, it would be a typecast using the struc type. So instead of 'int' it should be something like 'struct name_of_struct'.
@ishanrana8402
@ishanrana8402 2 жыл бұрын
Thank you this was very helpful
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome Ishan! :-)
@rishabhpandey12aroll7.5
@rishabhpandey12aroll7.5 9 ай бұрын
Thanks, this really helped
@demiancoorey2394
@demiancoorey2394 Жыл бұрын
First class tutorial
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed it Demian! :-)
@nguyentuan-ef5hr
@nguyentuan-ef5hr 6 ай бұрын
Hmm why when you read the array and call qsort you can know is 2-7 but not 7-2 ur because you read the array from a[0] not form the last p/s : sorry if my english is bad
@Qwerty_945
@Qwerty_945 Жыл бұрын
why not x = 7 and y = 2 ?
@varaddeshmukh1137
@varaddeshmukh1137 Жыл бұрын
i am trying to sort ' ' ' float array[5]= {11.1f, 11.34f ,11.22f, 11.99f, 11.00f}; ' ' ' it won't work in qsort why? any solution if i want to sort float using qsort?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Good question Varad! :-) qsort() should work with floats, so it makes me think something else is happening in your code, or maybe how qsort() is being used.
@varaddeshmukh1137
@varaddeshmukh1137 Жыл бұрын
@@PortfolioCourses is there any way to sort float using qsort()
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Yes, the 2nd post here has an example: cboard.cprogramming.com/c-programming/1293-qsort-doesnt-work-floats.html. :-)
@LisaDunleavy-q8o
@LisaDunleavy-q8o Ай бұрын
Hackett Oval
@SailsburySaxon-j1o
@SailsburySaxon-j1o 2 ай бұрын
Morar Stravenue
@fridaaa0
@fridaaa0 2 жыл бұрын
THANKS
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome! 😀
@RuthPerez-t3w
@RuthPerez-t3w Ай бұрын
Lawson Lane
@MariselaCrelia-t1e
@MariselaCrelia-t1e Ай бұрын
Schiller Vista
@DoyleKim-v7d
@DoyleKim-v7d 2 ай бұрын
Jayne Trail
@YolandaPotter-t1d
@YolandaPotter-t1d 2 ай бұрын
Batz Throughway
@AlexSeals-j8s
@AlexSeals-j8s Ай бұрын
Wisoky Prairie
@PierreStudzinski-r3y
@PierreStudzinski-r3y Ай бұрын
Garrison Key
@ChereGiandomenico-l8n
@ChereGiandomenico-l8n 2 ай бұрын
Roberts Lodge
@tristanhurley9071
@tristanhurley9071 2 жыл бұрын
beautiful
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you Tristan! :-)
@tristanhurley9071
@tristanhurley9071 2 жыл бұрын
@@PortfolioCourses No worries! Quick question: I am trying to order an array of structs by their Age. In your example when calling the qsort function you use "compare" as the last argument, but why do you not need to provide two inputs (x and y) into your compare function? Yours runs with just 'compare'. Apart from this, I think comparing the ages within the structs should be straight forward?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Great question. :-) So it's actually the *function* compare that is being supplied as an argument, we're not actually *calling* the compare function there so we don't supply arguments to it. The qsort() function will "have" the compare function as a parameter now, and *it* will actually call the compare function with different arguments to sort the list. And comparing ages within structs should be pretty similar. You'll need to some kind of different casting here though I suspect: int x = *(int *)x_void; int y = *(int *)y_void;
@BrettAida-n8y
@BrettAida-n8y Ай бұрын
Halvorson Lake
@TrevaAgbayani-w8b
@TrevaAgbayani-w8b Ай бұрын
Torp Glens
@FastDutt-b2h
@FastDutt-b2h Ай бұрын
Boehm Radial
@DickensEden-r8t
@DickensEden-r8t 2 ай бұрын
Miller Mews
@KaylaPfister-h3i
@KaylaPfister-h3i Ай бұрын
Schuppe Prairie
@ahmet8266
@ahmet8266 2 жыл бұрын
Thanks man i copied all of it
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Ahmet! :-D
@Final187ers
@Final187ers 2 жыл бұрын
every programmer ever
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Hahaha… :-) images.app.goo.gl/kAaYgj9HLikh6HrN6
@SunaSönmez-r9m
@SunaSönmez-r9m Ай бұрын
Salvador Mall
@SpringhallTammy-r4v
@SpringhallTammy-r4v Ай бұрын
Zander Path
@SophiaGiles-h3r
@SophiaGiles-h3r Ай бұрын
Elroy Unions
@ChristopherCarlisle-z7g
@ChristopherCarlisle-z7g 2 ай бұрын
Israel Trail
@SmollettJoan-e5q
@SmollettJoan-e5q 2 ай бұрын
Jasmin Divide
@FDevinCotton-d1b
@FDevinCotton-d1b Ай бұрын
Shanahan Drive
@KatharineIan-i2l
@KatharineIan-i2l Ай бұрын
Lueilwitz Rue
Quicksort Algorithm Implementation | C Programming Example
20:37
Portfolio Courses
Рет қаралды 62 М.
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 65 М.
бабл ти гель для душа // Eva mash
01:00
EVA mash
Рет қаралды 7 МЛН
🕊️Valera🕊️
00:34
DO$HIK
Рет қаралды 20 МЛН
Dynamically Allocate Memory For An Array Of Strings | C Programming Example
12:10
2.8.1  QuickSort Algorithm
13:43
Abdul Bari
Рет қаралды 3,2 МЛН
Sorting An Array Of Strings | C Programming Example
8:46
Portfolio Courses
Рет қаралды 29 М.
Passing an Array to a Function | C Programming Tutorial
9:30
Portfolio Courses
Рет қаралды 42 М.
Dynamically Allocate An Array Of Structs | C Programming Tutorial
15:11
Portfolio Courses
Рет қаралды 35 М.
Qsort in C
15:40
Programming with Sikander
Рет қаралды 31 М.
135 How to use library qsort function for sorting arrays
5:58
Programming Studio
Рет қаралды 607
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
you will never ask about pointers again after watching this video
8:03
бабл ти гель для душа // Eva mash
01:00
EVA mash
Рет қаралды 7 МЛН