java排序算法

一、参考资料

1.

http://www.codeceo.com/article/8-java-sort.html

2.

堆排序参考

http://dsbryz.iteye.com/blog/1182056

基数排序参考

http://baike.baidu.com/link?url=gsZLl6VZLvbFH1Wzj-HdJCZpQUsEl4CIJe6JkC6C8QDKP7da1AEyPyvnKJfJ8eULJRCDWKVcFsSulAbfnKlFqq

3.

(1)代码主要看快速排序

(2)递归思想的排序不需要for循环!(快速排序和归并排序)

(3)快速排序是先处理再递归,归并是先分解(递归)再处理,这就是处理函数位置在分解(递归)前还是后的原因

二、插入排序代码

shell排序相当于分组插入排序

1.插入排序

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={4,2,1,3};
        
        InsertSort(a);
        
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+",");
        }
        
    }
    
    static void InsertSort(int a[]) {
        for (int i = 1; i < a.length; i++) {
            int temp=a[i];//插入算法需要一个辅助空间
            for (int j = i-1; j >= 0; j--) { //此处需要往后移动,所以必须从后往前顺序对比
                if (a[j]>temp) { //此处需要使用temp变量表示a[i],因为a[i]可能会变化
                    a[j+1]=a[j];
                    a[j]=temp;
                }
            }
        }
    }
}

 2. shell排序(分组插入)

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={4,2,1,3};
        
        ShellSort(a);
        
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+",");
        }
        
    }
    
    static void InsertSort(int a[]) {
        for (int i = 1; i < a.length; i++) {
            int temp=a[i];//插入算法需要一个辅助空间
            for (int j = i-1; j >= 0; j--) { //此处需要往后移动,所以必须从后往前顺序对比
                if (a[j]>temp) { //此处需要使用temp变量表示a[i],因为a[i]可能会变化
                    a[j+1]=a[j];
                    a[j]=temp;
                }
            }
        }
    }
    
    static void ShellSort(int a[]) {
        int d=a.length;
        do{
            for (int i = 0; i < d; i++) {//分组
                for (int j = i+d; j < a.length; j+=d) {//从这里开始相当于插入排序
                    int temp =a[j];
                    for (int k = j-d; k >=0; k-=d) {
                        if (a[k]>temp) {
                            a[k+d]=a[k];
                            a[k]=temp;
                        }
                    }
                }
            }
        }while((d=d/2)>0);
    }

}

三、交换排序

 1.冒泡排序

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={4,2,1,3};
        
        BubbleSort(a);
        
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+",");
        }
        
    }
    
    static void BubbleSort(int a[]) {
        for (int i = 1; i < a.length; i++) {//n-1
            for (int j = 0; j < a.length-i; j++) {//n-1
                if (a[j]>a[j+1]) {
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
    }

}

 2. 快速排序

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={4,2,1,3};
        
        QuickSort(a,0,a.length-1);//快速排序的参数也比较多,用于递归。
        
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+",");
        }
        
    }
        
    static void QuickSort(int a[],int low,int high){
        if (low<high) {
            int middle =Sort(a, low, high);//取中值
            QuickSort(a, low, middle-1);//middle-1 中值已经有序,所以不需要排序,否则有可能循环不完。
            QuickSort(a, middle+1, high);//middle+1
        }
    
    }

    static int Sort(int[] a, int low, int high) {
        // TODO Auto-generated method stub
        int temp=a[low];
        while (low<high) {
            while (a[high]>temp && high>low) {//交换移位的思想
                high--;
            }
            a[low]=a[high];
            while (a[low]<temp && low<high) {
                low++;
            }
            a[high]=a[low];
            
        }
        a[low]=temp;
        return low;
    }

}

四、选择排序

1. 简单选择排序

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={4,2,1,3};
        
        SelectSort(a);
        
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+",");
        }
        
    }
        
    static void SelectSort(int a[]){
        for (int i = 0; i < a.length-1; i++) {
            int min=a[i];
            int position=i;
            for (int j = i+1; j < a.length; j++) {
                if (a[j]<min) {
                    min=a[j];
                    position=j;
                }
            }
            a[position]=a[i];
            a[i]=min;            
        }
    }


}

2.堆排序

import java.util.Arrays;

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={4,2,1,3};
        
        HeapSort(a);        

        System.out.print(Arrays.toString(a));//Arrays中函数
