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.

>> REGISTER NOW TO START POSTING

1. optimized selection sort

Hi, I'm reading up on Wikipedia and it uses shaker sort is optimization, are they actually 2 different algorithms. I can't seem to find it when I google for optimized selection sort. I thought shaker sort was bi-directional bubble sort (at each iteration, current largest and current smallest put in their proper place)

2. Re: optimized selection sort

A bidirectional variant of selection sort, called cocktail sort, is an algorithm which finds both the minimum and maximum values in the list in every pass. This reduces the number of scans of the list by a factor of 2, eliminating some loop overhead but not actually decreasing the number of comparisons or swaps. Note, however, that cocktail sort more often refers to a bidirectional variant of bubble sort.
Sounds like they're two distinct algorithms.

3. Re: optimized selection sort

So many dumb names when I looked it up in Wikipedia which makes it confusing, but to make sure I'll use these terms:

1) bi-directional bubble sort: alternate going thru list from both ends until no swaps needed (a flag)
2) optimized selection sort: need to find the min and max values so linear time to find both these values, then we put them in their final places, is this right?

eg of bi-directional bubble sort:
[2,3,4,5,1]

pass 1: [2,3,4,1,5] (now start at end of unsorted list towards the front of list/array for next pass, swap needed to move 1 to front of list for pass 2)
pass 2: [1, 2,3,5] (now start at front of unsorted list towards end of unsorted list for next pass, no swap so done because we have flag reset to no swap currently after pass 1 done (because pass 1 had flag of swap is true) )

eg of optimized selection sort:
[2,3,4,5,1], but if I find the min: 1, and find max: 5, am I supposed to put them in the current front and current end of list, because this would be bad because we need to shift other items over unless I'm not understanding optimized seleciton sort properly. Any help appreciated.

4. Re: optimized selection sort

1) bi-directional bubble sort: alternate going thru list from both ends until no swaps needed (a flag)
2) optimized selection sort: need to find the min and max values so linear time to find both these values, then we put them in their final places, is this right?
Sounds right to me.

With your [2,3,4,5,1] example, you could define a special case where the number of indices involved is 3. Keep track of the 3 values, then place the min at the beginning, the max at the end, and the remaining value into the middle index, resulting in [1,3,4,2,5]. The min/max search space then decreases by 2 and repeats.

There might be a better way to implement the selection sort variant.