Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: selection sorting help?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default selection sorting help?

    Hi guys, I'm trying to sort a string array that has 100 words using the selection sort algorithm. Here's my code so far:
    import java.util.*;
     
    class arraysort{
    	public static void main(String args[]){
    		int index,number=0;			
    		Scanner input = new Scanner(new File("wordlist.txt"));
    		String words[] = new String[100];
    		String max,k;
     
    		for(int i=0;i<words.length;i++){
    		   number = (int)(Math.random()*10);
    		   for (int j=0;j<number;j++){
    		     words[j] = input.nextLine();
    		}
     
    		for (int i=0;i<99;i++){
               {
    		     max = a[i];
    		     for (int j=i+1;j<100;j++)
    			   {
    			     if (a[j].compareTo(max)>0)
    				   {
    				     max = a[j];
    					 index = j;
    				   }
    			   }
    			 k = a[i];
    			 a[i] = a[index];
    			 a[index] = k;
    		    }
    		}
     
     
    	}
    }
    Is my code correct?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: selection sorting help?

    Can you test it and let us know if you have questions, errors, problems, etc.?

Similar Threads

  1. Help with Course Selection
    By randy81 in forum The Cafe
    Replies: 4
    Last Post: August 14th, 2013, 03:51 AM
  2. Basic Java Sorting Algorithm (Selection/Insertion) help
    By Arte7 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 22nd, 2011, 01:38 PM
  3. [SOLVED] Selection Sorting a text array
    By ComputerSaysNo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 30th, 2011, 06:56 PM
  4. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM
  5. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM