Home » Java » Selection Sort Algorithm with full code – java

Selection Sort Algorithm with full code – java

public class Sorts {
    public static void main(String[] args) {

        int[] arr1 = {9,14,3,2,43,11,58,22}; 
   
        System.out.println("Before Insertion Sort");  
  
        for(int i:arr1){    
            System.out.print(i+" ");    
        }  
  
        System.out.println();    
           
        //sorting array using selection sort
        selectionSort(arr1);    
       
        System.out.println("After selection Sort");    
        for(int i:arr1){    
            System.out.print(i+" ");    
        } 
    }
    
    public static void selectionSort(int[] arr){  
        for (int i = 0; i < arr.length - 1; i++)  
        {  
            int index = i;  
            for (int j = i + 1; j < arr.length; j++){  
                if (arr[j] < arr[index]){  
                    index = j;//searching for lowest index  
                }  
            }  
            int smallerNumber = arr[index];   
            arr[index] = arr[i];  
            arr[i] = smallerNumber;  
        }  
    }
}
Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*