How to sort part of an array in C

  Рет қаралды 5,994

Jacob Sorber

Jacob Sorber

Күн бұрын

Пікірлер: 36
@papasmurf9146
@papasmurf9146 9 ай бұрын
This is why I think learning C (and assembly) is important ... you get a better understanding of the fundamentals.
@martijnb3381
@martijnb3381 8 ай бұрын
And for me its way more fun then beeing a webdeveloper.. You are working on the solution instead of learning a framework that falls appart after every update😅
@godnyx117
@godnyx117 7 ай бұрын
@@martijnb3381 Amazing comment! And don't forget that every year, a new framework is made and yours losses and losses founding, developers and community so you have to learn the new one. 🤦
@martijnb3381
@martijnb3381 7 ай бұрын
@@godnyx117 Thank you! I also think that some frameworks or solutions (in my case Magento) are necessary. But i really enjoy building things from scatch, with jQuery, C programming or making a custom PHP project that uses Magento bootstrap to import products. I wish i could do that all day :)
@godnyx117
@godnyx117 7 ай бұрын
@@martijnb3381 Well, you cannot code all day, it's unhealthy 😉
@madhurbatra321
@madhurbatra321 8 ай бұрын
man, you are a legend. All these videos and deep linux/C insights are really good. This is a niche content that may not get absurdly high views, but this is a really high quality content. Thank you! Please continue what you're doing.
@grimvian
@grimvian 8 ай бұрын
I really like C and thanks for making informative C videos. I have a few comments: I think you are over editing your videos (less is more) and I think the code should occupy more than half of the screen.
@martijnb3381
@martijnb3381 9 ай бұрын
Maybe to complicated for beginners but please use sizeof(*values) instead of sizeof(int). For me this is a bug waiting to happen. Also applies to malloc().
@MrGeorge1896
@MrGeorge1896 9 ай бұрын
Oh I just wanted to comment on this topic too. As a bonus: we can use both forms i.e. "sizeof *values" or "sizeof values[0]". We don't need the brackets after "sizeof" either. They are only needed if we just use "sizeof" with a type name and not a variable name.
@martijnb3381
@martijnb3381 9 ай бұрын
​@@MrGeorge1896 thx . yep, you are right. Its a personal preference to use sizeof() . I think it is more readable this way. But using 'sizeof(int)' or 'sizeof int' when you have context, is just bad practice. Dont do this in production code. To make it a bit more clear, a 'good' example for malloc: int *pInt = (int *)malloc(sizeof(*pInt)); And when you hate C++ programmers just do: int *pInt = malloc(sizeof(*pInt)); :)
@hedgechasing
@hedgechasing 8 ай бұрын
@@martijnb3381 writing the explicit cast is actually a terrible idea. If you do not include the header that defines malloc, C89 says the compiler should assume it returns an Int and the cast will truncate the pointer on a 64 bit system with 32 but ints (all modern platforms). If you have an explicit cast there, then it will silence a potential warning about implicitly casting from int to pointer, and besides you are introducing another thing to change if you retype the variable. You shouldn’t be calling malloc and casting the return in C++ anyways, you need to use the placement form of the new operator to properly convert a void point into a typed pointer by calling the constructor.
@martijnb3381
@martijnb3381 8 ай бұрын
@@hedgechasing I see what you mean. Ofcourse i include stdlib.h. I just use malloc in the same way as Sqlite. And that made a lot more sense to me, then doing sizeof(int) inside of malloc. The explicit cast is there for C++ and to get a warning from the compiler when var pInt changed type. Ooo you say "retype" we are talking about the same thing, probably.. Then i prob dont fully understand the explicit cast. To me it seems safe because you give the compiler the most info. And you will get an error when retype, like: double *p = (int *)malloc(sizeof(*p));
@hedgechasing
@hedgechasing 8 ай бұрын
@@martijnb3381 yeah so if you changed the variable type, then you have to change the cast just as you would have to change the size of. For C code, I am recommending int *p=malloc(sizeof *p) so that you can change Int to anything else, and nothing else needs to change to keep the program working. As for C++ compatibility, you should not be calling malloc in C++ and just casting the result. If you want to dynamically allocate memory, you should just call new and let it call malloc, or if you really want to, the proper way to write that code in C++ would be to use the result of calling malloc within a placement new expression, something like int *p = new (malloc(sizeof *p)) std::remove_reference_t {};
@0LoneTech
@0LoneTech 8 ай бұрын
This is not often the kind of partial sort that helps. It's common to want a specific window into a fully sorted array, without necessarily sorting it all. Consider SQL order by price asc limit 25 offset page*25. Quicksort is easily adaptable to this task, but libc qsort is not. A double ended heap might be better still. uvector-algorithms has functions like selectByBounds and partialSortByBounds for such flexibility.
@slavafrog7946
@slavafrog7946 2 ай бұрын
Hi, Jacob! How could C interact with Excel sheets? I mean, apart from converting xlsx to CSV, how could I read data from Excel with C? Is there a relatively straightforward way to do that?
@sumitbhosale3401
@sumitbhosale3401 8 ай бұрын
Nice Video. Thanks! Hey Can You Implement Vector in C Or Hashmap Using Open addressing in C ?
@justcurious1940
@justcurious1940 8 ай бұрын
Nice,Thanks.
@magicmorz
@magicmorz 7 ай бұрын
hey, could be interesting to make a video about applying BPF filters to sockets and the subject of BPF in general :)
@rustycherkas8229
@rustycherkas8229 8 ай бұрын
Can only imagine the question comes from a beginner who will be meticulously writing complicated code to perform iterative "sub-sorting" on a 2nd field in the struct version... Perhaps writing that single required comparison function is the topic for next week's video??
@nsg650
@nsg650 9 ай бұрын
I just do &array[INDEX] since it looks alot cleaner and would cause less logic bugs. Edit: the word i was going for was logic bugs not ub sorry!
@ziyad_ibrahim
@ziyad_ibrahim 9 ай бұрын
This way could do more cpu work than in the video Like first you access indexed value after take its address, in video just go 2 more block and send to function
@SlideRSB
@SlideRSB 9 ай бұрын
Less undefined behavior? The notification of baseptr+offset is very well defined in C. Whether or not your code is cleaner is just a matter of taste.
@casperes0912
@casperes0912 9 ай бұрын
@@SlideRSBthis isn’t UB
@ЂорђеМилановић
@ЂорђеМилановић 9 ай бұрын
​​​@@ziyad_ibrahimHow will it do more work? Do you know how is "taking" address implemented? It's literrary reversed, you first must get the pointer, and then dereference it. So compiler won't dereference anything in this case, it will just calculate the address.
@SlideRSB
@SlideRSB 9 ай бұрын
@@casperes0912 I know.
@Jack_Pro_video_editor
@Jack_Pro_video_editor 5 ай бұрын
Your recent vidEos are good. With some strategIc editing, they have the potEntial to attract a wIder audience aNd generate morE engagemeNt.
@unperrier5998
@unperrier5998 7 ай бұрын
Is everything fine Jacob? It's been a month since your last video.
@greg4367
@greg4367 8 ай бұрын
OK, another T-shirt idea... qsort() sordid sorting since 1972
@soniablanche5672
@soniablanche5672 9 ай бұрын
well obviously you can, sorting algorithms take a pointer and the size of the array so all you have to do is adjust the pointer and size parameter lol
When do I use a union in C or C++, instead of a struct?
11:18
Jacob Sorber
Рет қаралды 69 М.
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 101 М.
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 150 МЛН
Cool Parenting Gadget Against Mosquitos! 🦟👶 #gen
00:21
TheSoul Music Family
Рет қаралды 33 МЛН
What's in the clown's bag? #clown #angel #bunnypolice
00:19
超人夫妇
Рет қаралды 33 МЛН
What's the Best Way to Copy a Struct in C and C++?
13:44
Jacob Sorber
Рет қаралды 34 М.
Why Function Pointers are Awesome
11:11
Jacob Beningo
Рет қаралды 8 М.
Understanding and implementing a Hash Table (in C)
24:54
Jacob Sorber
Рет қаралды 359 М.
Pulling Back the Curtain on the Heap
21:38
Jacob Sorber
Рет қаралды 37 М.
"Hello, World" in 5 CURSED languages that no one should use
13:08
Dreams of Code
Рет қаралды 548 М.
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 722 М.
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 54 М.
Making variables atomic in C
11:12
Jacob Sorber
Рет қаралды 37 М.
How does fork work with open files?
13:12
Jacob Sorber
Рет қаралды 10 М.
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 150 МЛН