Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Location
    California
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using arrays and methods in java

    I am stuck on a java programming assignment for my first-semester java programming course. I was given a code for a method which sorts an array of numbers by finding the smallest number and swapping it with the first number in the array. (I've put the given code in below). I am supposed to rewrite it to find the largest number and swap it wth the last number in the array, and write a test program that reads in ten double numbers, invokes the method, and displays the sorted numbers.
    I tried to rewrite the code and I'm not sure whether what I did will work (I may have only gotten it to swap the largest number in the array with the first number, rather than the last). But I'm mostly having trouble with writing the program around it. The way I've got it right now, when I try to compile the .java file, I'm getting 9 errors that all say "cannot find symbol" for different places all through the code. (The program isn't completely written yet either, because I haven't put in the part that will print the sorted array. I wanted to get the part I have done working before I make the program more complicated.)

    Here is the code I was given:
    Code:
    public class SelectionSort {
      public static void selectionSort(double[] list) {
        for (int i = 0; i < list.length - 1; i++) {
          //find the minimum in the list
          double currentMin = list[i];
          int currentMinIndex = i;
    
          for (int j = i + 1; j < list.length; j++)) {
            if (currentMin > list[j] {
              currentMin = list[j];
              currentMinIndex = j;
            }
          }
    
          //swap list[i] with list[currentMinIndex] if necessary
          if (currentMinIndex != i) {
            list[currentMinIndex] = list[i];
            list[i] = currentMin;
          }
        }
      }
    }

    And here is my program's code:
    Code:
    import java.util.Scanner;
    
    public class Exercise6_17 {
      public static void main(String[] args) {
        
        double[] list = new double[10];
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("Enter " + list.length + " values: ");
        for (int i = 0; i < list.length; i++)
          list[i] = input.nextDouble();
        selectionSort(list);
      }
    
    
        public static void selectionSort() {
          for (int i = 0; i < list.length - 1; i++) {
            //find the maximum in the list[i...list.length-1]
            double currentMax = list[i];
            int currentMaxIndex = i;
      
            for (int j = i + 1; j < list.length; 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;
            
          }
        }
      }
    }
    Can you guys help?
    Last edited by Fou-Lu; 07-10-2010 at 05:18 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There isn't a lot we can do off the bat in regards to code since this is an assignment.
    First priority will be fixing syntax errors and missing method parameters. Code that can't compile is useless.
    Second will be analysing a selection sort. You need to know what it does in order to alter it. Use paper and a pen to manually evaluate each step.
    Next, alter so it will sort descending instead of ascending. To do so you only need to change how an object compares itself to another. You want to swap if an item is > or >= to another instead of <.
    > and< create stable sorts, while <= and >= create instable sorts - selection sort is undefined for stability, so you can choose.
    Sorting is all about comparisons.

    If your knowledge is high enough, and you're allowed to modify that selection sort class, I'd rewrite an overload that takes a comparator. Then you just need two write two 1 line anonymous classes and you can dynamically switch the comparator.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Location
    California
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your suggestions. I did get it figured out on my own eventually though.

    This is how it ended up:
    import java.util.Scanner;

    public class Exercise6_17 {
    public static void main(String[] args) {

    double[] list = new double[10];
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print("Enter " + 10 + " values: ");
    for (int i = 0; i < 10; i++)
    list[i] = input.nextDouble();
    selectionSort(list);
    }

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

    for (int j = i - 1; j > (-1); 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;
    }
    }
    for (int k = 0; k < 10; k++)
    System.out.println(list[k]);
    }
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •