算法-排序-选择排序

  很久没有复习算法了,今天开始准备拿来练练手。

  先从简单的选择排序入手。

  选择排序,即按顺序,每次从某数开始,选择出一个最小(大)数,与某数交换位置。

  

 1 /*
 2  * Copyright (c) 2015  4  * All rights reserved.
 5  */
 6 package sort;
 7 /**
 8  * Description        : 选择排序算法
 9  * <p/>
10  * <br><br>Time        : 2015-11-3 下午4:58:05
11  *
12  * @author ZXL
13  * @version 1.0
14  * @since 1.0
15  */
16 public class SelectionSort {
17     public static void main(String[] args) {
18         int[] nums= {4,8,7,5,8,5,9,3,2,1};
19         SelectionSort.selection(nums);
20         for (int e : nums){
21             System.out.print(e+",");
22         }
23     }
24     
25     public static void selection(int[] nums){
26         int temp = 0;
27         int mark = 0;//用于标记最小数的位置
28         for (int i=0; i<nums.length; i++){
29             //每次从某数开始
30             int min = nums[i];
31             //往后与后面的数比大小,找出最小数
32             for (int j=i; j<nums.length; j++){
33                 if (min>nums[j]){
34                     min = nums[j];
35                     mark = j;
36                 }
37             }
38             //与之交换位置
39             temp = nums[i];
40             nums[i] = nums[mark];
41             nums[mark] = temp;
42         }
43     }
44 }
原文地址:https://www.cnblogs.com/lindaZ/p/4934178.html