zoukankan      html  css  js  c++  java
  • java四种数组排序

    数组的四种排序

    1.快速排序法Arrays.sort();

    用法1.sort(byte[] a)

         对指定的 byte 型数组按数字升序进行排序。
      sort(byte[] a, int fromIndex, int toIndex)
      对指定 byte 型数组的指定范围按数字升序进行排序。
      sort(char[] a)
      对指定的 char 型数组按数字升序进行排序。
      sort(char[] a, int fromIndex, int toIndex)
      对指定 char 型数组的指定范围按数字升序进行排序。
      sort(double[] a)
      对指定的 double 型数组按数字升序进行排序。
      sort(double[] a, int fromIndex, int toIndex)
      对指定 double 型数组的指定范围按数字升序进行排序。
      sort(float[] a)
      对指定的 float 型数组按数字升序进行排序。
      sort(float[] a, int fromIndex, int toIndex)
      对指定 float 型数组的指定范围按数字升序进行排序。
      sort(int[] a)
      对指定的 int 型数组按数字升序进行排序。
      sort(int[] a, int fromIndex, int toIndex)
      2.sort(long[] a)

      对指定的 long 型数组按数字升序进行排序。
      sort(long[] a, int fromIndex, int toIndex)
      对指定 long 型数组的指定范围按数字升序进行排序。
      sort(Object[] a)
      根据元素的自然顺序,对指定对象数组按升序进行排序。
      sort(Object[] a, int fromIndex, int toIndex)
      根据元素的自然顺序,对指定对象数组的指定范围按升序进行排序。
      sort(short[] a)
      对指定的 short 型数组按数字升序进行排序。
      sort(short[] a, int fromIndex, int toIndex)
      对指定 short 型数组的指定范围按数字升序进行排序。
      sort(T[] a, Comparator<? super T> c)
      根据指定比较器产生的顺序对指定对象数组进行排序。
      sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
      根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。
     

    2.冒泡排序法

    int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51};  
        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;
                    }
                }
            }
    

    3.选择排序

    int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51};  
       for(int i=0;i<a.length;i++){
           for(int j=i+1;j<a.length;j++){
               if(a[i]>a[j]){
                   int temp=a[i];
                   a[i]=a[j];
                   a[j]=temp;
               }
           }
       }
    

    4.插入排序

    int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51};  
         for (int i = 1; i < a.length; i++)
            {
                for (int j = i; j > 0; j--)
                {
                    if (a[j] < a[j - 1])
                    {
                        int temp = a[j - 1];
                        a[j - 1] = a[j];
                        a[j] = temp;
                    }
                    else
                        break;
                }
            }
    
  • 相关阅读:
    Java下载文件,中文名乱码(attachment;filename=中文文件名)
    Spring Boot打jar包,排除lombok等scope=provided的依赖
    VirtualBox+WinDbg+Win7调试环境配置
    【编译原理】FIRST集、FOLLOW集算法原理和实现
    epoll EPOLLL、EPOLLET模式与阻塞、非阻塞
    【编译原理】提取左部公因子算法
    编译和阅读WRK源码
    WRK 源码编译使用
    Windows加载器与模块初始化
    关于Win7 x64下过TP保护(应用层)
  • 原文地址:https://www.cnblogs.com/wcyBlog/p/3926497.html
Copyright © 2011-2022 走看看