Bubble Sort
public class BubbleSort {
public void Sort() { //method scope start
int[] salary = { 5, 1, 12, -5, 16 };
for (int j = 0; j < salary.length; j++){ //first for loop
for (int i = 0; i < salary.length - 1; i++) { //second for loop
int n1 = salary[i];
int n2 = salary[i + 1];
if (n1 > n2) { //if condition start
int temp = n1;
n1 = n2;
n2 = temp;
} //if condition end
salary[i] = n1;
salary[i + 1] = n2;
} //second for loop end
System.out.println("");
for (int i = 0; i<salary.length; i++) {
System.out.print(salary[i] + ",");
}
}// first for loop end
} //method end
} //class end
Reference : http://www.java2novice.com/java-sorting-algorithms/bubble-sort/
Comments
Post a Comment