This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/3/19

Sorting outcome problem

JasonZhu JasonZhu

2014/3/19

#
    public void gruntSortList()
    {
        long startTime = System.nanoTime();
        int pos;
        int[] tempList = new int[list.length];        
        for(int i=0;i<tempList.length;i++){
            pos = 0;
            for(int j=i;j<list.length;j++){
                if(list[j]<list[pos]){
                    pos=j;
                }
            }
            tempList[i]=list[pos];
            list[pos] = maxValue+1;
        }
        for(int i=0;i<list.length;i++){
            list[i] = tempList[i];
        }
        long endTime = System.nanoTime();
        System.out.println("Grunt Sort");
        System.out.println("Completed in "+(endTime-startTime)+" nanoseconds");
    }
If my input is: 4 8 8 4 10 1 6 6 3 7 My output is: 1 3 4 4 6 6 7 11 11 11 Can someone help me figure out the problem? Thanks in advance!
danpost danpost

2014/3/19

#
Oh, this was so close. It is mainly how you handle the 'pos' variable. (1) change line 4 to : int pos = 0; (2) remove line 7 ( pos = 0; ) Then changing the starting value of 'j' in the 'for' loop on line 8 to '0' should do it.
JasonZhu JasonZhu

2014/3/20

#
Sorry for the late reply, I have it working now. Thanks once again danpost!
JasonZhu JasonZhu

2014/3/20

#
Hey danpost, just out of curiosity, how long have you been coding for? And what is your career?
danpost danpost

2014/3/20

#
JasonZhu wrote...
Hey danpost, just out of curiosity, how long have you been coding for?
I will say this -- 'probably close to twice as long as you have years -- but, all for fun'. Only three years in Java.
JasonZhu JasonZhu

2014/3/20

#
That is amazing how much knowledge you have about coding.
You need to login to post a reply.