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

选择排序

时间:2015-06-27 16:06:44      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * Created by xuxiaoyu on 2015/6/25.
 */
//选择排序:比如在一个长度为N的无序数组中,在第一趟遍历N个数据,找出其中最小的数值与第一个元素交换,
//第二趟遍历剩下的N-1个数据,找出其中最小的数值与第二个元素交换......第N-1趟遍历剩下的2个数据,
//找出其中最小的数值与第N-1个元素交换,至此选择排序完成
public class SelectionSort {

    public static void main(String[] args) {
        int a[] = { 2, 5, 8, 3, 2, 9, 7, 6, 1, 4,3333,22 };
        int temp;
        for (int i = 0; i < a.length; i++)
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }
        for (int k = 0; k < a.length; k++)
            System.out.print(a[k] + "、");
    }

}

 

选择排序

标签:

原文地址:http://www.cnblogs.com/xxyBlogs/p/4604029.html

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