0 Replies - 877 Views - Last Post: 11 May 2013 - 10:55 PM Rate Topic: -----

#1 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 12-May 09

Bubble sort failure

Posted 11 May 2013 - 10:55 PM

so i entered my self into a contest where i had to make several programs. The program that gave me the most trouble was a sorting program. I had to use an sorting algorithm, the only problem was ive never used an sorting algorithm and even when i asked my java teacher for help she had to ask another person lol. I decided i wasn't going to look on line for code examples all i did was look on wiki for how they are supposed to work and the name of the sort i chose bubble and this is what i came up with after 5 hours of work apsolutly nothing. I was able to sorta get it to work in an infinate loop. I looked online for a solutine after the contest and found that i was being stupid and making it more complicated than it needed to be here is my bad code

    

public void scoreSort()
    {  
        float compareOne = 0;
        float compareTwo = 0;
        boolean fin = false;
        SampleHighScore sample=null;
        SampleHighScore samples=null; 
        int count = 0;
        int indexa = 1;
        while(!fin){
            for(int index = 0; index < scoreList.size();index++){
                sample = scoreList.get(index);
                samples = scoreList.get(indexa);
                compareOne = sample.getScore();
                compareTwo = samples.getScore();
                if(compareOne < compareTwo){
                    fin = true;
                }else{
                    scoreList.remove(indexa);
                    scoreList.remove(index);
                    scoreList.add(index, samples);
                    scoreList.add(index+1, sample);
                    fin =false;
                    break;
                }
                if (index == scoreList.size() -1){
                    indexa = scoreList.size();
                }else{
                    index++;
                }
                for(int indexs = 0;indexs < scoreList.size();indexs++){
                    float ind =0;
                compareOne = scoreList.get(index).getScore();
                compareTwo = scoreList.get(index).getScore();
                if(compareOne < compareTwo){
                    fin =true;
                }else{
                    fin = false;
                    break;
                }
              if (indexs == scoreList.size() -1){
                    ind = scoreList.size();
                }else{
                    index++;
                }
            }
        }
    }
}
}



Is This A Good Question/Topic? 1
  • +

Page 1 of 1