I have been trying to implement Bubble Sort using simple static integer array in java. However there seems to be some problem.
class BubbleSort {
static int[] a = { 10, 8, 11, -6, 9 };
public void swap(int i, int k) {
if (a[i] == a[k])
return;
int temp;
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
public static void main(String[] args) {
BubbleSort bs = new BubbleSort();
for (int end = a.length - 1; end > 0; end--) {
for (int i = 0; i < end; i++) {
if (a[i] > a[i + 1])
bs.swap(i, i++);
}
}
for (int j = 0; j < a.length; j++)
System.out.println(a[j]);
}
}
i expect output -6,8,9,10 but the actual output is not sorted at all.it is showing 10,8,-6,9