Bubble in Array java

Back to Problem Solutions forum

knightdeadd     2016-02-29 04:35:51

вот собственно код. метод checkSum взят из problems #17, и впринципе не может считать не правильно. массив отсортирован, проверял выводом каждого элемента...не пойму что не так. у меня даже счетчик на колличество перестановок не совпадает. вот что выводит при сдаче:

here transfer from translate.ru of my problem. here actually code. the checkSum method is taken from problems #17, and in principle can't consider not correctly. the massif is sorted, checked a conclusion of each element... I won't understand that not so. at me even the counter on the number of shifts doesn't coincide. here that removes at delivery:

Expected answer was: 38 4039243

Your answer was: 41 9097419

help!please!:)

    for(int j=0;j<list.size();j++){  
        count++;
        for(int i=0;i<list.size()-1-j;i++){
            if(list.get(i)>list.get(i+1)){
                c=list.get(i);
                list.set(i,list.get(i+1));
                list.set(i+1,c);
            }
        }
    }
    System.out.print(count+" ");

       System.out.print(checkSum(list));

}
static long checkSum(ArrayList<Integer> s){
Motyga_free     2016-02-29 11:18:50
User avatar

Прочитайте внимательно условие задачи, вы написали, что ваш массив отсортирован, но сортировать его не нужно. Только пройтись по нему, и если число слева больше числа справа поменять их местами и занести в этот if переменную count, Собственно говоря это и есть перестановка.

knightdeadd     2016-03-01 02:43:02

спасибо!не сразу понял о чем вы говорите, но потом осенило, что надо сделать лишь один проход, а не до полной сортировки) сдал:)добра вам)

Please login and solve 5 problems to be able to post at forum