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,990 questions

51,935 answers

573 users

How to implement selection sort in Java

2 Answers

0 votes
import java.util.Random;
 
public class MyClass {
    public static int[] selection_sort(int[] arr) {
          
        for (int i = 0; i < arr.length - 1; i++) {
            int minindex = i;
            
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minindex]) {
                    minindex = j;
                }
            }
       
            int tmp = arr[minindex];  
            arr[minindex] = arr[i];
            arr[i] = tmp;
        }
 
        return arr;
    }
 
    public static void main(String args[]) {
          
        int[] arr = new int[15];
         
        Random randomGenerator = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = randomGenerator.nextInt(1000) + 1;
        }
         
        arr = selection_sort(arr);
         
        for (int i:arr) {
            System.out.print(i + " ");
        }
    }
}
 
 
 
/*
  
run:
  
147 152 174 302 317 324 386 405 502 533 658 674 855 946 994  
  
*/

 



answered May 13, 2018 by avibootz
edited Feb 19, 2024 by avibootz
0 votes
public class MyClass {
    static void selection_sort(int[] arr) {  
        int len = arr.length;
        
        for (int i = 0; i < len - 1; i++)  {  
            int min_i = i;  

            for (int j = i + 1; j < len; j++) {  
                if (arr[j] < arr[min_i]) {                      
                   min_i = j;   
                }              
            }  

            int min = arr[min_i];               
            arr[min_i] = arr[i];              
            arr[i] = min;          
       }      
    }

    public static void main(String args[]) {
        int arr[] = {2, 141, 3, 4, 21, 13, 30, 50};
         
        selection_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 Feb 19, 2024 by avibootz
edited Feb 19, 2024 by avibootz

Related questions

1 answer 204 views
1 answer 178 views
1 answer 202 views
202 views asked May 12, 2018 by avibootz
1 answer 228 views
228 views asked May 12, 2018 by avibootz
1 answer 176 views
176 views asked May 12, 2018 by avibootz
1 answer 176 views
176 views asked May 12, 2018 by avibootz
1 answer 186 views
186 views asked May 12, 2018 by avibootz
...