zoukankan      html  css  js  c++  java
  • 常用的基础算法总结之 冒泡排序

    package TT;
    
    public class Test206 {
    
    public static void bubbleSort(int[] a){
    
    int temp=0;
    for(int i =0; i<a.length-1; i++){
    for(int j =0; j<a.length-1-i; j++){
    if(a[j]>a[j+1]){
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
    }
    }
    
    
    
    }
    public static void main(String[] args){
    
    int a[] = {49,38,65,97,76,13,27,49,78,34,12,64};
    bubbleSort(a);
    for( int i=0;i<a.length;i++){
    System.out.println(a[i]);
    }
    
    }
    
    }

    外层循环决定几次

    内层循环比较几次 

    package com.toov5.test;
    
    public class Test10 {
         
       public static void bubbleSort(int[] arr) {
           for(int i=0; i<arr.length; i++) {
               for(int j=1 ;j<arr.length-i; j++) {
                   int temp = 0;
                   if (arr[j]<arr[j-1]) {
                    temp=arr[j];
                    arr[j]=arr[j-1];
                    arr[j-1]=temp;
                }
                   
               }
           }
           
       }
        
        public static void main(String[] args) {
            
            int[] arr = {1,5,2,8,3,};
            
            bubbleSort(arr);
    
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }

    五个数比较四次就OK了 arr.length-1 此    外循环的

    数组是引用传递哦哦哦哦嘎嘎

  • 相关阅读:
    2-1
    project 1
    application.properties
    springbootmybaits_day2
    springbootMybaits_day1
    linux文件夹赋予权限
    属性拼接问题
    三种数据库的配置文件db.properties
    mysql对应java中常用的字段
    Spring里面的注解
  • 原文地址:https://www.cnblogs.com/toov5/p/7602285.html
Copyright © 2011-2022 走看看