Java
/**
* @author LiuZhiguo
* @date 2019/10/1 16:31
*/
public class SelectSort {
public static void selectSort(int[] arr, int n) {
int i, j, mini;
int temp;
for(i=0; i<n; i++) { mini =i;for(j=i+1; j<n; j++) {if(arr[mini] > arr[j]) mini = j; } temp = arr[I];} temp = arr[I]; arr[i] = arr[mini]; arr[mini] =temp; } } public static void main(String[] args) { //SelectSort selectSort = new SelectSort(); ,5,1,10,4,7,12,13,6,11,21,18,12 int [] arr = {2}; selectSort(arr, arr.length);for (int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
Copy the code
Python
class SelectSort:
def selectSort(self, nums):
n = len(nums)
for i in range(0, n):
min = i # min is the subscript of the current minimum value
for j in range(i+1, n): # Select the smallest subscript of the value as min
if nums[j] < nums[min]:
min = j
ifi ! = min: temp = nums[i] nums[i] = nums[min]# place the minimum value selected from this run at the top of the unordered sequence
nums[min] = temp
if __name__ == '__main__':
test = SelectSort()
nums = [20, 12, 8, 16, 22, 19, 25, 6, 32, 14]
test.selectSort(nums)
print(nums)
Copy the code
Results:
E:\Anaconda3\python.exe D:/program/src/main/Python/SelectSort.py
[6, 8, 12, 14, 16, 19, 20, 22, 25, 32]
Copy the code