<< Chapter < Page Chapter >> Page >

The code to implement a selection sorter is straightforward. One need only implement the split and join methods where the split method always returns the lo+1 index because the smallest value in the (sub-)array has been moved to the index lo position. Because the bulk of the work is being done in the splitting method, selection sort is classified as an "hard split, easy join" sorting process.In the Java implementation of the SelectionSorter class below, the split method splits off the extrema (minimum, here) value from the sub-array, while the join method is a no-op.

Selectionsorter class

package sorter; /*** A concrete sorter that uses the Selection Sort technique. */public class SelectionSorter extends ASorter {/** * The constructor for this class.* @param iCompareOp The comparison strategy to use in the sorting. */public SelectionSorter(AOrder iCompareOp) {super(iCompareOp); }/** * Splits A[lo:hi]into A[lo:s-1] and A[s:hi]where s is the returned value of this function. * This method places the "smallest" value in the lo position and splits it off.* @param A the array A[lo:hi] to be sorted.* @param lo the low index of A. * @param hi the high index of A.* @return lo+1 always */protected int split(Object[] A, int lo, int hi){ int s = lo;int i = lo + 1; // Invariant: A[s]<= A[lo:i-1].// Scan A to find minimum: while (i<= hi) {if (aOrder.lt(A[i], A[s])) s = i;i++; // Invariant is maintained. } // On loop exit: i = hi + 1; also invariant still holds;// this makes A[s] the minimum of A[lo:hi]. // Swapping A[lo]with A[s]:Object temp = A[lo];A[lo] = A[s]; A[s]= temp; return lo + 1;} /*** Joins sorted A[lo:s-1] and sorted A[s:hi]into A[lo:hi].* This method does nothing. The sub-arrays are already in proper order. * @param A A[lo:s-1]and A[s:hi] are sorted.* @param lo the low index of A. * @param s* @param hi the high index of A. */protected void join(Object[] A, int lo, int s, int hi){ }}

What's interesting to note here is what is missing from the above code. A tradional selection sort aalgorithm is implemented using a nested double loop, one to find the smallest value and one to repeatedly process the ever-shrinking unsorted sub-array. Notice that the above code only has a single loop, which coresponds to the inner loop of a traditional implementation. The outer loop is embodied in the recursive nature of the sort template method in the ASorter superclass.

Notice also that the selection sorter implementation does not include any explicit connection between the split and join operations nor does it contain the actual sort method. These are all contained in the concrete sort method of the superclass. We describe the SelectionSorter class as a component in a framework (technically a "white box" framework, as described above). Frameworks display inverted control where the components provide services to the framework. The framework itself runs the algorithms, here the high level, templated sorting process, and call upon the services provided by the components to fill in the necessary processing pieces, e.g. the split and join procedures.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, My first collection. OpenStax CNX. Aug 03, 2009 Download for free at http://cnx.org/content/col10870/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'My first collection' conversation and receive update notifications?

Ask