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 6 of 6

Thread: Basic Java Sorting Algorithm (Selection/Insertion) help

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic Java Sorting Algorithm (Selection/Insertion) help

    n my Computer Science I class in high school, we have to make an array with 100 int values with a range of 1-1000:

    public static void main(String[] args)
    int list []=new int [100];
    for(int i=0; i<list.length; i++) 
    {
    list[i]=(Math.random()*1000+1)//
    }

    but our teacher left us on our own to find out how to do Sorting Algorithms. He wants us to use the Selection or Insertion sorting algorithms to sort the array.

    If you could help me, that would be awesome


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Basic Java Sorting Algorithm (Selection/Insertion) help

    See Sorting algorithm - Wikipedia, the free encyclopedia which has links to the sorting algorithms you mentioned, each page of which has pseudo-code you can work from and, more importantly, descriptions to understand the algorithm.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Java Sorting Algorithm (Selection/Insertion) help

    public static void main(String[] args) {
        int list[]=new int[100];
    for (int i=0; i<list.length; i++)
        	{
        		list[i]=((int)(Math.random()*1000)+1);
        	}
     
    for (int i=0; i<list.length-1; i++)
    	{
    		for (int j=i+1; j<list.length; j++)
    			{
    				if (list[ i ] > list[ j ])
    					{
    						int temp = list[ i ];
    						list[ i ] = list[ j ];
    						list[ j ] = temp;
    					}
    			}
    	}

    Here is my code, all I get are symbols for output, what am I doing wrong?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Basic Java Sorting Algorithm (Selection/Insertion) help

    all I get are symbols for output,
    You will have to define what this means...what symbols? does it compile? Exceptions?

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Basic Java Sorting Algorithm (Selection/Insertion) help

    As you have mentioned that you need basic sorting technique,you could use Bubble Sort.Check out http://c-madeeasy.blogspot.com/2011/07/various-sorting-techniques

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Java Sorting Algorithm (Selection/Insertion) help

    If your professor asked to figure it out on your own, your professor is probably going to ask you to write some pseudo code on an exam. The right way to tackle this algorithm is to watch a youtube video of someone demonstrating the algorithm so you understand it. Once you understand the algorithms it shouldnt take you more than 30 minutes to implement a working program.

Similar Threads

  1. 2 Java methods and a sorting algorithm
    By Tiberius in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 26th, 2011, 03:41 PM
  2. [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
  3. [SOLVED] Sorting a vector using insertion
    By DM21 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 25th, 2010, 05:36 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

Tags for this Thread