码迷,mamicode.com
首页 > 编程语言 > 详细

算法学习之选择排序

时间:2015-09-04 11:06:49      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

选择排序,就是选择最小的,然后置换,循环再找到最小的,再置换...

 1 package com.swust.插入排序;
 2 
 3 import java.util.Random;
 4 
 5 public class Example1 {
 6     public static void main(String[] args) {
 7         int[] arr=new int[10];
 8         for(int i=0;i<arr.length;i++){
 9             arr[i]=new Random().nextInt(100);
10         }
11         test(arr);
12         for (int i = 0; i < arr.length; i++) {
13             System.out.print(arr[i]+",");
14         }
15     }
16     public static void test(int[] a){
17             for (int i=0; i<a.length; i++){ 
18                     int small = i; 
19                     //找出最小的 
20                      for (int j=i+1; j<a.length; j++){ 
21                             if (a[small]>a[j]){ 
22                                     small = j; 
23                             } 
24                     } 
25                      int temp =0;
26                     //置换位置 
27                      if (i != small){ 
28                             temp = a[small]; 
29                             a[small] = a[i]; 
30                             a[i] = temp; 
31                     } 
32             } 
33     }
34 }

 

算法学习之选择排序

标签:

原文地址:http://www.cnblogs.com/sunfie/p/4781367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!