Data structure: common sorting algorithm (3) - exchange sort (bubble sort, quick sort)

(1) Bubble sort
① Principle:

In the 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 in order as a whole

②Code implementation:
import java.util.Arrays;
//冒泡排序:  时间复杂度O(n^2)  空间复杂度:O(1)
public class bubbleSort {
    
    
    public static void main(String[] args) {
    
    
        int[] array={
    
    5,8,1,2,5,22,44,15,3,0,48};
        System.out.println(Arrays.toString(array));
        bubble(array);
        System.out.println(Arrays.toString(array));
    }
    public static void bubble(int[] array){
    
    
        for (int i = 0; i < array.length - 1; i++) {
    
    
            boolean isSorted = true;
            for (int j = 0; j < array.length - i - 1; j++) {
    
    
                // 相等不交换,保证稳定性
                if (array[j] > array[j + 1]) {
    
    
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                    isSorted = false;
                }
            }
            if (isSorted) {
    
    
                break;
            }
        }
    }
}

Run screenshot:
Insert picture description here

③Performance analysis

Insert picture description here** Stability: ** Stable

(2) Quick sort
①Principle-partition:

1. Select a number from the interval to be sorted as the reference value (pivot) ;
2. Partition: traverse the entire interval to be sorted, and place the value smaller than the reference value (which can include equal) to the left of the reference value, which will be more than the reference value The larger one (which can include equal ones) 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 that the cells are in order or the cells are in order. The length of the interval == 0, which means there is no data.

Insert picture description here
Insert picture description here

②Code implementation:
import java.util.Arrays;

/**
 * 快速排序  时间复杂度:O(n*log2n)~O(n^2)    空间复杂度:O(log2n)~O(n)
 *稳定性:不稳定
 */
public class quickSort {
    
    
    public static void main(String[] args) {
    
    
        int[] array={
    
    4,8,7,5,12,14,0,3,36,24};
        System.out.println(Arrays.toString(array));
        quickSort1(array);
        System.out.println(Arrays.toString(array));
    }
    //快速排序函数
    public static void quickSort1(int[] array){
    
    
        quick(array,0,array.length-1);  //接口
    }
    public static void quick(int[] array,int low,int high){
    
    
        if(low<high){
    
    
            int piv=pivot(array,low,high);
            quick(array,low,piv-1);   //递归实现
            quick(array,piv+1,high);
        }
    }
    //找基准的函数
    public static int  pivot(int[] array,int start,int end){
    
    
        int tmp=array[start];
        while(start<end){
    
    
            while(start<end && array[end]>=tmp){
    
    
                end--;
            }
            //把数值赋值给start
            array[start]=array[end];
            while(start<end && array[start]<=tmp){
    
    
                start++;
            }
            //把start下标的值给end
            if(start>=end){
    
    
                break;
            }else{
    
    
                array[end]=array[start];
            }
        }
        array[start]=tmp;
        return start;
    }
}

Run screenshot:
Insert picture description here

Code implementation after optimization (the third number is the middle method):
/**
 * @Author: XiShanShan
 * @Description:
 * @Date:Created in 20:21 2021/3/30
 * @Modified By:xss666
 */

import java.util.Arrays;

/**
 * 快速排序  时间复杂度:O(n*log2n)~O(n^2)    空间复杂度:O(log2n)~O(n)
 *稳定性:不稳定
 */
public class quickSort {
    
    
    public static void main(String[] args) {
    
    
        int[] array=new int[1_0000];
        for (int i = 0; i < array.length; i++) {
    
    
            array[i]=i;
        }
        quickSort1(array);
    }
    //快速排序函数
    public static void quickSort1(int[] array){
    
    
        long startTime=System.currentTimeMillis();
        quick(array,0,array.length-1);  //接口
        long endTime=System.currentTimeMillis();
        System.out.println(endTime-startTime); //输出排序所需要的时间
    }

    public static void swap(int[] array,int k,int i){
    
    
        int tmp=array[k];
        array[k]=array[i];
        array[i]=tmp;
    }

    //三数取中法优化
    public static void medianOfThree(int[] array,int low,int high){
    
    
        int mid=(low+high)/2;

        if(array[low]<=array[mid]){
    
    
            swap(array,low,mid);
        }//mid<=low

        if(array[low]>array[high]){
    
    
            swap(array,low,high);
                }//array[low]<=array[high]

        if(array[mid]>array[high]){
    
    
            swap(array,mid,high);
        }  //array[mid]<array[high]
    }
    public static void quick(int[] array,int low,int high){
    
    
        if(low<high){
    
    

            //优化后
            medianOfThree(array,low,high);
            int piv=pivot(array,low,high);
            quick(array,low,piv-1);   //递归实现
            quick(array,piv+1,high);
        }
    }
    //找基准的函数
    public static int  pivot(int[] array,int start,int end){
    
    
        int tmp=array[start];
        while(start<end){
    
    
            while(start<end && array[end]>=tmp){
    
    
                end--;
            }
            //把数值赋值给start
            array[start]=array[end];
            while(start<end && array[start]<=tmp){
    
    
                start++;
            }
            //把start下标的值给end
            if(start>=end){
    
    
                break;
            }else{
    
    
                array[end]=array[start];
            }
        }
        array[start]=tmp;
        return start;
    }
}

The optimized code sorting speed will be greatly improved:

After sorting an array with a data size of 1_0000, the time is saved about 26ms

③Performance analysis

Insert picture description hereStability: unstable

Guess you like

Origin blog.csdn.net/qq_47364122/article/details/115331277