Thursday, 19 February 2015

Bubble Sort

public class BubbleSort {
public static void main(String[] args) {

int[] a = { 5, 1, 4, 2, 8 };

int temp = 0;

for (int i = 0; i < a.length; i++) {

for (int j = 1; j < a.length - i; j++) {

if (a[j - 1] < a[j]) {
temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}

}

}
for (int i1 : a) {
System.out.println(i1);
}

}
}

No comments:

Post a Comment