java选择排序算法

en9的头像 en9 11 2014-12-30 16:53 4

 基本信息

× 1   

浏览数: 4238

分享时间: 4 年 前

3
/**
 * 选择排序:执行完一次内for循环后最小的一个数放在了数组的最前面。
 *
 * 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
 *
 */
public class SelectSort {
 
    /**
     * 排序算法的实现,对数组中指定的元素进行排序
     *
     * @param array
     *            待排序的数组
     * @param from
     *            从哪里开始排序
     * @param end
     *            排到哪里
     * @param c
     *            比较器
     */
    public void select(Integer[] array) {
        int minIndex;// 最小索引
 
        /*
         * 循环整个数组(其实这里的上界为 array.length - 1 即可,因为当 i= array.length-1
         * 时,最后一个元素就已是最大的了,如果为array.length时,内层循环将不再循环),每轮假设
         * 第一个元素为最小元素,如果从第一元素后能选出比第一个元素更小元素,则让让最小元素与第一 个元素交换
         */
 
        for (int i = 0; i < array.length; i++) {
            minIndex = i;// 假设每轮第一个元素为最小元素
            // 从假设的最小元素的下一元素开始循环
            for (int j = i + 1; j < array.length; j++) {
                // 如果发现有比当前array[smallIndex]更小元素,则记下该元素的索引于smallIndex中
                if ((array[j].compareTo(array[minIndex])) < 0) {
                    minIndex = j;
                }
            }
 
            // 先前只是记录最小元素索引,当最小元素索引确定后,再与每轮的第一个元素交换
            swap(array, i, minIndex);
        }
    }
 
    public static void swap(Integer[] intgArr, int x, int y) {
        // Integer temp; //这个也行
        int temp;
        temp = intgArr[x];
        intgArr[x] = intgArr[y];
        intgArr[y] = temp;
    }
 
    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        Integer[] intgArr = { 5, 9, 1, 0, 4, 2, -6, 3, 8, 0, 76, -7, 7, 11, 12,
                19, 8, 9, 10, 28, 53 };
        SelectSort insertSort = new SelectSort();
        insertSort.select(intgArr);
        for (Integer intObj : intgArr) {
            System.out.print(intObj + " ");
        }
 
    }
}


12 3 4 [下一页]

  • ossaa的头像 ossaa 2018-05-12 19:32 代码数:0

    This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine.  Lewis SEO

  • jhoneila的头像 jhoneila 2018-12-22 14:30 代码数:0

    I learn some new stuff from it too, thanks for sharing your information. Pro Training Bat

  • jhoneila的头像 jhoneila 2018-12-22 15:14 代码数:0

    I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. CPR Bend Oregon

  • ossaa的头像 ossaa 2018-05-13 21:30 代码数:0

    Your music is amazing. You have some very talented artists. I wish you the best of success.

    Colorado SEO services

  • ossaa的头像 ossaa 2018-05-14 16:43 代码数:0

    Your music is amazing. You have some very talented artists. I wish you the best of success.

    mail order marijuana online

  • ossaa的头像 ossaa 2018-05-14 21:10 代码数:0

    I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

    Montana ranch for sale

  • ossaa的头像 ossaa 2018-05-15 18:04 代码数:0

    This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine.

    auction estate property

  • ossaa的头像 ossaa 2018-12-26 21:19 代码数:0

    All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks

    celular

  • digital112的头像 digital112 2018-05-16 01:15 代码数:0

    Your music is amazing. You have some very talented artists. I wish you the best of success.

    Cincinnati water slide rentals

  • ossaa的头像 ossaa 2018-05-20 15:39 代码数:0

    Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job.

    web design and development bangla tutorial

您的评论: