Bubble Sort
Bubble sort is a sorting algorithm, in every loop an item either max/min according to program is shifted to the end of array. Similarly the second max/min to second last position, O(n^2).
public class BubbleSort{
public static void main(String args[]){
int num[]={44,22,33,34,25}; //any arbit array of number, u can change its length
//Sorting Algorithm Start
for(int i=0; i<(num.length-1); i++){
for(int j=0; j<(num.length-1); j++){
if(num[j]>num[j+1]){ //swapping if number is greater than the next number
int temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
//Sorting Algorithm Ends
System.out.print("Sorted: ");
for(int i=0; i<num.length; i++){
System.out.print(num[i] + " ");
}
}
}
Download Program


