zoukankan      html  css  js  c++  java
  • Java-数据结构与算法-选择排序与冒泡排序

    Java 选择排序与冒泡排序

    1.DataSorter.java

     1 public class DataSorter {
     2 
     3     //冒泡排序法
     4     //主要思路:按升序排序,数组元素两两比较,大的立即排后面
     5     public static void bubbleSort(int[] a) {
     6         for(int i = 0 ;i < a.length ;i++){
     7             int index = i;
     8             for(int j = i ; j < a.length ; j ++){
     9                if(a[index] > a [j]){
    10                 index = j;
    11               }
    12             }
    13             if(index != i){
    14               swap(a, i, index);
    15             }
    16           }
    17     }
    18     
    19 
    20     //直接选择排序法
    21     //主要思路:按升序排序,每次循环找出最小数,把他放到第i位置上
    22     public static void selectSort(int[] array) {
    23         
    24     }
    25 
    26     
    27     //交换数组元素
    28     private static void swap(int[] a, int x, int y) {
    29         int tmp = a[x];
    30         a[x] = a[y];
    31         a[y] = tmp;
    32     }
    33     
    34     //输出数组元素
    35     public static void show(int[] a) {
    36         for(int i = 0; i < a.length; i++){
    37             System.out.println(a[i]);
    38         }
    39     }
    40     
    41 }

    2.测试类Test.java

    public class Test {
    
    	public static void main(String[] args) {
    		int [] a = {9,2,1,8,0,3};
    		DataSorter.selectSort(a);
    		DataSorter.show(a);
    		
    	}
    
    }
    

      

    3.测试结果:

  • 相关阅读:
    正则工具
    反射工具类
    FastJson前置属性过滤器
    随机数工具类
    对图片进行压缩,水印,伸缩变换,透明处理,格式转换操作
    ChineseNumber 转换
    auth src
    tex src
    freeradius防止用户异常断开无法重新链接上
    gnu
  • 原文地址:https://www.cnblogs.com/shamgod/p/4585261.html
Copyright © 2011-2022 走看看