zoukankan      html  css  js  c++  java
  • java 之冒泡排序

    冒泡排序:可以想象成煮开水,气泡在瓶底的时候是比较小的,到达水面的时候达到最大。

    冒泡排序的思想:先确定是升序还是降序,这里升序为例。每两个相邻的数字进行比较,前一个数字比后面一个数字大,就将两个数字交换位置,否则位置不变继续下一个。一轮排序之后,数组中最大的数字一定是在最后。

    伪代码:

        数组a={10,9,8,7,6,5,4,3,2,1}

          for(int i=0;i<a.length;i++){

              for(int j=0;j<a.length-i-j;j++){   //为什么是j<a.length-i-1; 先解释减1,到数据组的最后两个数的时候a[j]  和 a[j++] 进行比较,当没有减1的时候会下标越界。为什么要减i,每一轮排序都会产生(i+1)个已经排好序的数据

                  交换数据:swap(a[j],a[j++])

                          }

              }

          输出排序完的数据:升序

    代码:

    package math;
    
    /**
     * Created by Administrator on 2018/3/2.
     * 冒泡排序
     * 思想:冒泡排序就是将相邻的两个相比较,按照定义的规则,决定将大数放在前面还是后面,然后进行交换顺序,
     *       每一次排序都会将那一次最大的或者最小的数排在最后
     *        冒泡排序的时间复杂度:o(n2)
     */
    public class BubbleTest {
    
    
    
    
        public static void  main(String[] args){
       /*     //控制台进行输入数据
            Scanner s=new Scanner(System.in);
            //输入的数字存放到数组中
            Integer array[]=new Integer[10];
            Integer count=0;
            System.out.println("请输入十个数:");
           while (s.hasNextInt()){
                Integer tempNum=s.nextInt();
               array[count++]=tempNum;
               if(count>9){
                    break;
               }
            }
            System.out.println("输入的十个数是:");
            for(int i=0;i<10;i++){
                System.out.print(array[i]+",");
            }*/
            Integer count=100000;
            Integer array[]=new Integer[count];
            for(int w=0;w<array.length;w++){
                array[w]=count--;
            }
    
    
    
            /*冒泡排序*/
            long startTime=System.currentTimeMillis();
            System.out.println(startTime);
            for(Integer i=0;i<array.length;i++){
                for(Integer j=0;j<array.length-i-1;j++){
                    if(array[j]>array[j+1]){
                     Integer temp;
                        temp=array[j];
                        array[j]=array[j+1];
                        array[j+1]=temp;
                    }
                }
               /* System.out.println("第"+i+"次排序之后的顺序:");
                for(Integer k=0;k<array.length;k++){
                    System.out.print(array[k]+",");
                }*/
            }
            Long endTime=System.currentTimeMillis();
            System.out.println(endTime);
            System.out.println("总耗时==="+(endTime-startTime));
       /*     System.out.println("排序之后的顺序:");
            for(Integer f=0;f<array.length;f++){
                System.out.print(array[f]+",");
            }
    */
    
        }
    
    
    
    
    
    }
  • 相关阅读:
    LeetCode数学系列(3)——快速幂算法(50题)
    LeetCode树系列(3)——200题岛屿数量
    Arrays.sort()详解
    图表示学习系列(1)——GCN学习笔记:第一部分,详细讲解GCN
    LeetCode动态规划系列(3)——编辑距离问题求解
    深度学习系列(9)——node2vec算法中的alias采样介绍
    LeetCode数学系列(2)——解决约瑟夫问题
    Java数据结构系列(4)——队列常用方法
    LeetCode树系列(1)——广度搜索应用,图的BFS
    LeetCode树系列(2)——深度搜索运用:LeetCode695题详解
  • 原文地址:https://www.cnblogs.com/blogxiao/p/8496492.html
Copyright © 2011-2022 走看看