Answers for "selection sort jav"

3

Selection sort java

// example on selection sort java
public class SelectionSortInJava
{
   void toSort(int[] arrNum)
   {
      int number = arrNum.length;
      for(int a = 0; a < number - 1; a++)
      {
         // finding minimum element
         int minimum = a;
         for(int b = a + 1; b < number; b++)
         {
            if(arrNum[b] < arrNum[minimum])
            {
               minimum = b;
            }
         }
         // swapping minimum element with first element
         int temp = arrNum[minimum];
         arrNum[minimum] = arrNum[a];
         arrNum[a] = temp;
      }
   }
   // printing array
   void displayArray(int[] arrPrint)
   {
      int num = arrPrint.length;
      for(int a = 0; a < num; ++a)
      {
         System.out.print(arrPrint[a] + " ");
      }
      System.out.println();
   }
   public static void main(String[] args)
   {
      SelectionSortInJava obj = new SelectionSortInJava();
      int[] arrInput = {5, 4, -3, 2, -1};
      obj.toSort(arrInput);
      System.out.println("After sorting : ");
      obj.displayArray(arrInput);
   }
}
Posted by: Guest on December-10-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language