七大排序代码以及快排思路图解

插入排序

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

希尔排序

public static void shell(int[] array,int gap){
	for(int i = gap;i<array.length;i++){
		int tmp = array[i];
		int j = i-gap;
		for(;j>=0;j-=gap){
			if(array[j]>gap){
				array[j+gap] = array[j];
			}else{
				break;
			}
		}
		array[j+gap] = tmp;
	}
}
public static void shellSort(int[] array){
	int[] drr = {5,3,1};
	for(int i = 0;i < drr.length;i++){
		shell(array,drr[i]);
	}
}

选择排序

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

堆排序

public static void heapSort(int[] array){
        for (int i = (array.length-1-1)/2; i >= 0 ; i--) {
            adjustDown(array,i,array.length);
        }
        int end = array.length-1;
        while(end>0){
            int tmp = array[0];
            array[0] = array[end];
            array[end] = tmp;
            adjustDown(array,0,end);
            end--;
        }
    }
    public static void adjustDown(int[] elem,int root, int len) {
        int parent = root;
        int child = 2 * parent + 1;
        while (child < len) {
//判断是否有右孩子,且谁最大
            if (child + 1 < len && elem[child] < elem[child + 1]) {
                child = child + 1;
            }
            if (elem[child] > elem[parent]) {//child肯定是左右孩子最大值的下标
                int t = elem[child];
                elem[child] = elem[parent];
                elem[parent] = t;
                parent = child;
                child = 2 * parent + 1;
            }else{
                break;
            }
        }
    }

冒泡排序

public static void bubbleSort(int[] array){
	for(int i = 0;i<array.length-1;i++){//趟数
	boolean flg = false;
		for(int j= 0;j < array.length - 1-i;j++){//下标
			if(array[j]>array[j+1]){
				int tmp = array[j];
				array[j] = array[j+1];
				array[j+1] = tmp;
				flg = true;
			}
		}
		if(!flg){
			break;
		}
	}
}

快速排序


//快速排序,,,递归
    public static void quickSort(int[] array){

        quick(array,0,array.length-1);
    }
    public static void quick(int[] array,int low,int high) {
        if(high-low+1<=100){
            insertSort(array,low,high);
            return;//插入排序优化
        }

        threeNumMid(array,low,high);//三数取中法,优化

        int par = partion(array,low,high);//找到基准值
        System.out.println("low"+low);
        System.out.println("high"+high);
        if(par>low+1) {
            quick(array,low,par-1);//左边继续
        }
        if(par <high -1) {
            quick(array, par + 1, high);//右边继续
        }
    }
    
    //找基准值
    public static int partion(int[] array,int low,int high){
        int i = low;
         int j = high;
        int p = array[low];//基准值选取的是第一个元素
        while (i < j) {
            while (i < j && array[j] >= p) {
                j--;
            }
            //循环结束后,j就指向从右到左小于基准值的第一个数
            while (i < j && array[i] <= p) {
                i++;
            }
            //循环结束后,i就指向从左到右大于基准值的第一个数
            swap(array, i, j);//i和j都找到就交换
        }
        swap(array, i, low);//重合之后就交换
        return i;//找到基准值最后的位置
    }
    public static void insertSort(int[] array,int low,int high) {
        for (int i = low+1; i <= high; i++) {//从1开始
            int tmp = array[i];
            int j = i - 1;
            for (; j >= low; j--) {
                if(array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
	public static void threeNumMid(int[] array,int low,int high){
	        int mid = (low+high)/2;
	        if(array[mid]>array[low]){
	            swap(array,low,mid);
	        }
	        if(array[mid]>array[high]){
	            swap(array,high,mid);
	        }
	        if(array[low]>array[high]){
	            swap(array,low,high);
	        }
	    }
    public static void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    //快速排序,,非递归
    public static void quickSort1(int[] array){
        Stack<Integer> stack = new Stack<>();
        int low = 0;
        int high = array.length-1;
        int par = partion(array,low,high);
        if(par<low+1){
            stack.push(low);
            stack.push(par - 1);
        }
        if(par<high-1){
            stack.push(par+1);
            stack.push(high);
        }
        while(!stack.empty()){
            high = stack.pop();
            low = stack.pop();

            par = partion(array,low,high);
            if(par>low +1){
                stack.push(low);
                stack.push(par-1);
            }
            if(par<high-1){
                stack.push(par+1);
                stack.push(high);
            }
        }
    }

在这里插入图片描述

归并排序

//归并排序   递归
    public static void mergeSort(int[] array,int low,int high){
        if(low >= high || high - low == 1){
            return;
        }
        int mid = (low+high)/2;
        mergeSort(array, low, mid);//左区间递归归并
        mergeSort(array, mid+1, high);//右区间递归归并
        merge(array,low,mid,high);//进行比较归并
    }
    public static void merge(int[] array,int low,int mid,int high){
        int s1 = low;
        int s2 = mid+1;
        int len = high-low+1;
        int[] arr = new int[len];//创建一个新数组
        int i = 0;//记录新数组的下标变化
        while(s1 <= mid && s2 <= high){
            //拆分成两个部分,进行比较,
            if(array[s1]<=array[s2]){//此处必须用<=,这样才是稳定的
                arr[i++]=array[s1++];//s1小就放s1到arr[]中
            }else{
                arr[i++]= array[s2++];
            }
        }//循环结束,把剩余的元素加在arr[]的末尾
        while(s1<=mid){
            arr[i++]=array[s1++];
        }
        while(s2<=high){
            arr[i++] = array[s2++];
        }
        //最后一步,把排好序的数组拷贝回到原来的区间
        for (int j = 0; j < arr.length; j++) {
            array[low+j] = arr[j];
        }
    }

//归并排序非递归

    public static void merge1(int[] array,int gap) {
        int[] tmp = new int[array.length];
        int i = 0;
        int s1 = 0;
        int e1 = s1+gap-1;
        int s2 = e1+1;
        int e2 = s2+gap-1 >= array.length ? array.length-1 : s2+gap-1;

        while (s2 < array.length) {
            while (s1 <= e1 && s2 <= e2) {
               if(array[s1] <= array[s2]){
                   tmp[i++] = array[s1++];
               }else{
                   tmp[i++] = array[s2++];
               }
            }
            while(s1<=e1){
                tmp[i++] = array[s1++];
            }
            while(s2<=e2){
                tmp[i++] = array[s2++];
            }
            s1 = e2+1;
            e1 = s1+gap-1;
            s2 = e1+1;
            e2 = s2+gap-1>=array.length?array.length-1:s2+gap-1;
        }
        //判断s1是否有数据
        while(s1<=array.length-1){
            tmp[i++] = array[s2++];
        }
        //拷贝tmp到array
        for (int j = 0; j < tmp.length; j++) {
            array[j] = tmp[j];
        }

    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值