/* * Chimomo's Blog: https://blog.csdn.net/chimomo/ */ package chimomo.learning.java.algorithm.sort; /** * * @author Chimomo * * <p> * The selection sort is following: * </p> * <p> * Every round of sorting selecting the smallest or biggest element from the * to-be-sorted sequence, put it at the end of already-ordered sequence, until * all the elements of to-be-sorted sequence are sorted. Selection sort is not * stable. * </p> */ public class SelectionSort { /** * The selection sort. * * @param a The array to be sorted */ public static void sort(int[] a) { for (int i = 0; i < a.length - 1; i++) { // Store the sbuscript index of the smallest element. int min = i; // Find the smallest element. for (int j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } } // Put the samllest element at already-ordered sequence. int t = a[min]; a[min] = a[i]; a[i] = t; // Print each round of sorting. System.out.println(String.format("Round %d", i + 1)); for (int k : a) { System.out.print(k + " "); } System.out.println(); } } }
算法 - 选择排序(Java)
猜你喜欢
转载自blog.csdn.net/chimomo/article/details/80533790
今日推荐
周排行