---
title: "Write a program to sort an array of integers using the bubble sort algorithm."  
description: "Write a program to sort an array of integers using the bubble sort algorithm."  
author: "Revati S Misra"  
published: 2023-04-19  
updated: 2023-04-24  
canonical: https://www.mindstick.com/forum/157933/write-a-program-to-sort-an-array-of-integers-using-the-bubble-sort-algorithm  
category: "java"  
tags: ["java", "programs"]  
reading_time: 2 minutes  

---

# Write a program to sort an array of integers using the bubble sort algorithm.

Write a [program to sort](https://www.mindstick.com/forum/158770/write-a-python-program-to-sort-a-list-of-objects-based-on-a-specific-property) an [array of integers](https://www.mindstick.com/forum/157932/write-a-program-to-find-the-missing-number-in-an-array-of-integers) using the bubble sort algorithm.

## Replies

### Reply by Aryan Kumar

```java
public class BubbleSort {
   public static void main(String[] args) {
       int[] arr = {5, 2, 8, 3, 1, 7, 9, 4, 6}; // array to be sorted

       // bubble sort algorithm
       for (int i = 0; i < arr.length - 1; i++) {
           for (int j = 0; j < arr.length - i - 1; j++) {
               if (arr[j] > arr[j + 1]) {
                   // swap arr[j] and arr[j+1]
                   int temp = arr[j];
                   arr[j] = arr[j + 1];
                   arr[j + 1] = temp;
               }
           }
       }

       // print the sorted array
       for (int i = 0; i < arr.length; i++) {
           System.out.print(arr[i] + " ");
       }
   }
}
```

This [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) uses the bubble sort algorithm to sort the input [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) of integers. The algorithm works by repeatedly swapping adjacent elements if they are in the wrong order, until the array is sorted. The program uses two nested loops to compare adjacent elements and swap them if necessary. The sorted array is then printed to the console. In the example array above, the sorted array would be: 1 2 3 4 5 6 7 8 9.

\


---

Original Source: https://www.mindstick.com/forum/157933/write-a-program-to-sort-an-array-of-integers-using-the-bubble-sort-algorithm

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
