其他分享
首页 > 其他分享> > 几大排序法

几大排序法

作者:互联网

冒泡排序法

//冒泡排序法
public class BubbleSort {
    public static void main(String[] args) {
        int[] arr={88,66,44,55,77,99,11,33,22};
        int swap;//中间变量
        for (int i = 0; i <arr.length-1 ; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                //大的数向后沉
                if (arr[j]>arr[j+1]){
                    //移动
                    swap=arr[j+1];
                    arr[j+1]=arr[j];
                    arr[j]=swap;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}

堆排序

public class HeapSort {
    public void heap(int[] arr , int len){
        for (int i = len/2-1 ; i >= 0 ; i--) {
            int maxNode=i;
            if(arr[2*i+1]>arr[maxNode]){
                maxNode=2*i+1;
            }
            if(2*i+2 <len && arr[2*i+2]>arr[maxNode]){
                maxNode=2*i+2;
            }
            int swap=arr[maxNode];
            arr[maxNode]=arr[i];
            arr[i]=swap;
        }
    }

    public static void main(String[] args) {
        int[] arr = {66,77,88,11,22,55,99,33,44};
        HeapSort sort = new HeapSort();
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            sort.heap(arr,len-i);
            int swap=arr[len-1-i];
            arr[len-1-i]=arr[0];
            arr[0]=swap;
        }
        System.out.println(Arrays.toString(arr));
    }
}

直接插入排序

//直接插入排序
public class InsertSort {
    public static void main(String[] args) {
        int[] arr={99,88,77,66,55,44,33,22,11};
        int temp;//哨兵
        int j;//用于对前面已经排好序的子列遍历
        for (int i = 1; i < arr.length; i++) {
            temp=arr[i];
            j=i-1;
            for ( ; j >=0 && arr[j]>temp; j--) {
                arr[j+1]=arr[j];
            }
            arr[++j]=temp;
        }
        System.out.println(Arrays.toString(arr));
    }
}

简单选择排序

public class SelectSort {
    public static void main(String[] args) {
        int[] arr = {99,88,77,66,55,44,33,22,11};
        int swap;
        for (int i = 0; i < arr.length-1 ; i++) {
            int min=i;
            for (int j = i+1; j <arr.length ; j++) {
                if (arr[min]>arr[j]){
                    min=j;
                }
            }
            swap=arr[min];
            arr[min]=arr[i];
            arr[i]=swap;
        }
        System.out.println(Arrays.toString(arr));
    }
}

希尔排序

public class ShellSort {
    public void insertSort(int[] arr , int start , int d){
        int temp,j;
        for (int i = start+d; i <arr.length ; i+=d) {
            temp=arr[i];
            j=i-d;
            for ( ; j >= start && arr[j]>temp  ; j-=d) {
                arr[j+d]=arr[j];
            }
            int pos=j+d;
            arr[pos]=temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {99,88,77,66,55,44,33,22,11};
        ShellSort sort=new ShellSort();
        int d=arr.length/2;
        while(true){
            for (int i = 0; i < d ; i++) {
                sort.insertSort(arr,i,d);
            }
            if(d==1){
                break;
            }
            d/=2;
        }
        System.out.println(Arrays.toString(arr));

    }
}

标签:arr,temp,int,maxNode,swap,排序,public
来源: https://blog.csdn.net/qq_44371305/article/details/113058986