Sorting Algorithms Redux 04: Cocktail Sort

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ม.ค. 2025

ความคิดเห็น •

  • @squirrelbrains2197
    @squirrelbrains2197 8 ปีที่แล้ว +28

    i like precise and concise explanations, thanks for the video.

    • @NERDfirst
      @NERDfirst  8 ปีที่แล้ว +2

      You're welcome! Very happy to be of help =)

    • @bleisenberg
      @bleisenberg 4 ปีที่แล้ว

      yes

  • @NERDfirst
    @NERDfirst  12 ปีที่แล้ว +1

    Hello again!
    The expression of time complexity is done in what's called the "Big O Notation", and one characteristic of such expression is that in general, coefficients and constant factors are discarded.
    The reason why we do this is we want to consider how the algorithm performs on a large set (ie n approaches infinity).
    So even if the time taken for something is 6n³ + 2n² + 3, under the Big O Notation we simply say it takes O(n³) time, since as n approaches infinity, only the "³" matters!

  • @NERDfirst
    @NERDfirst  11 ปีที่แล้ว

    Hello and thank you for your comment!
    Cocktail sort is slightly faster than bubble sort, because it eliminates the "turtles" in bubble sort (large elements that take a long time to move towards the left, in an ascending sort). This does not change the Big O time complexity though - It's still an O(n²) algorithm.
    Not sure if there are any disadvantages compared to bubble sort, but in general, it is of course still inefficient compared to say QuickSort which has O(n log n) time.

  • @NERDfirst
    @NERDfirst  11 ปีที่แล้ว

    Hello and thank you for your comment!
    Indeed, as mentioned in the video, rigourously speaking, cocktail sort does not provide a better performance in comparison to bubblesort, as they are both O(n²) algorithms.
    It's just that in some cases (eg sorting {2,3,4,5,1} in ascending order), cocktail sort requires far less comparisons to complete the job.
    That of course, doesn't make it more efficient than O(n²), but there is indeed less computation, and hence is "faster".

  • @NERDfirst
    @NERDfirst  11 ปีที่แล้ว

    Hello and thank you for your comment!
    In general, best time refers to the least time for a sorting algorithm to finish its work. The naive implementation of cocktail sort will not have a difference between the two (both O(n²)), but if it was able to identify that no swaps have been made and to terminate, then the best case time is O(n), with a sorted list as input.

  • @NERDfirst
    @NERDfirst  12 ปีที่แล้ว

    Hello again and many thanks for your comment! Glad you enjoyed =)

  • @NERDfirst
    @NERDfirst  10 ปีที่แล้ว

    +D Haugabook Thank you very much for your comment! I personally leave out the pseudocode to keep these episodes simple. After all, they are targetted at the absolute beginner, and it's more important for beginners to be able to picture the algorithms first, before anything else!

  • @NERDfirst
    @NERDfirst  12 ปีที่แล้ว +2

    You're very welcome! Glad you found my stuff useful =)

  • @NERDfirst
    @NERDfirst  11 ปีที่แล้ว

    Hello and thank you for your comment! Glad you enjoyed the video =)

  • @NERDfirst
    @NERDfirst  11 ปีที่แล้ว

    Hello and thank you for your comment!
    In the event that you need to write a program that needs to sort some data (whether directly for output, at the user's command, or as an intermediate step for another algorithm), the techniques shared here can be applied.

  • @skaniver1
    @skaniver1 11 ปีที่แล้ว

    Thank you :) :) that is perfect! wow what a interactive channel, definitely got my subscription, busy doing a semester project on all the sorts :) studying a BSC computer science :)

  • @surajsajjala2857
    @surajsajjala2857 3 ปีที่แล้ว

    Too good, explained very easily, understandable

    • @NERDfirst
      @NERDfirst  3 ปีที่แล้ว

      Hello and thank you for your comment! Glad you liked the video =)

  • @Mytreya-maan6
    @Mytreya-maan6 5 ปีที่แล้ว

    wow, the explanations are truly precise!

    • @NERDfirst
      @NERDfirst  5 ปีที่แล้ว

      Hello and thank you for your comment! Glad you liked the video =)

  • @ninav1522
    @ninav1522 4 ปีที่แล้ว

    You´ve helped me a lot!
    That´s what I was looking for

    • @NERDfirst
      @NERDfirst  4 ปีที่แล้ว

      Hello and thank you for your comment! Very happy to be of help =)

  • @kyuchanao
    @kyuchanao 12 ปีที่แล้ว

    One case is 2n, then completing the whole thing is (2n)^2, which should be 4n^2, do we ignore the 4 in front of 4n^2 (so that the time complexity becomes n^2)?

  • @NERDfirst
    @NERDfirst  11 ปีที่แล้ว +1

    Hello again and thank you so much! Actually same here, I'm doing a degree program in computer science as well =P

  • @sumyatnoepwint9684
    @sumyatnoepwint9684 9 ปีที่แล้ว +1

    Pretty Helpful. Thanks!

    • @NERDfirst
      @NERDfirst  9 ปีที่แล้ว

      +Sumyat Noepwint You're welcome! Very happy to be of help =)

  • @preciouswocha9620
    @preciouswocha9620 11 ปีที่แล้ว

    Thanks for the explanaton. Very precise as always. But I have a question. How do these algorithms, relate to coding?

  • @enditend2
    @enditend2 11 ปีที่แล้ว

    what's the differenece between best time and average?

  • @GabrielHasbun
    @GabrielHasbun 12 ปีที่แล้ว

    Fun as always.

  • @surgegraphicnovel
    @surgegraphicnovel 11 ปีที่แล้ว

    This was very helpful. It would be great to also include pseudocode and flowchart examples. (I'm studying this in school.) Your explanation where great. I look forward to watching more videos from you.

    • @NERDfirst
      @NERDfirst  11 ปีที่แล้ว +2

      Hello and thank you for your comment! I'm very happy to be of help =)
      I deliberately left out pseudocode in this entire series because one of the most common pitfalls for beginners is attempting to go through pseudocode before being able to picture an algorithm. I'm only focusing on the latter for this course.
      As for flowcharts, I'm not very sure what sort of flow we're showing here. I feel nothing really beats showing how a sort works better than simply tracing it! =P

  • @skaniver1
    @skaniver1 11 ปีที่แล้ว

    can you please help me with some advantages and disadvantages of the cocktail sort :) Thanks in advance :)

  • @adamzerner5208
    @adamzerner5208 10 ปีที่แล้ว +1

    First off, your stuff is great! I've looked at a lot of algorithms resources and your videos are by far my favorite! Keeps it concise without leaving out any essential information.
    Just want to be clear: in each run through, you're looking at two less terms right? Like if you had an array of size 10... it'd you'd look at elements 0-8 on the way up and 8-1 on the way down, then 1-7 on the way up and 7-2 on the way down, then 2-6 on the way up and 6-3 on the way down etc.
    Also, why is there so much focus on worst cases? To use cocktail sort as an example, it seems that it'd take a really odd arrangement of elements to lead to the worst case, and that it'd perform really good in the average case. Shouldn't an algorithm's efficacy be judged as an expected value? Like you'd take each possible arrangement of elements and the corresponding speeds of the algorithm, and multiply that by the likelihood that the elements start off arranged that way. I guess looking at worst-case would work well in practice though because using expected values is complicated. What do you think?

    • @NERDfirst
      @NERDfirst  10 ปีที่แล้ว

      Adam Zerner Hello and thank you very much for your comment, and doubly thanks for your kind words! Yes you are correct - Every pass puts two terms in place, and as such for every pass, we look at two less terms. This is an interesting train of thought. I do acknowledge that most of the time, algorithms terminate before their worst case time complexity. In fact, average-case analysis is also something that is done, but like you mentioned, it is a considerably harder task to perform. This is especially true when the input data set starts to get massive, or when we are working with more complex algorithms that perform more actions to the data. Also, there is a value to worst case analysis, both of time and space complexity - Knowing the worst case performance gives us a guarantee of how much time and resources we need, and practically, this is pretty important information since this allows us to determine system requirements.

  • @lorenainfanter.3099
    @lorenainfanter.3099 7 ปีที่แล้ว

    thank you!!! excellent explanation!

    • @NERDfirst
      @NERDfirst  7 ปีที่แล้ว +1

      You're welcome! Very happy to be of help =)

  • @yaraalshahrani8943
    @yaraalshahrani8943 9 ปีที่แล้ว

    hello can you tellm me what the advantage and disadvantage for the cocktail sort

    • @NERDfirst
      @NERDfirst  9 ปีที่แล้ว +1

      +yara alshahrani Trouble with school work? Let's see...
      Advantages: Reasonably easy to implement. Just a bit tougher than Bubble sort, but requires significantly less passes in the general case.
      Disadvantages: Still a slow algorithm with an O(n²) worst case.

    • @wassilchoujaa3478
      @wassilchoujaa3478 8 ปีที่แล้ว

      you arlready said it in the video m8 => no need to repeat yourself . ! I rate this AA++ content by the way nearly perfect !

    • @NERDfirst
      @NERDfirst  8 ปีที่แล้ว

      +Dontsaythat Hello and thank you for your comment! No worries about the repetition, as long as I have the time to help people out, I'll try to do so in whatever way works best for them :)

  • @Megaeggz1
    @Megaeggz1 7 ปีที่แล้ว +1

    Amazing thanks!

    • @NERDfirst
      @NERDfirst  7 ปีที่แล้ว

      You're welcome! Very happy to be of help =)

  • @jackodum9643
    @jackodum9643 8 ปีที่แล้ว

    0612 Tv rocks!

    • @NERDfirst
      @NERDfirst  8 ปีที่แล้ว

      Thank you very much! Glad you've been liking my work =)

  • @kanglinzeng306
    @kanglinzeng306 7 ปีที่แล้ว

    Thanks for the video!

    • @NERDfirst
      @NERDfirst  7 ปีที่แล้ว

      You're welcome! Very happy to be of help!

  • @hanaleblanc1995
    @hanaleblanc1995 9 ปีที่แล้ว

    This was soooo helpful thank you

    • @NERDfirst
      @NERDfirst  9 ปีที่แล้ว

      hanaleblanc1995 You're welcome! Very happy to be of help =)

  • @Bl4sTTer
    @Bl4sTTer 7 ปีที่แล้ว

    very very good explanation thanks :)

    • @NERDfirst
      @NERDfirst  7 ปีที่แล้ว

      You're welcome! Thank you very much for you comment and I'm happy to be of help =)

  • @saraharar3114
    @saraharar3114 5 ปีที่แล้ว

    that was great ! thank you :)

    • @NERDfirst
      @NERDfirst  5 ปีที่แล้ว +1

      Hello and thank you very much for your comment! Glad you liked the video =)

  • @evelynduran9827
    @evelynduran9827 10 ปีที่แล้ว +1

    Hello Urjente need that you develop step by step as you get O n ^ 2 thank you

    • @evelynduran9827
      @evelynduran9827 10 ปีที่แล้ว

      O(n²)

    • @NERDfirst
      @NERDfirst  10 ปีที่แล้ว

      Hello and thank you for your comment!
      Let's take that every pass of cocktail sort is just in one direction. Meaning:
      First pass (left to right): Looks at n items
      Second pass (right to left): Looks at n-1 items
      Third pass (left to right): Looks at n-2 items
      ... .... ...
      Second last pass: Looks at 2 items
      Last pass: Looks at 1 item
      Add up all the total comparisons:
      n + (n-1) + (n-2) + ... + 2 + 1
      This is an arithmetic progression, and hence we can perform the following substitution:
      dl.dropboxusercontent.com/u/27029501/formula.jpg
      O( (1/2)(n)(n+1) ) = O( (1/2)(n² + n) )
      Since the Big O Notation discards coefficients and all terms except that of the highest degree, we get O(n²).

    • @evelynduran9827
      @evelynduran9827 10 ปีที่แล้ว

      lcc0612 OHHHH you are a genius thank a lot :)

    • @NERDfirst
      @NERDfirst  10 ปีที่แล้ว

      You're welcome! I'm very happy to be of help =)

  • @seansimonalle8896
    @seansimonalle8896 3 ปีที่แล้ว

    A little late to say it, but thanks so much for this!

    • @NERDfirst
      @NERDfirst  3 ปีที่แล้ว +1

      You're welcome! It's never too late because I get notified of new comments all the time and I'll do my best to engage =) Either way, very happy to be of help =)

  • @issa159357
    @issa159357 11 ปีที่แล้ว

    great keeping on ,,, :)

  • @GAMEPLAYNORTE
    @GAMEPLAYNORTE 9 ปีที่แล้ว

    insane, man.
    help a lot.
    thanks
    hold this like!

    • @NERDfirst
      @NERDfirst  9 ปีที่แล้ว +1

      +GAMEPLAY NORTE Thank you very much! Glad you found the video helpful =)

  • @shawn576
    @shawn576 2 หลายเดือนก่อน

    This doesn't seem like it should be faster. Each pass is still only putting 1 item in the correct position.

    • @NERDfirst
      @NERDfirst  2 หลายเดือนก่อน

      Hello and thank you for your comment! Yes, indeed, there's no improvement in terms of worst case Big O time complexity. The advantage of this technique is that it increases the likelihood of an early termination. Consider the following data set:
      2, 3, 4, 5, 6, 7, 8, 1
      If you're using regular old bubble sort, it moves the "1" by one position with each pass, necessitating a full _n_ passes to get everything in place. Cocktail sort on the other hand, gets the job done by the second pass, so assuming that we have mechanisms in place to detect a sorted list, three full passes is all it takes before the algorithm terminates.
      So cocktail sort is more about allowing more combinations of input to achieve early termination, and less about changing the worst case.

  • @kettefu
    @kettefu 11 ปีที่แล้ว

    In this case, it just appears as if the cocktail sort is faster. In the bubble sort, 9 is fastest into position, followed by 8 (a little slower), and so forth. In the cocktail, 1 becomes the new "8", and so forth. So the order of "speed" is then 9,1,8,2,7,3,6,4,5. No speed advantages at all. It's all in appearances. Still n^2.