Categories
Algorithm Algorithms & Design Sort

Bubble Sort


public class BubbleSort {

	public static void main(String[] args) {

		int arr[] = {21,12,14,1,26,34,67,45,0};
		int temp;
		for(int i=0;i<arr.length;i++){
			
			for(int j=0;j<(arr.length-i-1);j++){
				
				if(arr[j]>arr[j+1]){
					temp = arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=temp;
				}
			}
		}
	
		printArray(arr);
		
	}

	public static void printArray(int array[]){
		
		System.out.println("Printing sorted array ::");
		for(int i=0;i<array.length;i++)
			System.out.println(array[i]);
		
	}
}

Output :


Printing sorted array ::
0
1
12
14
21
26
34
45
67

Leave a comment

Design a site like this with WordPress.com
Get started