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

    名词解释:——来自百度百科

      选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。

     1 public class SelectionSort {
     2 
     3     public static void main(String[] args) {
     4         int[] arr = {100, 30, 70, 20, 80, 60, 40, 50, 90, 10};
     5         arr = selectionSort(arr);
     6         System.out.println(Arrays.toString(arr));
     7     }
     8 
     9     private static int[] selectionSort(int[] arr){
    10         int minIndex = 0;
    11         int temp = 0;
    12         for (int i = 0; i < arr.length; i++) {
    13             // 无序区数组的最小索引
    14             minIndex = i;
    15             for (int j = i + 1; j < arr.length; j++) {
    16                 // 无序区数组中找到最小元素,并保存其索引
    17                 if (arr[j] < arr[minIndex]) {
    18                     minIndex = j;
    19                 }
    20             }
    21             // 将无序区中找出的最小元素放到本次循环的前端
    22             temp = arr[i];
    23             arr[i] = arr[minIndex];
    24             arr[minIndex] = temp;
    25         }
    26         return arr;
    27     }
    28 }
    如发现有错误欢迎指正,欢迎交流,接受反驳。 -- by不为 :)
  • 相关阅读:
    sas 基础(1)-关于数据格式的SAS函数
    sas 命令行打开SAS IDE 的代码
    获取指定数据集观测数
    sas spawner
    sas 解析json
    正则表达式(更新中。。。)
    async await的简单使用
    element ui只输入数字校验
    element ui中表单循环项的校验
    微信小程序template和组件
  • 原文地址:https://www.cnblogs.com/buwei/p/10080664.html
Copyright © 2011-2022 走看看