Q:

Java Program to implement Selection Sorting

0

Program to implement Selection Sorting

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public class demo1 
{  
  public static void selectionSort(int[] arr)
    {  
      for (int a = 0; a < arr.length - 1; a++)  
        {  
            int res = a;  
            for (int b = a + 1; b < arr.length; b++)
                {  
                if (arr[b] < arr[res])
                   {  
                    res = b; 
                   }  
                }  
            int smallerNumber = arr[res];   
            arr[res] = arr[a];  
            arr[a] = smallerNumber;  
        }  
    }       
   public static void main(String array[]){  
   int[] array1 = {8,43,2,143,6,75,24};  
   System.out.println("Before Selection Sort");  
   for(int a:array1)
      {  
        System.out.print(a+" ");  
      }  
        System.out.println();  
          
        selectionSort(array1);  
         
        System.out.println("After Selection Sort");  
        for(int a:array1)
          {  
            System.out.print(a+" ");  
          }  
   }  
} 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now