Quicksort Algorithm Implementation | C Programming Example

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • How to implement the quicksort algorithm in C. This algorithm notably uses a randomly selected pivot. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
    See Quicksort Algorithm Wikipedia article: en.wikipedia.org/wiki/Quicksort
  • Jak na to + styl

Komentáře • 118

  • @sniro1984
    @sniro1984 Před rokem +7

    Thank you so much for this video! I'm in the middle of a very intense bootcamp and they gave us an assignment to implement a generic quicksort for all types (like qsort in the linux man page). It was really hard for me but then I came across this tutorial and step by step I was finally able to understand the algorithm and make it work with the correct adjustments (pointer arithmetic and generic swap function). :)

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +2

      I'm so glad to hear this video was able to help you out! :-) Good luck with the rest of your bootcamp!

    • @uri-naor
      @uri-naor Před 3 měsíci

      It's Infinity Labs am I right? I'm doing it as well.

  • @meanup
    @meanup Před rokem +4

    Thank you so much. Initially I was struggling with the partition implementation but you make it clear.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +2

      You’re welcome, I’m really glad to hear the video cleared that up for you! :-)

  • @dieterbohm9700
    @dieterbohm9700 Před 7 měsíci +1

    Thank you so much bro! You make look this algorithms like a walk in the park! Your explanations are so good :)

  • @EmHuHuHu
    @EmHuHuHu Před 2 lety +11

    clear and well explained, your tutorials are beautiful!

  • @burkayelbir
    @burkayelbir Před rokem +9

    Love your videos man, you explaining everything one by one, i am glad i found this channel, thank you.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      I'm glad to hear you love the videos Burkay! 🙂 And you're very welcome, thank you for the kind feedback.

  • @nicoloasjosse9507
    @nicoloasjosse9507 Před rokem

    I needed to sort an array and move elements of others arrays conforming to order to the first array. Your crystal clear vidéo was a great help for me. Thank you sir, you got a new subscriber.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      You're very welcome, I'm glad the video was helpful for you! :-)

  • @nicolascristal3915
    @nicolascristal3915 Před 10 měsíci

    Crystal clear explanation. Thank you.

  • @wisdomokafor9631
    @wisdomokafor9631 Před 4 měsíci

    Thanks for the video so much. You really broke it down to the bear minimum

  • @idanmariani8601
    @idanmariani8601 Před rokem +1

    your awsome..every video you make just make everything super clear.. thank you!!!! :)

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      You're welcome Idan! :-) Thanks so much for the kind feedback too!!

  • @sofiahuppertz3653
    @sofiahuppertz3653 Před rokem

    Thank you very much for your videos! They have helped me a lot all along the way in learning C.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      You're very welcome Sophia, I'm glad to hear the videos are helping you to learn C! :-)

  • @gunjanagarwala5440
    @gunjanagarwala5440 Před 2 měsíci

    Thank you so much..after watching your explanation quick sort was crystal clear to me..

  • @WorstDraven
    @WorstDraven Před 5 měsíci

    Good structure and very understandable. Thank you ❤

  • @kornelodri9906
    @kornelodri9906 Před 2 lety +7

    Nice topic, nice explanation. Keep up in this direction.

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +2

      Thank you! I’m hoping to do some more sorting videos over the next while.

  • @netanelkomm5636
    @netanelkomm5636 Před rokem +1

    Thanks, this is awesome. Just what I needed.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      You’re welcome Netanel, I’m glad it was helpful for you! :-)

  • @hashious7270
    @hashious7270 Před rokem

    just thnx. no words to describe how good u explained it.

  • @Munchkin303
    @Munchkin303 Před rokem +1

    Very clear explanation!

  • @walidoulondon8107
    @walidoulondon8107 Před rokem +1

    This Chanel is awesome ❤ I learn a lot from it especially c language

  • @enmanuelsanchezabarua5482

    Very well explained!👍🏽👍🏽

  • @franciscrypto9143
    @franciscrypto9143 Před 2 lety +3

    you awesome at teaching c programming man!

  • @krishnabhagavankarri5623

    Best explanation i could get in youtube

  • @dimitrioskalfakis
    @dimitrioskalfakis Před rokem +1

    nice specialized version of qsort(). clear explanation of partition.

  • @ibrahimtarek9765
    @ibrahimtarek9765 Před rokem

    Clear explanation, thank you.

    • @PortfolioCourses
      @PortfolioCourses  Před 11 měsíci

      You're welcome, I'm glad you found the explanation clear! :-)

  • @vainar_
    @vainar_ Před rokem

    I love your videos and watch playlists, it would be cool if the tasks were more difficult!)

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      I'm glad you enjoy the videos! :-) The focus of the channel is helping beginner programmers so most of the content is covering easier tasks, but that said I do hope to cover more advanced topics and problems too. You might enjoy the videos on topics like function pointers, pthreads, structure padding and others if you're looking to learn some more advanced topics.

  • @lucky13pjn
    @lucky13pjn Před 5 měsíci

    So I used this video as an exercise to port your quicksort implementation to Rust for a bit of fun and learning. I tried to maintain the logic of the C version as best as I could (only the swap function and random number generation are different), but ran into a similar issue as the other comments here that make mention of using size_t instead of int where they were getting underflow problems. I was getting this issue where the partition function would return 0 after several recursions, and the next recursive call to quicksort would try to subtract 1 from that 0. I "fixed" this by wrapping the first quicksort recursive call in an if statement that checks if the pivot_index > 0. The output seems to always be correct. My question is, does that change break the quicksort in a way my potato brain is just not seeing?

  • @bytecode5834
    @bytecode5834 Před rokem

    Many thanks for the video.

  • @irinamocanu6210
    @irinamocanu6210 Před 5 měsíci

    good job!

  • @godswillchase1866
    @godswillchase1866 Před rokem

    using data type size_t for low, high and size of array, i get segmentation fault(core dumped)..
    why ???

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +3

      One possible reason is that size_t is an unsigned integer type. When we mix unsigned and signed types together in C, we can get some behaviours that we did not intend. For example when mixing unsigned int with int, the int may get converted to an unsigned int: riptutorial.com/c/example/6573/mixing-signed-and-unsigned-integers-in-arithmetic-operations. If you're trying to use something that can store larger values than "int", maybe using "long" instead, as long allows us to store larger signed integers values. :-)

  • @kurocxl5497
    @kurocxl5497 Před 2 lety

    What if I want the pivot to be the middle number of the array what would I type in the code?

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +1

      I have not tested this, but I think this should work:
      int pivot_index = low + ((high - low) / 2);
      That should find the midway point between the high and low indexes. If you give that a try, I would be curious to know if it works. :-)

  • @evgeniifeygin1030
    @evgeniifeygin1030 Před rokem

    Thanks a lot. But I agree with the previous point that indexes in C must be of size_t type. Then, let us imagine we have an array [3] = {3,3,1}, and we have no random pivot var. Pivot == 1, and in the end of the partition function we return index 0. Calling recursievly quicksort_recursion function with ''pivot_index - 1 , we receive : 0 -1 == 77737182319123178 " ( because pivot index is size_t)

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      No, indexes in C do not need to be of type size_t. What you've written is a bit difficult to understand, but if I'm understanding correctly it's pointing out a situation where size_t would actually cause a problem because it is unsigned.

  • @naboulsikhalid7763
    @naboulsikhalid7763 Před rokem +1

    when the code is explained, the picture is very clear. But my issue, how myself can think like the way it is explained. and there, where I got stuck all the time. I mean the thinking problem. Thank you for the effort deployed from your side to bring knowledge to us with simple way.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +9

      That's a good question Naboulsi. :-) I would say a couple things... coming up with something clever like the quicksort algorithm is a very hard problem. Most programmers don't "invent" algorithms like quicksort. So I wouldn't worry about being able to invent something like quicksort. But programms will study them and understand algorithms like these, so that part of the "thinking problem" is important. Unfortunately there is not an easy answer either. When programmers learn more and more solutions to problems, they gradually build up a toolbox of prior solutions in their mind. Then when they see a new but similar problem they are able to pull from the features of past solutions to help solve the new problem. So a lot of getting better at the thinking problem is studying lots and lots of code, for years at a time. Over time, you develop better and better problem solving skills and have a larger and larger library of past solutions in the back of your mind. :-)

    • @naboulsikhalid7763
      @naboulsikhalid7763 Před rokem

      @@PortfolioCourses it seam you just answer to my question dilemma. I will keep practicing to build my pyramid of solution. Thank you for your Gold answer.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      You're welcome! :-)

  • @marbles5590
    @marbles5590 Před rokem

    Hello, do you have a video for Shell sort?

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      No, but I *really* want to do a video on shell sort, so hopefully one day I can do it. :-)

  • @user-ej9pe2fw6x
    @user-ej9pe2fw6x Před 8 měsíci +1

    thanks ,sir ❤

  • @user-zv4gc1bz4w
    @user-zv4gc1bz4w Před 5 měsíci

    why use &array

  • @dijkstra4678
    @dijkstra4678 Před 2 lety

    very nice

  • @natevaub
    @natevaub Před rokem

    Hi, great video as always. Is it a good idea to calculate the median of the array and pick it as the pivot value?

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      Great question Nathan! We could choose the median as the pivot, but the issue is that the "cost" of determining the median is too high as we need to examine each element in that portion of the array: cs.stackexchange.com/a/33600. Choosing a random pivot is faster. One similar thing we could do though is choose the "median/middle" element between the first, middle and last elements in the portion of the array we are looking at. That way we are only looking at 3 elements, and not all elements in the portion of the array. I believe that strategy is actually about as good as choosing a random pivot: en.wikipedia.org/wiki/Quicksort#Choice_of_pivot. :-)

    • @natevaub
      @natevaub Před rokem

      @@PortfolioCourses Thanks a lot for the quick answer, you are such a good instructor. May love and happiness surround you and your family

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      @@natevaub Thank you so much Nathan, I wish the same for you and your family too! 🙂

  • @cirobermudez
    @cirobermudez Před rokem

    To generate random number between low and high the equation is
    A) rand() % (high + 1 - low) + low
    so is it ok to use rand() % (high + 1 - low) ?
    Love the way you explained, your tutorials are amazing.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +2

      Thank you for the kind feedback! :-) The problem with rand() % (high + 1 - low) is that it will only give us numbers in the range of 0 .... (high - low). So that wouldn't work. rand() % (high + 1 - low) will give us numbers in the range of 0 ... (high - low), and then we we add low to this range we get low ... high, which is what we want, so that's why we do it that way. :-)

    • @cirobermudez
      @cirobermudez Před rokem +1

      @@PortfolioCourses
      Thank you very much for your answer, I'm very sorry, I wrote the equation wrong, I meant the following:
      bottom + ( rand() % (top - bottom + 1) ) has a range of [bottom, top]
      In the code:
      low + (rand() % (high - low) ) has a range of [low, high -1]
      the real question is should we change the equation for?
      low + (rand() % (high - low + 1) ) which has a range of [low, high]

  • @MrJzvoyeur
    @MrJzvoyeur Před 5 měsíci

    Testing sorting algorithms from this playlist
    (videos 36, 44, 73, 121, 126) on a raspberry-pi 4,
    measure-function from video 244, length of sort-array: 100000
    > quick-sort: 0.086109 sec (on my other computers quick-sort is faster than merge-sort)
    > merge-sort: 0.045641 sec
    > insertion-sort: 23.990210 sec
    > selection-sort: 29.300313 sec
    > bubble-sort: 74.007234 sec

  • @juicewrldmain2474
    @juicewrldmain2474 Před rokem

    w creator w video w explanation

  • @jonjones6218
    @jonjones6218 Před 2 lety +2

    Sir,awesome algorithm.
    Can You please make a video about this task:
    Delete all fruits which are equal with a fruit enter by user.
    For example if array is:
    "apple pineapple apple strawberry apple"
    The array will be:
    "pineapple strawberry"

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety

      For some reason CZcams held your comment in review, I've just noticed this and published it now. Are all of the fruits in one string? i.e. a 1D char array Or are all of the fruits in an array of strings? i.e. a 2D char array

  • @user-lf3yj5zb2r
    @user-lf3yj5zb2r Před 2 lety +2

    I loooooveeee your explanation. I know that you teach in Canada colleges. (from previous comments) Could you please recommend some colleges in Canada with decent Computers and Information Technology level? Currently I am doing Electrical Eng. in Germany but it is tooooo long and I do not like the way how it is there. I am thinking to move to Canada and take 2years diploma in IT.
    Best regards

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +1

      I'm gad you enjoyed the explanation! :-) In terms of 2 year college programs in Canada... here the government heavily regulates post-secondary programs like college programs. And all the schools are 'public sector', i.e. they're all funded by the government too. So the quality of various college programs is pretty even across different schools by design. Unlike say the USA where you have many public and private institutions with more varying quality. I would suggest picking a school based on where you would like to live. And if you want to immigrate to the country one day to stay long term, you could pick a school some place where you would like to work one day too. 🙂 But in terms of colleges in Canada, you can't really go wrong, so picking one based on location is probably you best bet.

  • @Jake-cb8dm
    @Jake-cb8dm Před 9 měsíci

    did you use randomized pivoting here with that optimization at the end?

    • @PortfolioCourses
      @PortfolioCourses  Před 9 měsíci

      Yes! :-)

    • @Jake-cb8dm
      @Jake-cb8dm Před 9 měsíci

      @@PortfolioCourses I tried to implement this using a random number generator and wasn’t too sure if I was using that optimization correctly I was able to use basic quicksort though

    • @Jake-cb8dm
      @Jake-cb8dm Před 9 měsíci

      When testing run time to go through the random array I wasn’t seeing too much of a difference obviously there won’t be but still wasn’t sure if it was working properly either way thank you for this video

  • @franciscrypto9143
    @franciscrypto9143 Před 2 lety

    hi, do you have linked list implementation of quick sort? wanna know and learn to

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety

      Sorry Francis I don't have a video on a linked list implementation of quicksort.

  • @simonapopa9397
    @simonapopa9397 Před 2 lety

    nice!

  • @pastifier9349
    @pastifier9349 Před 5 měsíci

    if no swaps occur during partitions, the index stays zero. Whether you use size_t or int, the resulting value would be invalid, causing a buffer-overflow. So, be careful of that. Add guards *!

    • @PortfolioCourses
      @PortfolioCourses  Před 5 měsíci

      Nothing breaks if the index of i is still 0 because no swaps occur.

  • @gabiedubin
    @gabiedubin Před 2 lety

    if you're wonder why your code doesn't work when you did exactly what this guy did, check if you're returning the end of all of the lower values in partitition and not the pointer to the divider.

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety

      I just want to make sure I understand Gabie, are you saying that the code has a bug in it? Or are you saying that this is a bug you made when you coded it, and you're giving people a heads up to help them avoid it? :-)
      If there is a bug in the code I would want to fix it is all, but there shouldn't be a bug in it: github.com/portfoliocourses/c-example-code/blob/main/quicksort.c. :-)

  • @BobChess
    @BobChess Před 7 měsíci

    I thought this would be easier than merge sort but I was completely wrong

  • @indigo_diary
    @indigo_diary Před 7 měsíci

    If you're a beginner, is it even possible to figure out how to do quicksort without watching and basically copy-pasting code from a tutorial like yours?

    • @PortfolioCourses
      @PortfolioCourses  Před 7 měsíci

      Quicksort and other harder algorithms like merge sort are known as being trickier for beginners to understand. It might technically be possible as a beginner to just read how the algorithm works and then implement it, but in practice I think virtually all beginners will at first copy and run code to help understand the algorithm. Maybe later on, once they know the algorithm better and how it is implemented, then they can get more comfortable with writing the code themselves. But even then to be honest with you experienced teachers before teaching quicksort will have to remind themselves of how quicksort works, including the implementation/code (....I know I did !). :-)

    • @indigo_diary
      @indigo_diary Před 7 měsíci

      @PortfolioCourses thanks a lot for your encouraging and fast reply!

    • @MrJzvoyeur
      @MrJzvoyeur Před 5 měsíci

      @@PortfolioCourses from my experience it is a good way to use paper and pencil and follow the algorithm.

  • @fifaham
    @fifaham Před rokem

    @10:28 isn't that supposed to be J ? Nice algorithm. Thank you.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      You're welcome! And yes that's true, I fix that later in the video before running it. :-)

  • @alankertegy2590
    @alankertegy2590 Před 7 měsíci

    Who up quicking their sort?

  • @ionguzun3952
    @ionguzun3952 Před 2 lety +2

    this is harder than it looks...

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +2

      Agreed, Quicksort is considered trickier than Bubble Sort, Selection Sort, and Insertion Sort by most people.

    • @vladguzun2522
      @vladguzun2522 Před 2 lety

      yes baby:0

  • @nipundesilva9306
    @nipundesilva9306 Před rokem

    If first element less than initial pivot value this code has a bug

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      That's not true, you might be misunderstanding the algorithm or something. If you explain why you think that will occur, I might be able to help. :-)

  • @adnanemezrag3809
    @adnanemezrag3809 Před rokem

    This is so hard you could explain it deliberately.

  • @ambermittal8174
    @ambermittal8174 Před rokem +1

    MK

  • @twinsjka8304
    @twinsjka8304 Před 6 měsíci

    thank you man

  • @youarestillalive
    @youarestillalive Před rokem +4

    you are talking super fast

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +3

      Sorry to hear you find the talking fast, I hope you’re able to find another video where the talking is slower. :-)

    • @ameen6630
      @ameen6630 Před 7 měsíci +1

      Just use 0.75x if ya have feel it's too fast,

    • @IrohaNatsumeMyBeloved
      @IrohaNatsumeMyBeloved Před 6 měsíci

      theres literally an option where you can make the video speed slower

    • @tsum3489
      @tsum3489 Před 5 měsíci

      Then why am i listening at 1.5x😂

  • @ahmedbr4926
    @ahmedbr4926 Před rokem

    can you tell me haw can in use the fonction srand plz🥲

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      This video covers random number generation in C: czcams.com/video/Mp3eGLX-OpY/video.html. :-)

    • @ahmedbr4926
      @ahmedbr4926 Před rokem

      @@PortfolioCourses Thanks 🙏🏻😊

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      @@ahmedbr4926 You're welcome! 🙂