zoukankan      html  css  js  c++  java
  • 选择排序

    选择排序的思路是:

    通过遍历数组,得到这一次遍历过程中遍历到的值最小的数的下标,将改下标的值与下标为第一的那个值交换,遍历元素个数减一,每次排序能确定一个数到其该去的位置上去

    比较简单,代码记录,完事。

    public class SelectSort {
        public static void main(String [] args){
            int [] arr = new int []{5 ,9 ,3 ,6 ,4};
            selectSort(arr);
            System.out.println(Arrays.toString(arr));
        }
    
        //这是一个选择排序的函数
        //找出最小的数的下标,记录,循环完了之后与第一个数进行交换
        public static int [] selectSort(int [] array){
            int temp;
            for (int i = 0; i < array.length - 1; i++) {
                int min = i;
                for (int j = i + 1; j < array.length; j++) {
                    if(array[min] > array[j]){
                        //标记最小值的下标
                        min = j;
                    }
                }
                //将刚才找到的最小值与第一个数交换,即每次确定一个数
                temp = array[min];
                array[min] = array[i];
                array[i] = temp;
            }
            return array;
        }
    }
  • 相关阅读:
    Android发送信息模拟系统
    Android SharedPreferences
    Android中SQLiteDatabase操作【附源码】
    poj 2453
    pku 1020
    poj 2594 Treasure Exploration
    pku 2092 Grandpa is Famous
    zoj Weekend Party
    poj 3259 Wormholes
    poj 2455 Secret Milking Machine
  • 原文地址:https://www.cnblogs.com/zzxisgod/p/13335642.html
Copyright © 2011-2022 走看看