Java program to sort an array of integers using bubble sort

Java program to sort an array of integers using bubble sort

Write a Java program to sort an integer array is one of the common Java coding interview questions. This question is asked to test candidate’s ability to implement sorting algorithm in Java. In this post we will see java program to sort an array of integers using bubble sort algorithm.

To sort an array of integers in Java, we can simply use the Arrays.sort() method which actually uses Quicksort algorithm to sort the elements. But the interviewer may ask you to implement the solution without using in-built API. So you need to write your own implementation.

Java program to sort array in ascending order using bubble sort

Below is the output of running the above program.

Java program to sort array in descending order using bubble sort

For descending order we just need to change the logic a[j] > a[j+1] to a[j] < a[j+1] in the above program. Here is the code.

Below is the output of running the above program.

The interviewer may ask to sort a list of integers(without using an API) instead of an array. You can refer the below program for the same.

Sort ArrayList without using collections.

Hope you find this post useful. If you have any comments, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.
One comment

Add Comment

Required fields are marked *. Your email address will not be published.