Back to Problem Solutions forum
Hello, I am having some trouble solving the bubble in array problem. I used the code which correctly solved the previous problems, 17 array checksum and 27 bubble sort, but I cannot get the same code to give correct answers. The swaps as well as the checksum value come out incorrect every time.
The code for checksum portion:
public static long checksum(ArrayList<Long> values){
int seed = 113;
int limit = 10000007;
int len = values.size();
long result = 0;
for(int i = 0; i < len; i++){
long n = values.get(i);
result = ((n + result) * seed)%limit;
}
return (result);
}
The code for bubble sort:
boolean swapped = true;
int passes = 0;
while(swapped){
swapped = false;
for(int i = 0; i < values.size()-1; i++){
if(values.get(i) > values.get(i+1)){
long temp = values.get(i);
values.set(i, values.get(i+1));
values.set(i+1, temp);
swapped = true;
swaps++;
}
}
passes++;
}
Please point me in the right direction for what I'm doing wrong. While I wait, I will solve a different problem.
Hello,
In 'bubble in array' you have to browse through the line only once, and do the necessary swaps.
In 'bubble sort' you browse the line as many times as necessary until it is perfectly sorted and won't budge (as it should).
Hope it helps.
Thank you for the clarification, that solved the problem.
emmm,I want to delete my review.