xxxxxxxxxx
<a href="#" onClick="history.go(-1)">Go Back</a>
xxxxxxxxxx
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
xxxxxxxxxx
package com.SortingAlgorithm;
import java.util.Arrays;
public class Bubblesort {
public Bubblesort() {
}
public static void main(String[] args) {
int[] x = new int[]{5, 1, 4, 12, 7};
int n = x.length;
System.out.println("Original Array list is: " + Arrays.toString(x));
for(int i = 0; i < n - 1; ++i) {
// swapping of element occurs here
if (x[i] > x[i + 1]) {
int temp = x[i];
x[i] = x[i + 1];
x[i + 1] = temp;
}
}
System.out.println("The Sorted list is: " + Arrays.toString(x));
}
}
xxxxxxxxxx
dism /online /set-edition:serverstandard /productkey:VDYBN-27WPP-V4HQT-9VMD4-VMK7H /accepteula
xxxxxxxxxx
public void bubbleSort(java.util.ArrayList aList) {
int n = aList.size();
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (aList.get(j - 1) > aList.get(j)) {
//swap elements
temp = aList.get(j - 1);
aList.set(j-1, aList.get(j));
aList.set(j, temp);
}