Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to implement the bubble sort algorithm in Java

1 Answer

0 votes
public class MyClass {
    static void bubbleSort(int[] arr) {
      int len = arr.length;
      
      for (int i = 0; i < len - 1; i++) {
         for (int j = 0; j < len - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
            }
         }
      }
    }
    public static void main(String args[]) {
        int[] arr = {6, 8, 0, 3, 1, 6, 4};
        
        bubbleSort(arr);
 
        for (int n : arr) {
            System.out.printf("%2d", n);
        }
    }
}



/*
run:

 0 1 3 4 6 6 8

*/

 



answered Mar 25, 2021 by avibootz

Related questions

1 answer 99 views
1 answer 56 views
1 answer 69 views
1 answer 83 views
1 answer 84 views
1 answer 123 views
1 answer 181 views
...