//        for (int i = 0; i < a.length; i++) {
//            System.out.print(a[i]+",");
//        }
        
    }
        
    static void HeapSort(int a[]){
        //建堆
        BuildHeap(a);
        //此时数组a的顺序是堆的顺序,a[0]为堆顶。
        for (int i = a.length-1; i > 1; i--) {
            int temp=a[0];
            a[0]=a[i];
            a[i]=temp;
        }        
    }

    private static void BuildHeap(int[] a) {
        // TODO Auto-generated method stub
        //从底层建堆,完全二叉树的性质
        for (int i = a.length/2; i > 0 ; i--) {//堆的序号从1开始,所以不能(a.length-1)/2,下面左子树0*2会一直为0
            maxHeap(a,i,a.length);
        }
        
    }

    private static void maxHeap(int[] a, int parentNodeIndex, int heapSize) {
        // TODO Auto-generated method stub
        int leftChildNodeIndex=parentNodeIndex*2;
        int rightChildNodeIndex=parentNodeIndex*2+1;
        int lagestNodeIndex=parentNodeIndex;
        if (leftChildNodeIndex<heapSize && a[leftChildNodeIndex-1]>a[parentNodeIndex-1]) {//数组对应堆序号,均需要减1
            lagestNodeIndex=leftChildNodeIndex;
        }
        if (rightChildNodeIndex<heapSize && a[rightChildNodeIndex-1]>a[parentNodeIndex-1]) {
            lagestNodeIndex=rightChildNodeIndex;
        }
        if (lagestNodeIndex != parentNodeIndex) {
            int temp=a[parentNodeIndex-1];
            a[parentNodeIndex-1]=a[lagestNodeIndex-1];
            a[lagestNodeIndex-1]=temp;
        }        
    }
}

 五、归并排序

import java.util.Arrays;

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={4,2,1,3};
        
        MergeSort(a);

        System.out.println(Arrays.toString(a));
        
    }
    
    static void MergeSort(int a[]) {
        Sort(a,0,a.length-1);
        
    }

    private static void Sort(int[] a, int low, int high) {
        // TODO Auto-generated method stub
        if (low<high) {
            int center=(low+high)/2;
            //递归分解
            Sort(a,low,center);
            Sort(a, center+1, high);
            //合并
            Merge(a,low,center,high);
        }
    }

    private static void Merge(int[] a, int low, int center, int high) {
        // TODO Auto-generated method stub
        int tempArr[]=new int[a.length];
        int mid=center+1;
        int temp=low;
        int third=low;
        while(low<=center && mid<=high){//都是坐标,需要=
            if (a[low]<=a[mid]) {
                tempArr[third++]=a[low++];
            }else {
                tempArr[third++]=a[mid++];
            }
        }
        while(low<=center){
            tempArr[third++]=a[low++];  
        }
        while(mid<=high){
            tempArr[third++]=a[mid++];  
        }
        while(temp<=high){
            a[temp]=tempArr[temp++];  
        }
    }
}

六、基数排序

import java.util.Arrays;

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={428,2,123,30};
        
        RadixSort(a);

        System.out.print(Arrays.toString(a));//Arrays中函数
    }
        
    static void RadixSort (int a[]){
        //1.最大数
        int max=a[0];
        for (int i = 0; i < a.length; i++) {
            if (a[i]>a[0]) {
                max=a[i];
            }
        }
        //2.循环趟数
        int time=0;
        while (max>0) {
            max=max/10;
            time++;
        }
        
        int [][]temp = new int[10][a.length]; //10个数组表示余数的可能出现的十种情况
        int []point=new int [10];//temp的指针
        
        int n=1;
        for (int i = 0; i < time; i++) {
            //每个temp[i][]均包含一个完整的a[]
            for (int j = 0; j < a.length; j++) {
                int rest=a[j]/n%10;
                temp[rest][point[rest]]=a[j];
                point[rest]++;
                        
            }
            int m=0;
            for (int j = 0; j < 10; j++) {
                if (point[j]!=0) {
                    for (int k = 0; k < point[j]; k++) {
                        a[m++]=temp[j][k];//相当于对a[]进行一次排序
                    }
                }
                point[j]=0;
            }
            n*=10;
        }
    }

}

 改进

import java.util.Arrays;

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={428,2,123,30};
        
        RadixSort(a);

        System.out.print(Arrays.toString(a));//Arrays中函数
    }
        
    static void RadixSort (int a[]){
//        //1.最大数
//        int max=a[0];
//        for (int i = 0; i < a.length; i++) {
//            if (a[i]>a[0]) {
//                max=a[i];
//            }
//        }
//        //2.循环趟数
//        int time=0;
//        while (max>0) {
//            max=max/10;
//            time++;
//        }
        
        int [][]temp = new int[10][a.length]; //10个数组表示余数的可能出现的十种情况
        int []point=new int [10];//temp的指针
        
        int n=1;        
        boolean isEnd;
        do{
            isEnd=true;
            //每个temp[i][]均包含一个完整的a[]
            for (int j = 0; j < a.length; j++) {
                int rest=a[j]/n%10;
                temp[rest][point[rest]]=a[j];
                point[rest]++;
                if (rest!=0) {
                    isEnd=false;
                }
                        
            }
            int m=0;
            for (int j = 0; j < 10; j++) {
                if (point[j]!=0) {
                    for (int k = 0; k < point[j]; k++) {
                        a[m++]=temp[j][k];//相当于对a[]进行一次排序
                    }
                }
                point[j]=0;
            }
            n*=10;
        }while(!isEnd);
    }

}

 

 

  

 

posted @ 2016-05-16 12:49  Hi,Leisure  阅读(164)  评论(0编辑  收藏  举报