I am writing a program that generates 10k random integers in the main class and then bubble-sorts it and returns the count of how many times the array was sorted. The problem is that even though I got the method to be public and return the count, it always shows up to means 0 after running it.
METHOD
public int bubbleSort(int bub[], int count){
int n = bub.length;
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(bub[j]>bub[j+1]){
int temp = bub[j];
bub[j] = bub[j+1];
bub[j+1] = temp;
count++;
}
return count;
}
void printArray(int bub[]){
int n = bub.length;
for(int i=0;i<n;i++){
System.out.print(bub[i] + " " );
}
}
}
MAIN CLASS
import java.util.Random;
class Main{
public static void main(String args[]){
Random rd = new Random();
Bubble ob = new Bubble();
int count=0;
int[] bub = new int[10000];
for(int i=0;i<bub.length;i++){
bub[i] = rd.nextInt(100);
}
ob.bubbleSort(bub, count);
System.out.println(count);
System.out.println("sorted array");
ob.printArray(bub);
System.out.println("number of times array was sorted " + count);
}
}