Solved

# Logic Error

Posted on 2004-04-24
200 Views
I think I have a logic error in here but I can't figure out what it is, anyone have a clue?

The method is to sort an array of double values.
/** The method for sorting the numbers */
static void selectionSort(double[] list) {
for (int i = list.length - 2; i >= 1; i--) {
// Find the maximum in the list[0..i]
double currentMax = list[0];
int currentMaxIndex = 0;

for (int j = 1; j <= i; j++) {
if (currentMax < list[j]) {
currentMax = list[j];
currentMaxIndex = j;
}
}

// Swap list[i] with list[currentMaxIndex] if necessary;
if (currentMaxIndex != i) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
}
}
0
Question by:Job4Suprmn

LVL 92

Accepted Solution

objects earned 0 total points
ID: 10909801
>  list.length - 2

at 1st glance it would appear that should be  list.length - 1
0

LVL 30

Assisted Solution

GrandSchtroumpf earned 25 total points
ID: 10909830
And your swap needs to be inside the for loop:

static void selectionSort(double[] list) {
for (int i = list.length - 1; i >= 1; i--) {
// Find the maximum in the list[0..i]
double currentMax = list[0];
int currentMaxIndex = 0;

for (int j = 1; j <= i; j++) {
if (currentMax < list[j]) {
currentMax = list[j];
currentMaxIndex = j;
}

// Swap list[i] with list[currentMaxIndex] if necessary;
if (currentMaxIndex != i) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
}
}
0

LVL 23

Expert Comment

ID: 10911076
try this program logic...seudo code..

void sortArray()
{
for(i=0; i<MAX; i++)
{
for(j=i+1;j<MAX;j++)
{
sort(number[i],number[j]);

number[i]=a;
number[j]=b;
}
}
}

void shuffle()
{
for(i=0; i<MAX; i++)
{
j=(int)(Math.random()*MAX);

if(i!=j)
{
int temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}

R.K

0

LVL 30

Assisted Solution

mayankeagle earned 25 total points
ID: 10911219
>>  sort(number[i],number[j]);

BTW, where is this method in your code?

>> for(i=0; i<MAX; i++)

could be:

for ( i = 0 ; i < MAX - 1 ; i ++ )

Also, Job4Suprmn, your method is called selectionSort () but it doesn't actually implement selection-sort. The correct algo would be:

for ( int i = 0 ; i < array.length - 1 ; i ++ )
for ( int j = i + 1 ; j < array.length ; j ++ )
if ( array[i] > array[j] )
// swap them
0

LVL 30

Expert Comment

ID: 10913833
:°)
0

## Featured Post

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop: