基本排序算法

插入排序:


    /*      相对稳定的
     *      最好时,数列相对有序,时间复杂度O(n)
     *      最差:为双层嵌套循环,其时间复杂度为O(n2)
     */
    public static void sort(int[] array){
        for(int i=1;i<array.length;i++){
            int temp=array[i];
            int j=i;
            for(;j>0&&array[j-1]>temp;j--){
                array[j]=array[j-1];
            }
            array[j]=temp;
        }
    }

选择排序:


    /**
     *   其时间复杂度为O(n2)   
     *
     */
    public static void sort(int[] array){
        int length=array.length;
        for(int i=0;i<length;i++){
            int minIndex=i;
            //在当前值之后查找最小值的index
            for(int j=i+1;j<length;j++){
                if(array[j]<array[minIndex]){
                    minIndex=j;
                }
            }
            if(minIndex!=i){
                int temp=array[minIndex];
                array[minIndex]=array[i];
                array[i]=temp;
            }
        }
    }

堆建立与排序

```

/**
* 
* 堆:
*   1、插入操作:
*       树的高度为log(n+1),时间复杂度为O(logn)
*   2、删除操作:
*       树的高度为log(n+1),时间复杂度为O(logn)
*   3、堆排序:
*       进行n次插入,时间复杂度为O(nlogn)
*       遍历删除的时间n*logn,时间复杂度为O(nlogn)
*       平均时间复杂度为:O(nlogn)
*/
public class Test {
public static void main(String[] args) {
    Heap heap=new Heap(20);
    heap.insert(2);
    heap.insert(4);
    heap.insert(9);
    heap.insert(3);
    heap.insert(5);
    heap.printAll();
    heap.delete();
    heap.printAll();        
    heap.sort();
}
}

class Heap{
    private int[] element;
    public Heap(int maxsize){
        element=new int[maxsize+1];
        element[0]=0;
    }
    //判空
    public boolean isEmpty(){
        return element[0]==0;
    }
    //判满
    public boolean isFull(){
        return element[0]==element.length-1;
    }
    //插入
    public void insert(int value){
        if(isFull()){
            throw new IndexOutOfBoundsException("大顶堆已满");
        }
        if(isEmpty()){
            element[1]=value;
        }else{
            //确定下标
            int i=element[0]+1;
            while(i!=1&&value>element[i/2]){
                //若比父节点大时,父节点下移
                element[i]=element[i/2];
                i/=2;
            }
            element[i]=value;
        }
        element[0]++;
    }
    //大顶堆的删除
    //即删除最大元素
    public int delete(){
        if(isEmpty()){
            throw new IndexOutOfBoundsException("大顶堆已空");
        }
        //删除元素,先赋值为最后一个有效元素
        int deleteElement=element[1];
        element[1]=element[element[0]];
        element[0]--;
        int temp=element[1];
        //重建堆
        int parent=1;
        int child=2;
        //循环至叶子节点
        while(child<=element[0]){
            if(child<element[0]&&element[child]<element[child+1]){
                //如果左孩子的值小于右孩子,那么选择右孩子比较
                child++;
            }
            if(temp>element[child]){
                break;
            }else{
                element[parent]=element[child];
                //父节点下移,其子节点设为左孩子节点
                parent=child;
                child*=2;
            }       
        }
        element[parent]=temp;
        return deleteElement;
    }
    public void printAll(){
        for(int i=1;i<element[0]+1;i++){
            System.out.print(element[i]);
            if(i!=element[0]){
                System.out.print(",");
            }
        }
        System.out.println();
    }
    //堆排序
    public void sort(){
        if(isEmpty()){
            throw new RuntimeException("堆为空");
        }
        //依次删除元素,放入数组最后
        int size=element[0];
        for(int i=0;i<size;i++){
            int deleteElement=delete();
            element[element[0]+1]=deleteElement;
        }
        //输出
        for(int i=1;i<size+1;i++){
            System.out.print(element[i]);
            if(i!=size){
                System.out.print(",");
            }
        }
    }
}

归并排序:


/**
 * 
 * 归并排序是稳定排序,它也是一种十分高效的排序,能利用完全二叉树特性的排序一般性能都不会太差。
 *  java中Arrays.sort()采用了

一种名为TimSort的排序算法,就是归并排序的优化版本。
 *  每次合并操作的平均时间复杂度为O(n),
 *  总的平均时间复杂度为O(nlogn)。而且,归并排序的最好,最坏,平均时间复杂度均为O(nlogn)。
 * 
 * 参考:: <http://www.cnblogs.com/chengxiao/> 
 */
public class Test {
    public static void main(String[] args) {
           int []arr = {9,8,7,6,5,4,3,2,1};
           MergeSort m=new MergeSort();
           m.sort(arr);
           System.out.println(Arrays.toString(arr));
    }
}
class MergeSort {
    public static void sort(int []arr){
        //在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间
        int []temp = new int[arr.length];
        sort(arr,0,arr.length-1,temp);
    }
    private static void sort(int[] arr,int left,int right,int []temp){
        if(left<right){
            int mid = (left+right)/2;
            sort(arr,left,mid,temp);//左边归并排序,使得左子序列有序
            sort(arr,mid+1,right,temp);//右边归并排序,使得右子序列有序

            merge(arr,left,mid,right,temp);//将两个有序子数组合并操作
        }
    }
    private static void merge(int[] arr,int left,int mid,int right,int[] temp){
        int i = left;//左序列指针
        int j = mid+1;//右序列指针 
        int t = 0;//临时数组指针
        while (i<=mid && j<=right){
            if(arr[i]<=arr[j]){
                temp[t++] = arr[i++];
            }else {
                temp[t++] = arr[j++];
            }
        }
        while(i<=mid){//将左边剩余元素填充进temp中
            temp[t++] = arr[i++];
        }
        while(j<=right){//将右序列剩余元素填充进temp中
            temp[t++] = arr[j++];
        }
        t = 0;
        //将temp中的元素全部拷贝到原数组中
        while(left <= right){
            arr[left++] = temp[t++];
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值