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 bubble sort in Java

1 Answer

0 votes
public class MyClass {
    static void bubble_sort(int[] arr) {  
        int len = arr.length;  int tmp = 0;  
        
        for (int i = 0; i < len; i++) {  
            for (int j = 1; j < len - i; j++) {  
                if (arr[j - 1] > arr[j]) {                              
                    tmp = arr[j - 1];                                   
                    arr[j - 1] = arr[j];                                   
                    arr[j] = tmp; 
                }
            }                   
        }           
    }
    public static void main(String args[]) {
        int arr[] = {2, 141, 3, 4, 21, 13, 30, 50};
       
        bubble_sort(arr);
       
        for(int i = 0; i < arr.length; i++) {                          
            System.out.print(arr[i] + " "); 
        }
    }
}


/*
run:

2 3 4 13 21 30 50 141 

*/

 



answered May 31, 2019 by avibootz

Related questions

1 answer 156 views
1 answer 201 views
1 answer 161 views
1 answer 99 views
1 answer 56 views
1 answer 69 views
1 answer 83 views
...