Java中的几种排序算法:冒泡排序,插入排序,二分法排序,简单排序,快速排序

冒泡排序:

    int[] hehe={4,7,2,5,6,9,0};
      
      for(int i=0;i<hehe.length;i++){
        for(int j=i+1;j<hehe.length;j++){
            if(hehe[i]>hehe[j]){
                int temp=hehe[i];
                hehe[i]=hehe[j];
                hehe[j]=temp;
            } 
        }
      }

插入排序

int[] a={13,7,8,9,10,1,2,32};
      int i,j,t,h;
      for (i=1;i<a.length;i++)
      { 
       t=a[i]; //存后一位的数
      
       j=i-1;
      
       while(j>=0 && t<a[j])
       {   
         a[j+1]=a[j]; //将后一位数等于前一位
         
           j--;
          
       }
       a[j+1]=t; //如果进行了while将后一位数等于存的后一位数
       
       System.out.print(""+i+"步排序结果:");           //输出每步排序的结果
       for(h=0;h<a.length;h++)
       {
           System.out.print(" "+a[h]);             //输出
       }
       System.out.print("\n");
      } 

简单排序

for (int i = 0; i < hehe.length - 1; i++) {   
           int min = i;   
           // Find smallest name   
           for (int j = i + 1; j < hehe.length; j++) {   
               if (hehe[j] < hehe[min]) // 找到最小值 注意此处不是判断a[j]<a[i]   
                   min = j;   
           }   
           // Swap data if necessary   
           if (min != i) {   
               int temp = hehe[i];   
               hehe[i] = hehe[min];   
               hehe[min] = temp;   
           }   
       }   
       for (int i = 0; i < hehe.length; i++) {
        System.out.println(hehe[i]);
    }

二分法排序

int[] a = { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };

        for (int i = 0; i < 10; i++) {
            System.out.printf("%d  ", a[i]);
        }
        System.out.println("第0次");
        int i, j;
        int low, high, mid;
        int temp;
        for (i = 1; i < 10; i++) {
            temp = a[i];
            low = 0;
            high = i - 1;
            mid = (low + high) / 2;
            // 求high的值,也就是要换的次数
            while (low <= high) {
                mid = (low + high) / 2;

                if (a[mid] > temp)
                    high = mid - 1; // 对次数的计算
                else
                    low = mid + 1;
            }
            // 对数进行交换
            for (j = i - 1; j > high; j--) {
                a[j + 1] = a[j];
            }
            a[high + 1] = temp;

        }

        for (i = 0; i < 10; i++) {
            System.out.printf("%d  ", a[i]);
        }
        System.out.println("排序后");
    }

快速排序

public class quickSort {
    public void quick_sort(int[] arrays, int lenght) {
        if (null == arrays || lenght < 1) {
            System.out.println("input error!");
            return;
        }
        _quick_sort(arrays, 0, lenght - 1);
    }

    public void _quick_sort(int[] arrays, int start, int end) {
        if (start >= end) {
            return;
        }

        int i = start;
        int j = end;
        int value = arrays[i];
        boolean flag = true;
        while (i != j) {
            if (flag) {
                if (value > arrays[j]) {
                    swap(arrays, i, j);
                    flag = false;

                } else {
                    j--;
                }
            } else {
                if (value < arrays[i]) {
                    swap(arrays, i, j);
                    flag = true;
                } else {
                    i++;
                }
            }
        }
        snp(arrays);
        _quick_sort(arrays, start, j - 1);
        _quick_sort(arrays, i + 1, end);

    }

    public void snp(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i] + " ");
        }
        System.out.println();
    }

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

    public static void main(String args[]) {
        quickSort q = new quickSort();
        int[] a = { 49, 38, 65, 12, 45, 5 };
        q.quick_sort(a, 6);
    }

}

 

posted @ 2016-02-26 10:56  爱学习~天天学习  阅读(395)  评论(0编辑  收藏  举报