Answers for "Implement Selection Sort and print the index which gets swapped at each step."

5

selection sort in java

public static void SelectionSort(int[] arr)
{
  int small;
  for (int i = 0; i <arr.length - 1; i++)
  {
    small = i;
    for (int j = i + 1; j < arr.length; j++)
    {
      //if current position is less than previous smallest
      if (arr[j] < arr[small])
      {
        small = j;
        
        //swap values
        int temp = arr[i];
        arr[i] = arr[small];
        arr[small] = temp; 
      }
  	}
  }
}
Posted by: Guest on May-06-2020

Code answers related to "Implement Selection Sort and print the index which gets swapped at each step."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language