数据结构与算法:十大排序算法之选择排序

数据结构与算法:十大排序算法之选择排序

在这里插å¥å›¾ç‰‡æè¿°

package TopTenSortingAlgorithms;

import java.util.Arrays;
import java.util.Scanner;

public class SelectionSort {
     
    public static void main(String[] args) {
     
        Scanner scanner = new Scanner(System.in);
        int[] nums = new int[4];
        for (int i = 0; i < nums.length; i++) {
     
            nums[i]=scanner.nextInt();
        }
        int[] selectionsort = selectionsort(nums);
        System.out.println(Arrays.toString(selectionsort));
    }


    public static int[]  selectionsort(int[] nums){
     

        for (int end = nums.length-1; end >0 ; end--) {
     
            int maxIndex=0;
            for (int being = 1; being <= end; being++) {
     
                if (nums[maxIndex]<=nums[being]){
     
                    maxIndex=being; //获得该数组中最大值的索引
                }
            }
            //从小到大排序
            //将最大的值与最小的值进行交换
            int tmp=nums[maxIndex];
            nums[maxIndex]=nums[end];//
            nums[end]=tmp;

        }
        return nums;

    }
}


你可能感兴趣的