Re: optimized selection sort

Quote:

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.

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.

Re: optimized selection sort

Quote:

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.