Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Sorting Java.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 logitech1   Members   -  Reputation: 121

Like
0Likes
Like

Posted 04 June 2014 - 01:21 AM

Question guys, need some help about sorting using arraylist, i don't know why it doesn't sort at all.. >_< sorry for the post.

 

public class Driver2 {
    
    static Random r = new Random();

    public static void main(String[]args) {
    
        ArrayList<Integer> list = new ArrayList<Integer>();
        
        for(int i = 0 ; i < 3 ; i++) {
            list.add(rollDice());
        }
        
        for(Integer g : list)
        System.out.println(g);
        
        for(int index = 0 ; index < list.size() ; index++) {
            for(int i = 1 ; i < list.size() - 1 ; i++) {
                
                if(list.get(index) < list.get(i)) {
                    int temp = list.get(index);
                    list.remove(index);
                    list.add(index , list.get(i));
                    list.remove(i);
                    list.add(i, temp);
                }
            }
        }
        
        for(Integer g : list)
            System.out.println(g);
        
    }
    
    public static int rollDice() {
        
        int temp;
        temp = (1 + r.nextInt(6));
        
        return temp;
    }
}



Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4436

Like
0Likes
Like

Posted 04 June 2014 - 02:31 AM

First, what does "it doesn't sort at all" even mean? Is there no change? Is there change but it's not the expected result? Sample output might help.

Then, what algorithm are you trying to implement? I don't recognize it (possibly because of the horrible code formatting) and the quadratic runtime does not look like something often-used. Is that a well-known algorithm or something you think 'should work'. If the latter, have you tried to test it on paper if it should work at all.
I would also think it's highly unlikely you really want to use ArrayList.add() and ArrayList.remove() while sorting. It is unnecessary (in general you want to swap elements) and will cause hell with indices.
Also note, if implementing the sorting algorithm is not the purpose of this, there are sorting functions in java.util.Arrays.

#3 Ashaman73   Crossbones+   -  Reputation: 8001

Like
0Likes
Like

Posted 04 June 2014 - 02:33 AM

If this is not homework, then use the standard sort of java (eg Collections.sort).

 

If this is homework, and your task is to write bubblesort,then take a closer look here:

        for(int index = 0 ; index < list.size() ; index++) {
            for(int i = 1 ; i < list.size() - 1 ; i++) {


Edited by Ashaman73, 04 June 2014 - 02:34 AM.


#4 Wooh   Members   -  Reputation: 653

Like
0Likes
Like

Posted 04 June 2014 - 02:47 AM

The swap code also doesn't work. Take a look at these lines:
list.remove(index);
list.add(index , list.get(i));
Remove will shift all elements after the remove element so that they are stored at an index that is one smaller. If i > index then list.get(i) will not give you the same number as it did before you called remove.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS