一些Java经典算法

一,冒泡排序:
package com.sort;

import java.util.Random;

public class No1Maopao {
    public static void main(String[] args) {
        Random rd = new Random();
//生成随即数组
        int [] src = new int[10];
        for(int i=0;i<10;i++){
            src[i]=rd.nextInt(100);
        }
//排序前打印数组所有元素
        for(int i:src)
            System.out.println(i);
        System.out.println("---------------可爱的分割线-------------------");
        src = sort(src);
//排序后打印数组所有元素
        for(int i:src)
            System.out.println(i);
    }

    public static int[] sort(int [] src){
        for (int i = 0; i < src.length; i++) {
            for(int j=i;j<src.length;j++){
                if(src[j]<src[i]){
                    int temp = src[j];
                    src[j]=src[i];
                    src[i]=temp;
                }
            }
        }
        return src;
    }
}
二、选择排序:
package com.sort;

import java.util.Random;

public class No2XuanZhe {
    public static void main(String[] args) {
        Random rd = new Random();
        int[] src = new int[10];
        for (int i = 0; i < 10; i++) {
            src[i] = rd.nextInt(100);
        }
        for (int i : src)
            System.out.println(i);
        System.out.println("---------------可爱的分割线-------------------");
        src = sort(src);
        for (int i : src)
            System.out.println(i);
    }

    public static int[] sort(int[] src) {
        int min;
        for (int i = 0; i < src.length; i++) {
            min = src[i];
            for (int j = i + 1; j < src.length; j++) {
                if (src[j] < min) {
                    min = src[j];
                    int temp = src[i];
                    src[i] = src[j];
                    src[j] = temp;
                }
            }
        }
        return src;
    }
}
三、插入排序:
package com.sort;

import java.util.Random;

public class No3ChaRu {
    public static void main(String[] args) {
        Random rd = new Random();
        int[] src = new int[10];
        for (int i = 0; i < 10; i++) {
            src[i] = rd.nextInt(100);
        }
        for (int i : src)
            System.out.println(i);
        System.out.println("---------------可爱的分割线-------------------");
        src = sort(src);
        for (int i : src)
            System.out.println(i);
    }

    public static int[] sort(int[] src) {
        int in, out;
        for (in = 1; in < src.length; in++) {
            int temp = src[in];
            out = in;
            while (out > 0 && src[out - 1] >= temp) {
                src[out] = src[out - 1];
                --out;
            }
            src[out] = temp;
        }
        return src;
    }
}
四、希尔排序:
package com.sort;

import java.util.Random;

public class No4Xier {
    public static void main(String[] args) {
        Random rd = new Random();
        int[] src = new int[10];
        for (int i = 0; i < 10; i++) {
            src[i] = rd.nextInt(100);
        }
        for (int i : src)
            System.out.println(i);
        System.out.println("---------------可爱的分割线-------------------");
        src = sort(src);
        for (int i : src)
            System.out.println(i);
    }

    public static int[] sort(int[] src) {
        int len = src.length;
        int temp, i, j;
        int h = 1;
        while (h <= len / 3)
            h = h * 3 + 1;
        while (h > 0) {
            for (i = h; i < len; i++) {
                temp = src[i];
                j = i;
                while (j > h - 1 && src[j - h] >= temp) {
                    src[j] = src[j - h];
                    j -= h;
                }
                src[j] = temp;
            }
            h = (h - 1) / 3;
        }
        return src;
    }
}
五、快速排序:
public class No5Kuaisu {

    public static void quickSort(int[] array) {
        quickSort(array, 0, array.length - 1);
    }

    private static void quickSort(int[] array, int low, int high) {
        if (low < high) {
            int p = partition(array, low, high);
            quickSort(array, low, p - 1);
            quickSort(array, p + 1, high);
        }

    }

