Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


8,120 questions

11,301 answers

573 users

How to implement selection sort in Java

1 Answer

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 by avibootz
...