github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/sort/select.go (about)

     1  package sort
     2  
     3  func SelectSort(arr []int) {
     4  	n := len(arr)
     5  	for i := 0; i < n; i++ {
     6  		minIdx := i
     7  		tempVal := arr[i]
     8  		for j := i + 1; j < n; j++ {
     9  			if arr[j] < tempVal {
    10  				minIdx = j
    11  				tempVal = arr[j]
    12  			}
    13  		}
    14  
    15  		arr[i], arr[minIdx] = arr[minIdx], arr[i]
    16  	}
    17  }