public class Program {
public void start() {
int[] nums = { 35, 90, 5, 45 };
for(int i = nums.length; i > 0; i--){
bubble(nums, i);
}
for (int i = 0; i < nums.length; i++){
System.out.println(nums[i]);
}
}
private void bubble(int[] a, int n){
int temp = 0;
for (int i = 0; i < (n-1); i++) {
if (a[i] > a[i+1]) {
swapElements(a, i, i+1);
}
}
}
private void swapElements(int[] a, int pos1, int pos2){
int temp = a[pos1];
a[pos1] = a[pos2];
a[pos2] = temp;
}
}
I KNOW The answers :) But i want to know how to solve like how does it work through? which steps go first and so forth and so forth? And can you explain whats with the " bubble (nums, i); " and whats its purpose and why is there a method of it " private void bubble(int [ ] a, int n) " Like explain please if you can.
The answers :
5
35
45
90
Like how does it work out in that order?
Please need Help ! :)
Thanks
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
I'm guessing your trying to sort an array of numbers and the private void bubble(int[] a,int n) is implementing the BubbleSort algorithm, but it's not correct
This is how the BubbleSort algorithm works: http://en.wikipedia.org/wiki/Bubble_sort