/*
Bubble sort is a Comparison based sorting
Algorithm.
We Compare The Array Element With Next or
Previous Of Array Element . If The Element Is Higher Or
Lower Based on Our Requirement Swap Two.
*/
class Bubble{
public static void main(String[] args){
int[] arr={7,6,5,4,3,2,1};
int n=arr.length;
int swap=1;
int j=0;
while(swap>0){
swap=0;
for(int i=0;i<n-1;i++){
if(arr[i]>arr[i+1])
{
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
swap++;
}
}
}
for(int i=0;i<n;i++){
System.out.println(arr[i]);
}
}
}
output:
1
2
3
4
5
6
7
Comments
Post a Comment