Answers for "sort selection"

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
1

Selection Sort

import numpy as np

def selection_sort(x):
    for i in range(len(x)):
        swap = i + np.argmin(x[i:])
        (x[i], x[swap]) = (x[swap], x[i])
    return x
Posted by: Guest on October-09-2021
1

selection sort

def ssort(lst):
    for i in range(len(lst)):
        for j in range(i+1,len(lst)):
            if lst[i]>lst[j]:lst[j],lst[i]=lst[i],lst[j]
    return lst
if __name__=='__main__':
    lst=[int(i) for i in input('Enter the Numbers: ').split()]
    print(ssort(lst))
Posted by: Guest on September-30-2020
0

Selection sort

#include <stdio.h> 


int main() { 
   int a[10] = {5,8,0,1,4,2,4,3,9,7}; 
   int i, j, min, t;

//t è la variabile temporanea utilizzata per lo scambio 

   for(i=0; i<9; i++){ 
      min=i; 
      for (j = i+1;j < 10; j++){ 
         if (a[j]<a[min]) 
            min=j; 
      } 
      t = a[min];
//si trova l'elemento più piccolo dell'array e si scambia con l'elemento alla posizione i 
      a[min] = a[i]; 
      a[i] = t;  
    }

   for(i=0; i<10; i++){
      printf("%d ", a[i]);		
   }	
}
Posted by: Guest on July-05-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language