常用排序算法的实现

  • 定义数据结构
package sort;

public class Data {
    public int number;
    public String tag = "";
    public Data(int data, String tag) {
        this.number = data;
        this.tag = tag;
    }
    public Data(int data) {
        this.number = data;
    }
    public static Data[] getArr() {
        return new Data[] {new Data(750),new Data(666),new Data(128),new Data(985),new Data(863,"A"),
                new Data(10),new Data(400),new Data(863,"B"),new Data(211),new Data(233),};
    }
    public static void print(Data[] data) {
        for(int i=0;i<data.length-1;i++) {
            System.out.print(data[i]!=null?(data[i].number+data[i].tag+"\t"):"-\t");
        }
        System.out.println(data[data.length-1]!=null?(data[data.length-1].number+data[data.length-1].tag+"\t"):"-\t");
    }
    public static void exchange(Data[] datas,int i1,int i2) {
        Data temp = datas[i1];
        datas[i1] = datas[i2];
        datas[i2] = temp;
    }

    @Override
    public String toString() {
        return ""+number;
    }
}
  • 选择排序
package sort;

public class SelectSort {
    public static void main(String[] args) {
        Data[] arr = Data.getArr();
        Data.print(arr);
        System.out.println("-----------------");
        sort(arr);
    }
    /** * 选择排序 */
    private static void sort(Data[] arr) {
        for(int i=0;i<arr.length;i++) {
            int minIndex = i;
            for(int k=i;k<arr.length;k++) {
                if(arr[k].number<arr[minIndex].number) {
                    minIndex = k;
                }
            }
            if(minIndex!=i) {
                Data.exchange(arr, minIndex, i);
            }
            Data.print(arr);
        }
    }
}
  • 堆排序
package sort;

public class HeapSort {
    public static void main(String[] args) {
        Data[] arr = Data.getArr();
        Data.print(arr);
        System.out.println("-----------------");
        sort(arr);
    }
    /** * 堆排序 */
    private static void sort(Data[] arr) {
        for(int i=0;i<arr.length;i++) {
            int len = arr.length-i;
            createBigRootHeap(arr,len);
            Data.exchange(arr, 0, len-1);
            Data.print(arr);
        }
    }
    /** * @param arr 母数组 * @param length 子数组长度 */
    private static void createBigRootHeap(Data[] arr, int length) {
// 最后一个非叶子结点 length/2-1
        for(int i=length/2-1;i>=0;i--) {
            int lastNotLeafIndex = i;
            exchangeRoot(arr,length,lastNotLeafIndex);//完成lastNotLeafIndex节点以下的大根堆创建
        }
    }

    /** * 从某个节点开始,递归往下构建大根堆 * @param arr 母数组 * @param length 子数组长度 * @param lastNotLeafIndex 最后一个非叶子节点 */
    private static void exchangeRoot(Data[] arr,int length, int lastNotLeafIndex) {
        int leftChildIndex = 2*lastNotLeafIndex+1;
        int rightChildIndex = 2*lastNotLeafIndex+2;
        int maxValueIndex = lastNotLeafIndex;
        if(rightChildIndex<length&&arr[maxValueIndex].number<arr[rightChildIndex].number) {
            maxValueIndex = rightChildIndex;
        }
        if(leftChildIndex<length&&arr[maxValueIndex].number<arr[leftChildIndex].number) {
            maxValueIndex = leftChildIndex;
        }
        if(maxValueIndex!=lastNotLeafIndex) {
            Data.exchange(arr, maxValueIndex, lastNotLeafIndex);
            exchangeRoot(arr,length,maxValueIndex);
        }
    }
}
  • 归并排序
package sort;

public class MergeSort {
    private static Data[] temp;
    private static int index = 0;
    public static void main(String[] args) {
        Data[] arr = Data.getArr();
        temp = new Data[arr.length];
        Data.print(arr);
        System.out.println("-----------------");
        sort(arr);
    }
    /** * 归并排序 */
    private static void sort(Data[] arr) {
        partSort(arr,1);
    }
    private static void partSort(Data[] arr, int len) {
        if(len>=arr.length) {
            return;
        }
        for(int i=0,k=i+len;i<arr.length;i+=2*len,k=i+len) {
            int indexI=i;
            int indexK=k;
            int endI = (i+len-1);
            if(endI>=arr.length) {
                endI = arr.length-1;
            }
            int endK = (k+len-1);
            if(endK>=arr.length) {
                endK = arr.length-1;
            }
            while(indexI<=endI && indexK<=endK) {
                if(arr[indexI].number>arr[indexK].number) {
                    temp[index++] = arr[indexK++];
                }else if(arr[indexI].number<arr[indexK].number){
                    temp[index++] = arr[indexI++];
                }else {
                    temp[index++] = arr[indexI++];
                    temp[index++] = arr[indexK++];
                }
            }
            while(indexI<=endI) {
                temp[index++] = arr[indexI++];
            }
            while(indexK<=endK) {
                temp[index++] = arr[indexK++];
            }
            Data.print(temp);
        }
        System.out.println();
        index = 0;
        Data[] d = arr;
        arr = temp;
        temp = d;
        for(int i=0;i<temp.length;i++)temp[i] = null;
        partSort(arr, len*2);
    }
}
点赞