Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: shaker sort

  1. #1
    Join Date
    May 2011
    My Mood
    Thanked 0 Times in 0 Posts

    Default shaker sort


    so shaker sort is supposed to be bi-directional bubble sort but I tried with an example, how is is it any better than regular, terrible bubble sort?

    n = 12
    I put braces around the sorted sub-lists
    descending order list: 30,25,24,23,22,20,18,17,16,15,14,13

    pass 1: 25,24,23,22,20,18,17,16,15,14,13{30}
    pass 2: {13}, 25,24,23,22,20,18,17,16,15,14,{30}
    pass 3: {13}, 24,23,22,20,18,17,16,15,14, {25,30}
    pass 4: {13,14},24,23,22,20,18,17,16,15,{25,30}
    pass 5: {13,14}, 23,22,20,18,17,16,15, {24,25,30}
    pass 6: {13,14,15}, 23,22,20,18,17,16,{24,25,30}
    pass 7: {13,14,15}, 22,20,18,17,16, {23,24,25,30}
    pass 8: {13,14,15,16}, 22,20,18,17,{23,24,25,30}
    pass 9: {13,14,15,16},20,18,17,{22,23,24,25,30}
    pass 10: {13,14,15,16,17,18},20,18{22,23,24,25,30}
    pass 11: {13,14,15, 16,17,18}20,{22,23,24,25,30} (20 is left, 1 items so trivially sorted)

    It's still (n - 1) passes as in bubble sort, so how is it improvment by scanning in both directions? Any help appreciated.

  2. #2
    Join Date
    Mar 2012
    United States
    My Mood
    Thanked 33 Times in 31 Posts

    Default Re: shaker sort

    Hello IHeartProgramming,
    As you have already stated the number of passes is n-1 and thus not an improvement upon bubble sort. However, this is just a single example with a fairly small number of integers to sort through, and nothing in terms of random ordering of the integers. To answer your question, it is not an improvement at all, in this case, but with larger lists that need to be sorted in a less trivial fashion (ie random order to ascending/descending) you should see an improvement.

Similar Threads

  1. heap sort vs merge sort
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 3rd, 2012, 03:37 AM
  2. Replies: 2
    Last Post: November 11th, 2012, 09:44 PM
  3. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  4. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 02:27 PM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM