How to Implement Bubble Sort in Java


In this example, we will learn the method of bubble sort in Java.

Source Code

package com.beginner.examples;

import java.util.Arrays;

public class BubbleSortExample {
	
	public static void main(String[] args) {
	int[] arr = {10,34,235,22,1,93};
	int l = arr.length;
	for(int i=0;i<l-1;i++)
	{
		for(int j=0;jarr[j+1])
			{
				int temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
		}
	} 
	System.out.println(Arrays.toString(arr));
  }
}

Output:

[1, 10, 22, 34, 93, 235]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments