3 Replies Latest reply on Jan 25, 2009 11:26 PM by 843785

# How to change Bubble+Selection+Insertion Sorts to Sort String Values?

Hello,

I'm curious as to how I would modify these three algorithms to sort string values instead of integer values?
``````public void bubbleSort()
{
int out, in;

for(out=nElems-1; out>1; out--)   // outer loop (backward)
for(in=0; in<out; in++)        // inner loop (forward)
if( a[in] > a[in+1] )       // out of order?
swap(in, in+1);          // swap them
}  // end bubbleSort()

//--------------------------------------------------------------
public void selectionSort()
{
int out, in, min;

for(out=0; out<nElems-1; out++)   // outer loop
{
min = out;                     // minimum
for(in=out+1; in<nElems; in++) // inner loop
if(a[in] < a[min] )         // if min greater,
min = in;               // we have a new min
swap(out, min);                // swap them
}  // end for(out)
}  // end selectionSort()

//--------------------------------------------------------------
public void insertionSort()
{
int in, out;

for(out=1; out<nElems; out++)     // out is dividing line
{
long temp = a[out];            // remove marked item
in = out;                      // start shifts at out
while(in>0 && a[in-1] >= temp) // until one is smaller,
{
a[in] = a[in-1];            // shift item to right
--in;                       // go left one position
}
a[in] = temp;                  // insert marked item
}  // end for
}  // end insertionSort()``````
• ###### 1. Re: How to change Bubble+Selection+Insertion Sorts to Sort String Values?
Since you've cross-posted this question a bunch of places, I'm going to assume that someone else has answered you elsewhere and will not answer you here. No one likes wasting their time, do they?
• ###### 2. Re: How to change Bubble+Selection+Insertion Sorts to Sort String Values?
This question has not been answered, Is it not good practice to post your question on multiple forums?
• ###### 3. Re: How to change Bubble+Selection+Insertion Sorts to Sort String Values?
Cole1988 wrote:
This question has not been answered, Is it not good practice to post your question on multiple forums?