Results 1 to 8 of 8
  1. #1
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default why doesnt my insertion sort method not work?

    Java Code:
    public static void insertionSort(int[] list)
        {
            int j, key;
            
            for(int i = 1; i < list.length; i++)
            {
                key = list[i];
                j = i - 1;
                
                while(j >= 0 && list[j] > key);
                {
                    list[j + 1] = list[j];
                    j--;
                }
                
                list[j + 1] = key;
            }

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    what are the errors? does list.length need to be list.length()? How is it supposed to work? A little more info please.

  3. #3
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    I'm using BlueJ and whenever it gets to the while loop it just loads without doing anything.

    so i have to quit bluej or it will load forever
    Last edited by Jeremy8; 11-15-2009 at 02:22 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Jeremy8 View Post
    I'm using BlueJ and whenever it gets to the while loop it just loads without doing anything.

    so i have to quit bluej or it will load forever
    IDE (as you say BlueJ) have nothing to do with your code. What you going to go exactly? Sorting the int array? Then how did you check that the code didn't work, display the value to the console or pass to another method? I cannot either one you did in the code.

  5. #5
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    IDE (as you say BlueJ) have nothing to do with your code. What you going to go exactly? Sorting the int array? Then how did you check that the code didn't work, display the value to the console or pass to another method? I cannot either one you did in the code.
    i have a main method

    Java Code:
    public class List
    {
        public static void main (String[] args)
        {
            int[] list = new int[10];
            Random generator = new Random();
            
            for(int i = 0; i < list.length; i++)
            {
                list[i] = generator.nextInt(90);
                System.out.print(list[i] + " ");
            }
            
            Sorting.insertionSort(list);
            System.out.println("");
            
            for(int i = 0; i < list.length; i++)
            {
                System.out.print(list[i] + " ");
            }
        }
    }
    it works with a selection sort method i made...

    all my sorting methods are in the class called Sorting.
    Last edited by Jeremy8; 11-15-2009 at 03:52 AM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In that sorting method,

    Java Code:
    while (j >= 0 && list[j] > key);
    while loop is an empty statement. Did you notice that semicolon at the end?

  7. #7
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    ohhhhhhhhh shhhhhhh-

    ._________.

    i mean

    thankyou :o

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 10:31 PM
  2. [SOLVED] Insertion Sort in Linked List
    By taylorp in forum New To Java
    Replies: 10
    Last Post: 03-27-2009, 01:34 AM
  3. Replies: 3
    Last Post: 01-26-2009, 01:20 AM
  4. Insertion Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:41 PM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:26 PM

Posting Permissions

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