Answers for "selection sort jaa"

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
0

selection sort java

static void selectionSort(int[] arr) {
        int lowest, lowestIndex;
        for(int i = 0; i < arr.length -1; i++) {
            //Find the lowest
            lowest = arr[i];
            lowestIndex = i;
            for(int j = i; j < arr.length; j++) {
                if(arr[j] < lowest) {
                    lowest = arr[j];
                    lowestIndex = j;
                }
            }
            //Swap
            if(i != lowestIndex) {
                int temp = arr[i];
                arr[i] = arr[lowestIndex];
                arr[lowestIndex] = temp;
            }
            
        }
    }
Posted by: Guest on October-07-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language