    private static int partition(int[] array, int low, int high) {
        int s = array[high];
        int i = low - 1;
        for (int j = low; j < high; j++) {
            if (array[j] < s) {
                i++;
                swap(array, i, j);
            }
        }
        swap(array, ++i, high);
        return i;
    }

    private static void swap(int[] array, int i, int j) {
        int temp;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static void main(String[] args) {
        Random rd = new Random();
        int[] src = new int[10];
        for (int i = 0; i < 10; i++) {
            src[i] = rd.nextInt(100);
        }
        for (int i : src)
            System.out.println(i);
        System.out.println("---------------可爱的分割线-------------------");
        quickSort(src);
        for (int i : src)
            System.out.println(i);
        System.out.println("---------------可爱的分割线-------------------");
    }
}


1.写一个方法,用一个for循环打印九九乘法表

       /** *//**
     * 打印九九乘法口诀表
     */
    public void nineNineMulitTable(){
        for (int i = 1,j = 1; j <= 9; i++) { 
              System.out.print(i+"*"+j+"="+i*j+" "); 
              if(i==j){ 
                  i=0; 
                  j++; 
                  System.out.println(); 
              } 
          } 
    }

2.给定一个java.util.Date对象,如何转化为”2007-3-22 20:23:22”格式的字符串 
    /** *//** 
     * 将某个日期以固定格式转化成字符串 
     * @param date 
    * @return str 
    */ 
    public String date2FormatStr(Date date)
    { 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
      String str = sdf.format(date); 
      return str; 
    } 
3.写一个方法,能够判断任意一个整数是否素数 
    /** *//** 
    * 判断任意一个整数是否素数 
    * @param num 
    * @return boolean 
    */ 
    public boolean isPrimeNumber(int num) 
    { 
      for (int i = 2; i <= Math.sqrt(num); i++) { 
          if(num%i==0) 
          { 
              return false; 
          } 
      } 
      return true; 
    } 
4.写一个方法,输入任意一个整数,返回它的阶乘 
     
     /** *//** 
      *获得任意一个整数的阶乘 
      *@param n 
      *@returnn! 
      */ 
      public int factorial(int num) 
      { 
        //递归 
        if(num == 1) 
        { 
            return 1; 
        } 
        return num*factorial(num-1);
      }

5.写一个方法,用二分查找法判断任意整数在任意整数数组里面是否存在,若存在就返回它在数组中的索引位置,不存在返回-1 
      /** *//** 
       *二分查找特定整数在整型数组中的位置(递归) 
       *@param dataset 
       *@param data 
       *@param beginIndex 
       *@param endIndex 
       *@return index 
       */ 
       public int binarySearch(int[] dataset,int data,int beginIndex,int endIndex){ 
         int midIndex = (beginIndex+endIndex)/2; 
         //如果查找的数要比开始索引的数据要小或者是比结束索引的书要大,或者开始查找的索引值大于结束的索引值返回-1没有查到
         if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){
             return -1;
         }
         if(data <dataset[midIndex]){ 
             return binarySearch(dataset,data,beginIndex,midIndex-1); 
         }else if(data>dataset[midIndex]) 
         { 
             return binarySearch(dataset,data,midIndex+1,endIndex); 
         }else { 
             return midIndex; 
         } 
       } 
       
       /** *//** 
        *二分查找特定整数在整型数组中的位置(非递归) 
        *@param dataset 
        *@param data 
        *@return index 
        */ 
        public int binarySearch(int[] dataset ,int data) 
        { 
          int beginIndex = 0; 
          int endIndex = dataset.length - 1; 
          int midIndex = -1; 
          if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){
              return -1; 
          }
          while(beginIndex <= endIndex) { 
              midIndex = (beginIndex+endIndex)/2; 
              if(data <dataset[midIndex]) { 
                 endIndex = midIndex-1; 
              } else if(data>dataset[midIndex]) { 
                beginIndex = midIndex+1; 
              }else { 
                return midIndex; 
              } 
          } 
          return -1; 
        }


 

 

你可能感兴趣的:(java,算法,J#)