Тёмный

How to sort part of an array in C 

Jacob Sorber
Подписаться 165 тыс.
Просмотров 6 тыс.
50% 1

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.th...
Website ➤ www.jacobsorbe...
---
How to sort part of an array in C // in the last video we talked about sorting in C, using qsort. This video follows up on that one to look at sorting just a portion of an array.
Related Videos:
Sorting video: • Sorting in C: Why the ...
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorbe...
people.cs.clem...
persist.cs.clem...
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
Source code is also available to Patreon supporters. --- [jsorber-youtub...]

Опубликовано:

 

1 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 36   
@papasmurf9146
@papasmurf9146 8 месяцев назад
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 😉
@grimvian
@grimvian 7 месяцев назад
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.
@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.
@martijnb3381
@martijnb3381 8 месяцев назад
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 8 месяцев назад
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 8 месяцев назад
​@@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 {};
@slavafrog7946
@slavafrog7946 Месяц назад
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?
@Jack_Pro_video_editor
@Jack_Pro_video_editor 4 месяца назад
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.
@nsg650
@nsg650 8 месяцев назад
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 8 месяцев назад
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 8 месяцев назад
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 8 месяцев назад
@@SlideRSBthis isn’t UB
@ЂорђеМилановић
@ЂорђеМилановић 8 месяцев назад
​​​@@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 8 месяцев назад
@@casperes0912 I know.
@0LoneTech
@0LoneTech 7 месяцев назад
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.
@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??
@sumitbhosale3401
@sumitbhosale3401 8 месяцев назад
Nice Video. Thanks! Hey Can You Implement Vector in C Or Hashmap Using Open addressing in C ?
@soniablanche5672
@soniablanche5672 8 месяцев назад
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
@greg4367
@greg4367 8 месяцев назад
OK, another T-shirt idea... qsort() sordid sorting since 1972
@justcurious1940
@justcurious1940 8 месяцев назад
Nice,Thanks.
Далее
Being Competent With Coding Is More Fun
11:13
Просмотров 85 тыс.
Why Function Pointers are Awesome
11:11
Просмотров 7 тыс.
How does fork work with open files?
13:12
Просмотров 10 тыс.
What's the Best Way to Copy a Struct in C and C++?
13:44
Your Variables are Not Real.
10:38
Просмотров 17 тыс.
Everything Starts with a Note-taking System
21:23
Просмотров 254 тыс.