Bubble Sorting in java

Bubble Sorting:
                           Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. Example: First Pass: ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.



And here is the program of bubble sort.

package com.ritesh;

public class bubble_sort {
public static void main(String[] args) {
int []arr={4,56,742,3,86,1,-7,33,976,1256,324};
int n=arr.length;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(arr[j+1]<arr[j]){
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
for(int os:arr ){
System.out.print(os+"\t");
}
}
}

Comments