A long time no review algorithm, and today began to take to practice practiced hand.
Start with a simple sort of selection.
Select Sort, that is, in order, each time starting from a certain number, select a minimum (large) number to swap the position with a number.
1 /*2 * Copyright (c)4 * All rights reserved.5 */6 Packagesort;7 /**8 * Description: Select sorting algorithm9 * <p/>Ten * <br><br>time:2015-11-3 pm 4:58:05 One * A * @authorZXL - * @version1.0 - * @since1.0 the */ - Public classSelectionsort { - Public Static voidMain (string[] args) { - int[] nums= {4,8,7,5,8,5,9,3,2,1}; + selectionsort.selection (nums); - for(inte:nums) { +System.out.print (e+ ","); A } at } - - Public Static voidSelectionint[] nums) { - inttemp = 0; - intMark = 0;//location to mark the minimum number - for(inti=0; i<nums.length; i++){ in //each time starting with a certain number - intMin =Nums[i]; to //Next to the size of the number in the back, find the minimum number + for(intJ=i; j<nums.length; J + +){ - if(min>Nums[j]) { theMin =Nums[j]; *Mark =J; $ }Panax Notoginseng } - //swap positions with thetemp =Nums[i]; +Nums[i] =Nums[mark]; ANums[mark] =temp; the } + } -}
Algorithm-sort-select sort