十大经典排序算法

近期博主去实习了5个月了,尤其感觉算法的重要性,加上博主求职招聘中偶感,计算机类的面试大同小异,万变不离其中,算法是基础,无论工作中需不需要用到,但是深刻理解这十种算法,不说走遍天下不怕,面试成功大部分公司是没问题的。因此决定再次下苦工重温一次常用的算法。

先贴一张各个算法的复杂度比较

一,插入算法:

public static void insertSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = i; j > 0; j--) {
                if(array[j] < array[j - 1]){
                    int temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }
            }
        }
    }

如果你这样写就只是对了一部分,这个代码还没有达到最优,为什么呢?因为就算是已经排好序的情况(最好的情况),上面的代码的时间复杂度还是O(n^2),而实际上插入排序的最好的情况下时间复杂度应该为O(n)。

插入排序的思想是:

因此实际上代码应该这样写:

 public static void insertSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int j=i-1;
            int temp=array[i];
            while(j>=0&&temp<array[j]){
                array[j+1]=array[j];
                j--;
            }
            array[j+1]=temp;
        }
    }

二,选择排序

思想:顾名思义就是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。代码如下:

public static void selectSort(int[] array){
        for(int i=0;i<array.length;i++){
            int minIndex=i;
            for(int j=i+1;j<array.length;j++){
                if(array[minIndex]>array[j]){
                    minIndex=j;
                }
            }
            if(minIndex!=i){
                int temp=array[i];
                array[i]=array[minIndex];
                array[minIndex]=temp;
            }
        }
    }

三,冒泡排序

这是最原始的写法

public static void BulleSort(int[] array){
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array.length-i-1;j++){
                if(array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
    }

可以看到这个写法最好的情况(已经排好序)时间复杂度还是O(n^2),这不符合冒泡排序的复杂度要求,那怎么办呢?答案就是加上一个标记位,代码如下:

public static void BulleSort(int[] array){
        for(int i=0;i<array.length;i++){
            boolean flag=false;
            for(int j=0;j<array.length-i-1;j++){
                if(array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                    flag=true;
                }
            }
            if(flag==false){//证明没交换过位置(即原本就是排好序的了)
                return;
            }
        }
    }

四,快速排序:

方法一:递归方法的实现

主要参考了以下的博客,附有图文,看完之后花3分钟就能自己写出来了,主要代码如下:

public void quickSort(int[] array,int low,int high){
		//如果数组只有一个数
		if(low>=high){
			return;
		}
		int i=low;
		int j=high;
		int key=array[low];
		while(i<j){
			while(i<j&&array[j]>key){
				j--;
			}
			while(i<j&&array[i]<=key){
				i++;
			}
			//交换
			if(i<j){
				int temp=array[i];
				array[i]=array[j];
				array[j]=temp;
			}
		}
		int temp=array[low];
		array[low]=array[i];
		array[i]=temp;
		quickSort(array, low, i-1);
		quickSort(array, i+1, high);
	}

方法二,非递归方法的实现

public class QuickSort {
    public static void quickSort(int[] array,int low,int high){
        Stack<Order> stack=new Stack<>();
        stack.add(Order.valueOf(low,high));
        while (!stack.empty()){
            Order order=stack.pop();
            int position=position(array,order.getLow(),order.getHigh());
            if(order.getLow()<position-1){
                stack.push(Order.valueOf(low,position-1));
            }
            if(order.getHigh()>position+1){
                stack.push(Order.valueOf(position+1,high));
            }
        }
    }

    public static int position(int[] array,int low,int high){
        int i=low;
        int j=high;
        int key=array[low];
        if(i>j) return -1;
        while (i<j){
            while (i<j&&array[j]>key){
                j--;
            }
            while (i<j&array[i]<=key){
                i++;
            }
            int temp=array[i];
            array[i]=array[j];
            array[j]=temp;
        }
        int temp=array[i];
        array[i]=key;
        array[low]=temp;
        return i;
    }
}
class Order{
    private int low;
    private int high;

    public int getLow() {
        return low;
    }

    public int getHigh() {
        return high;
    }

    public void setLow(int low) {
        this.low = low;
    }

    public void setHigh(int high) {
        this.high = high;
    }

    public static Order valueOf(int low,int high){
        Order order=new Order();
        order.setLow(low);
        order.setHigh(high);
        return order;
    }
}

五,堆排序

package algorithm;

public class HeapSort {

    public static void main(String[] args){
        int[] array={42,6,8,9,4,5,67,78,87,0,87,90,9089,56,890};
        heapSort(array);
        for(int value:array){
            System.out.println(value);
        }
    }
    public static void heapSort(int[] array) {
        //初始化
        for(int i=array.length/2-1;i>=0;i--){
            heapAdjust(array,i,array.length-1);
        }
        for(int i=0;i<array.length-1;i++){
            swap(array,0,array.length-1-i);
            heapAdjust(array,0,array.length-2-i);
        }
    }

    public static void heapAdjust(int[] array,int start,int length){
        int parent=start;
        int left=2*parent+1;
        while (left<=length){
            //选择左右节点最大的那个
            if(left+1<=length&&array[left+1]>array[left]){
                left++;
            }
            if(array[left]<=array[parent]) return;
            //交换
            swap(array,parent,left);
            parent=left;
            left=2*parent+1;
        }
    }
    public static void swap(int[] array,int a,int b){
        int teap=array[a];
        array[a]=array[b];
        array[b]=teap;
    }
}

整理了一下网上看的博客,特记录一下链接以便以后需要

shell排序:点击打开链接

快速排序:点击打开链接

堆排序:点击打开链接

归并排序:点击打开链接

桶排序:点击打开链接

计数排序:点击打开链接

基数排序:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值