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

Thread: Bubble Sort on double Linked List

  1. #1
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bubble Sort on double Linked List

    Hi guys,

    What's wrong with my code? I have a Link class and a Linked List class. The Link class contains one random number. The Linked List class creates a new list. The size of that list is up to the user of the program. With a methode calles insertMany(int howMany) the user can insert x-links in the list as many as you like. The follow code is my bubblesort.

    But my question the code sometimes sorts the list very well talking about 50000 links. Sometimes it stucks in a loop or the sorting takes longer as usual. If i enter lets say 20000 it always works but i need to get to 100000 and is not working so what can i change and what is wrong with my code?

        public void bubbleSort(){
            Link current = first;
            int index = 0;
     
            while(current != null && index <= howMany){
                    if(current.next == null){
                        index++;
                        current = first;   
                    }
                    else if(current.random < current.next.random){
                        current = current.next;
                    }   
                    else if(current.random > current.next.random){    
                        swap(current, current.next);  
                    }       
            }   
        }


        public void swap(Link c, Link cn){
            long temp = c.getValue();
            c.setValue(cn.getValue());
            cn.setValue(temp);     
        }
    System.out.println(" dream in code ");


  2. #2
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort on double Linked List

    anybody?
    System.out.println(" dream in code ");

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,484
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Bubble Sort on double Linked List

    Use a profiler to determine what's happening when the code appears to be stuck or seems to be taking too long to complete.

  4. #4
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort on double Linked List

    whats a profiler?
    System.out.println(" dream in code ");

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,484
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Bubble Sort on double Linked List

    Here's an example. There are others.

Similar Threads

  1. Sort and Merge Two Linked List
    By Loraeron in forum What's Wrong With My Code?
    Replies: 49
    Last Post: February 28th, 2013, 09:08 PM
  2. Please Help: Simple Double Linked List (remove, addBefore, )
    By yeohv in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2012, 12:58 PM
  3. Help with using iterator remove on double linked list
    By papated21 in forum Collections and Generics
    Replies: 3
    Last Post: October 18th, 2011, 09:22 PM
  4. Insertion Sort on a Doubly Linked List
    By Kaisshau in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 7th, 2011, 11:18 PM
  5. Pseudo code of insertion sort linked list
    By sungirl in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 23rd, 2010, 09:25 AM