zoukankan      html  css  js  c++  java
  • java一些基本算法

    本文主要介绍一些常用的算法:

    冒泡排序:两两相互之间进行比较,如果符合条件就相互兑换。    

     1     //冒泡排序升序
     2     public static int[] bubblingSortAsc(int[] array){
     3         if (array.length==0)
     4             return array;
     5         for (int i=0;i<array.length;i++){
     6             for (int j=i+1;j<array.length;j++){
     7                 int temp;
     8                 if (array[i]>array[j]){
     9                     temp = array[i];
    10                     array[i]=array[j];
    11                     array[j]=temp;
    12                 }
    13             }
    14         }
    15         return array;
    16     }

    运行结果:

     

    简单插入排序:例如,将数组进行升序,遍历数组,取出i+1,和(i+1)之前的每一项进行对比,直到(i+1)的数据大于比较的数据。

     1     //简单插入排序
     2     public static int[] insertSortAsc(int[] array){
     3         if (array.length==0)
     4             return array;
     5         int currentValue;
     6 
     7         for (int i=0;i<array.length-1;i++){
     8             int pre = i;
     9             currentValue = array[pre+1]; //取出当前遍历的后一位
    10 
    11             //把当前位置之前的每一项都进行对比
    12             while (pre>=0 && currentValue<array[pre]){
    13                 array[pre+1] = array[pre];
    14                 pre--;
    15             }
    16             
    17             array[pre+1] = currentValue;
    18 
    19         }
    20         return array;
    21     }

    运行结果:

    归并排序:把一个数组一直拆分,直到符合数组定义的数据。然后比较,比较完之后再和其他的进行比较,然后在一层一层的比较。

      

     1     public static int[] sort(int[] array){
     2         final int len = 5;//默认最小的数组长度为5
     3         if (array.length<=len) {
     4             return insertSortAsc(array);//对数组进行正序排序
     5         }else {
     6             int mid = array.length / 2;
     7             int[] left = Arrays.copyOfRange(array, 0, mid);
     8             int[] right = Arrays.copyOfRange(array, mid, array.length);
     9             return compare(sort(left),sort(right));
    10         }
    11     }
    12 
    13     public static int[] compare(int[] left,int[] right){
    14         int[] result = new int[left.length+right.length];
    15         for (int i=0,l=0,r=0;i<result.length;i++){
    16             if (l>=left.length)//当left集合取不到数据的时候,直接从right集合拿数据
    17                 result[i] = right[r++];
    18             else if (r>=right.length) //当right集合取不到数据的时候,直接从left集合取数据
    19                 result[i] = left[l++];
    20             else if (left[l]>right[r]) //当left集合的数据大于right集合的数据,去right集合的数据
    21                 result[i] = right[r++];
    22             else //都不符合就直接取right集合数据
    23                 result[i] = left[l++];
    24         }
    25         return result;
    26     }

    运行结果,自己操作就可以了

    以上便是个人对一些简单算法的见解。

  • 相关阅读:
    正则表达式 \n和\r
    【转】单循环赛赛程安排算法研究
    Iterator效率
    Map获取键值
    PL/SQL语法详解(pdf)
    Iterator模式
    测试js函数的静态页面
    【转】java的一些基本概念
    Oracle 11g用户解锁
    oracle官方文档
  • 原文地址:https://www.cnblogs.com/orange-time/p/10974519.html
Copyright © 2011-2022 走看看