public class Main{
static void bubble_sort(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]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int array1[] ={10, 50, 30, 40, 20};
System.out.println("Before Bubble Sort");
for(int i=0; i < array1.length; i++){
System.out.print(array1[i] + " ");
}
System.out.println();
bubble_sort(array1);
System.out.println("After Bubble Sort");
for(int i=0; i < array1.length; i++){
System.out.print(array1[i] + " ");
}
}
}
Comments
Leave a comment