zoukankan      html  css  js  c++  java
  • 算法学习之选择排序

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

     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 }
  • 相关阅读:
    bzoj 4583 购物
    hdu 4694 支配树
    弦图问题初步
    第一次省选总结
    初学kd树
    省选前集训 lca
    bzoj 3282 Tree
    bzoj 2157 旅游
    二分图匹配(匈牙利算法模板)
    最大流(模板)
  • 原文地址:https://www.cnblogs.com/sunfie/p/4781367.html
Copyright © 2011-2022 走看看