bubble sorting algorithm with code......
here is the code of bubble sorting ...
public class bubblesort {
public static void printarray(int arr[]){
for(int i=0; i< arr.length; i++){
System.out.print(arr[i]+" ");
}
System.out.println("make face happy the array is sorted ->>");
}
public static void main(String[] args) {
int arr[] = {7, 8, 3, 1, 2};
//time complexity=O(n^2)
//bubble sorting
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
//swapping
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printarray(arr);
}
}
.png)
.png)
Comments
Post a Comment