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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Location
    Canada
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    selection sort & insertion sort sort methods? - java

    I have been working on the code for an alpha sort file and have become stumped. I need to incorporate both an insertion sort & selection sort method into my code before it will run.

    I attached the file I have been working on and it runs on Bluej with Java JDK. I would apretiate if you could take a look at it. If you would prefer not to download my file I have posted my code that I have been working on below.

    I am not familiar with the structure of an insertion sort or a selection sort mothod. I also am not clear on the point in which these methods would need to be placed in the file.

    Code:
    import java.io.*;
    import java.util.*;
    
    public class Words
    {
    ArrayList<String> words;
    
    public Words()
    {
    words = getData("wordlist.txt");
    }
    
    public void displayWords()
    {
    for(int i=0; i<words.size(); i++) {
    System.out.println(words.get(i));
    }
    }
    
    public ArrayList<String> getData(String filename)
    {
    ArrayList<String> list = new ArrayList<String>();
    File myFile = new File(filename);
    if(myFile.exists() && myFile.length()>0) {
    try {
    BufferedReader in = new BufferedReader( new FileReader(myFile) );
    String word = in.readLine();
    while( word != null ) {
    list.add(word);
    word = in.readLine();
    }
    } catch( Exception e )
    {} 
    }
    return list;
    }
    }
    Attached Files Attached Files

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    This is the JavaSCRIPT forum.

    Your question is about JAVA.

    About the only thing the two languages have in common are the letters J-A-V-A at the start of their names.

    By the way: Google is your friend. Clearly this is homework, so try googling for how the two kinds of sorts work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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