zoukankan      html  css  js  c++  java
  • 【数据结构和算法】选择排序

    记录一下选择排序。

    public class SelectionSort {
    
        public static void sort(Integer[] array) {
            if (array == null || array.length == 0) {
                return;
            }
    
            Integer miniNum = null;
            Integer miniIndex = null;
            for (int i = 0; i < array.length - 1; i++) {
                miniIndex = i;
                miniNum = array[i];
                for (int j = i + 1; j < array.length; j++) {
                    if (array[j] < miniNum) {
                        miniIndex = j;
                        miniNum = array[j];
                    }
                }
    
                if (miniIndex != i) {
                    swap(array, i, miniIndex);
                }
            }
        }
    
        private static void swap(Integer[] array, Integer i, Integer j) {
            Integer temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    
    }
    import org.junit.Test;
    
    
    public class HowToTest {
    
        @Test
        public void c1() {
            Integer[] array = {3,16,1,5,2,18,0,9,20,11};
            SelectionSort.sort(array);
    
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + ", ");
            }
            System.out.println();
        }
    
        @Test
        public void c2() {
            Integer[] array = {99,98,97,96,95,94,93,92,91,101,90,89,88,87,86,85};;
            SelectionSort.sort(array);
    
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + ", ");
            }
            System.out.println();
        }
    
    }
  • 相关阅读:
    acm寒假特辑1月20日 CodeForces
    acm寒假特辑1月24日 HDU
    acm寒假特辑1月25日HDU
    acm寒假特辑1月26日HDU
    acm寒假特辑1月22日HDU
    acm寒假特辑1月28日HDU
    ubuntu14.04安装notepadqq
    ntpd vs. ntpdate
    centos7 防火墙firewalld
    git 自动补全 (git auto completion)
  • 原文地址:https://www.cnblogs.com/nick-huang/p/5335052.html
Copyright © 2011-2022 走看看