Java basics-exchange sort (bubble sort, quick sort)

1. Bubble sort

  • Principle: In a disordered interval, by comparing adjacent numbers, bubble the largest number to the end of the disordered interval, and continue this process until the array is ordered as a whole.

  • Stability (stable)
    1. A stable sequence can become an unstable sequence;
    2. A stable sequence can not become stable;

  • Space complexity: O(1)

  • Time complexity: best case O(n); worst case O(n^2)

//冒泡排序
    public static void bubbleSort(int[] arr){
    
    
        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]){
    
    
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
    }

2. Quick sort

  • Principle:
    1. Select a number from the interval to be sorted as the pivot;
    2. Partition: traverse the entire interval to be sorted, and place a value smaller than the benchmark (which can contain equal) to the left of the benchmark value, and compare The larger reference value (which can include equal values) is placed on the right side of the reference value;
    3. Using the divide and conquer idea, the left and right cells are treated in the same way until the length between the cells == 1, which means it is in order. Or the length between cells == 0, which means there is no data.
  • Stability (unstable)

​ 1. A stable sort can become an unstable sort;

​ 2. An inherently unstable one cannot become stable;

  • Space complexity: O(log2n)
  • Time complexity: Degree: O(n*logn)
//快速排序
public static void quicksort(int[] arr){
    
    
        quick(arr,0,arr.length-1);

    }
    //排序
    public static void quick(int[] arr,int low,int high){
    
    
        if(low > high){
    
    
            return;
        }
        //par是基准
        int par = parttion(arr,low,high);
        quick(arr,low,par-1);
        quick(arr,par+1,high);
    }
    //划分函数
    public static int parttion(int[] arr,int start,int end){
    
    
        //申请一个tmp空间用来放基准的值(基准一般选择的是边上的值)
        int tmp = arr[start];//这里取的是最左边的值,那么一会就先从最右边开始找[end]位置
        while(start < end){
    
    
            while(start < end && arr[end] >= tmp){
    
    
                end--;
            }
            //到了这里,从while出来有俩种情况,不满足start < end或者不满足arr[end] >= tmp
            if (start >= end){
    
    
                //arr[start] = tmp;
                break;//直接退出,因为已经遍历完毕
            }else{
    
    //else就是arr[end] < tmp
                arr[start] = arr[end];
            }
            while(start < end && arr[start] <= tmp){
    
    
                start++;
            }
            //到了这里,同样从while出来有俩种情况,不满足start < end或者不满足arr[start] <= tmp
            if (start > end){
    
    
                //arr[end] = tmp;
                break;
            }else{
    
    
                arr[end] = arr[start];
            }
        }
        arr[start] = tmp;
        return start;//返回基准移动到位置,这样基准的左边全小于它,右边全都大于它

3. Result test

public class TestSort {
    
    
//冒泡排序
    public static void bubbleSort(int[] arr){
    
    
        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]){
    
    
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
    }
//快速排序
public static void quicksort(int[] arr){
    
    
        quick(arr,0,arr.length-1);

    }
    //排序
    public static void quick(int[] arr,int low,int high){
    
    
        if(low > high){
    
    
            return;
        }
        //par是基准
        int par = parttion(arr,low,high);
        quick(arr,low,par-1);
        quick(arr,par+1,high);
    }
    //划分函数
    public static int parttion(int[] arr,int start,int end){
    
    
        //申请一个tmp空间用来放基准的值(基准一般选择的是边上的值)
        int tmp = arr[start];//这里取的是最左边的值,那么一会就先从最右边开始找[end]位置
        while(start < end){
    
    
            while(start < end && arr[end] >= tmp){
    
    
                end--;
            }
            //到了这里,从while出来有俩种情况,不满足start < end或者不满足arr[end] >= tmp
            if (start >= end){
    
    
                //arr[start] = tmp;
                break;//直接退出,因为已经遍历完毕
            }else{
    
    //else就是arr[end] < tmp
                arr[start] = arr[end];
            }
            while(start < end && arr[start] <= tmp){
    
    
                start++;
            }
            //到了这里,同样从while出来有俩种情况,不满足start < end或者不满足arr[start] <= tmp
            if (start > end){
    
    
                //arr[end] = tmp;
                break;
            }else{
    
    
                arr[end] = arr[start];
            }
        }
        arr[start] = tmp;
        return start;//返回基准移动到位置,这样基准的左边全小于它,右边全都大于它
}


//测试main方法
public static void main(String[] args) {
    
    
//冒泡排序
        int[] arr3 = {
    
    7,4,9,34,0,8,5,22,55,6,12,33,56,89,77};
        bubbleSort(arr3);
        System.out.print("冒泡排序结果: ");
        for (int i = 0;i < arr3.length;i++){
    
    
            System.out.print(arr3[i]+" ");
        }
        //快速排序
        int[] arr5 = {
    
    7,4,9,34,0,8,5,22,55,6,12,33,56,89,77};
        quicksort(arr5);
        System.out.print("快速排序结果: ");
        for (int i = 0;i < arr5.length;i++){
    
    
            System.out.print(arr5[i]+" ");
        }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45665172/article/details/109648630