Bubble sorting, selection sorting, and quick sorting of arrays in Java

Bubbling, selection, and quick sorting of arrays in Java

1. Code and renderings

1. Bubble sort

The code is as follows (example):

public class Hello1 {
    
    
    public static void main(String[] args) {
    
    
        int arr[]={
    
    9,4,5,6,8,1,40};
        for (int i = 1; i <=arr.length-1; i++) {
    
    
            for (int j = 0; j <=arr.length-i-1; j++) {
    
    
                if (arr[j]>arr[j+1]){
    
    
                    int t=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=t;
                }
            }
        }
        //输出数组
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i]+" ");
        }
    }
}

Insert picture description here

2. Choose a sort

The code is as follows (example):

public class Hello1 {
    
    
    public static void main(String[] args) {
    
    
 int arr [] ={
    
    1,90,56,123,9,8,6,7,2,8,1};
        for (int i = 0; i <arr.length-1 ; i++) {
    
    
            int minIndex=i;//最小下标默认值
            //1.从i到len-1这个范围中找最小值下标
            for (int j = i; j <=arr.length-1 ; j++) {
    
    
                if (arr[j]<arr[minIndex]){
    
    
                    minIndex=j;
                }
            }
            //2.交换元素
            int t=arr[i];
            arr[i]=arr[minIndex];
            arr[minIndex]=t;

        }
        //输出数组
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i]+" ");
        }
    }
}

Insert picture description here

3. Quick sort

The code is as follows (example):

import java.util.Arrays;

public class TestBubble {
    
    
    public static void main(String[] args) {
    
    
        fun3();
    }

    private static void fun3() {
    
    
        int arr []={
    
    8,5,0,1,4};
        int[] arr2 = quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr2));
    }
    // 快 速
    private static int [] quickSort(int arr[],int l,int r) {
    
    
        int X=arr[l];
        int i=l,j=r;
        while (i<j){
    
    
            //从右向左,找出比X小的数的下标 j
            while (i<j &&arr[j]>X){
    
    
                j--;
            }
            //从左向右,找出比X大的数的下标
            while (i<j &&arr[i]<X){
    
    
                i++;
            }
            if(arr[i]==arr[j]&&i<j){
    
    
                i++;
            }else {
    
    
                int t=arr[i];
                arr[i]=arr[j];
                arr[j]=t;
            }
        }
        if(i-1>l){
    
    
            arr=quickSort(arr,l,i-1);
        }
        if(j+1<r){
    
    
            arr=quickSort(arr,j+1,r);
        }
        return arr;
    }
}

Insert picture description here

to sum up

The above is the whole content of bubble sorting, selection sorting and quick sorting in the array, mainly using arrays, loop nesting, subscript and numerical exchange and other methods to achieve sorting.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/110670693