Solved

Logic Error

Posted on 2004-04-24
5
205 Views
Last Modified: 2010-03-31
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
Comment
Question by:Job4Suprmn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 92

Accepted Solution

by:
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

by:GrandSchtroumpf
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

by:rama_krishna580
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

by:Mayank S
Mayank S 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

by:GrandSchtroumpf
ID: 10913833
:°)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to execute a Python program and gather return output in Java 2 62
DTD and JAVA versions 1 55
Java import explained 4 47
Firefox and firebub 5 26
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question