I am trying to write a selection sort that uses a switch statement to determine whether it should be sorted in ascending or descending order. I have timed the process and it is twice the time of the code without the switch statement and even longer then the bubblesort of the same #'s. How can I rewrite this to get back to actual speed...thanx in advance:
public void selectionSort(char sequence)
{
int out, in, min;
for(out=0; out<nElems-1; out++) // outer loop
for(in=out+1; in<nElems; in++) // inner loop
switch (sequence) {
case 'A':
min = out;
if(a[in] < a[min] ) // if min greater,
min = in; // we have a new min
swap(out, min); // swap them
break;
case 'D':
min = out;
if (a[in] > a[min] )
min = in; // we have a new min
swap(out, min); // swap them
break;
} // end switch
} // end selectionSort()
public void selectionSort(char sequence)
{
int out, in, min;
for(out=0; out<nElems-1; out++) // outer loop
for(in=out+1; in<nElems; in++) // inner loop
switch (sequence) {
case 'A':
min = out;
if(a[in] < a[min] ) // if min greater,
min = in; // we have a new min
swap(out, min); // swap them
break;
case 'D':
min = out;
if (a[in] > a[min] )
min = in; // we have a new min
swap(out, min); // swap them
break;
} // end switch
} // end selectionSort()