Java—Sorting

Bubble sort

//冒泡排序法
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));
    }
}

Quick sort

public class QuickSort {
    
    
    /**
     * 以第一个元素为中间元素,循环移动,
     * 使序列左面全部小于该中间元素,序列右面全大于该中间元素
     *
     * @param low  序列开头的索引
     * @param high 序列末尾的索引
     * @return 返回最后插入的中间元素的索引
     */
    public int partition(int[] arr, int low, int high) {
    
    
        int pivot = arr[low];//中间元素
        //循环移动
        while (low < high) {
    
    
            while (low < high && arr[high] >= pivot) {
    
    
                high--;
            }
            arr[low] = arr[high];
            while (low < high && arr[low] <= pivot) {
    
    
                low++;
            }
            arr[high] = arr[low];
        }
        //确定中间元素的位置
        arr[low] = pivot;
        //返回该中间元素的位置
        return low;
    }

    /**
     * 通过中间元素将序列划分为两个子列
     * 并对这两个子列分别递归访问,对其进行排序
     */
    public void sort(int[] arr, int low, int high) {
    
    
        if (low < high) {
    
    
            int pivotpos = partition(arr, low, high);
            //对左子列递归访问
            sort(arr, low, pivotpos - 1);
            //对右子列递归访问
            sort(arr, pivotpos + 1, high);
        }
    }
}

Heap sort

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));
    }
}

Direct insertion sort

//直接插入排序
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));
    }
}

Simple selection sort

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));
    }
}

Hill sort

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));

    }
}

Guess you like

Origin blog.csdn.net/qq_44371305/article/details/113058986