Java Basics: Common Algorithms (Sorting Algorithms)

1. Bubble sort

// bubbling sort
        int[] arr = {14,23,7,35,12,3,46};
        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 temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

2. Selection sort

// selection sort
        int[] arr = {14,23,7,35,12,3,46};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 1 + i; j < arr.length; j++) {
                if (arr[i] > arr[j]){
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

3. Insertion sort

int[] arr = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
        int NIndex = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] <= arr[i + 1]){
                NIndex = i + 1;
            }else {
                break;
            }
        }
        System.out.println(NIndex);

        for (int i = NIndex + 1; i < arr.length; i++) {
            int a = 0;
            for (int j = i - 1; j >= 0; j--) {
                if (arr[i - a] >= arr[j]){
                    break;
                }else {
                    int temp = arr[j];
                    arr[j] = arr[i - a];
                    arr[i - a] = temp;
                    a++;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

4. Quick Sort

4.1 Recursion

4.2 Recursion exercises

// 递归
    public static void main(String[] args) {
        int num = 5;
        int factorial = getFactorial(num);
        System.out.println(factorial);
    }

    private static int getFactorial(int num) {
        if (num == 1){
            return 1;
        }else {
            return num * getFactorial(num - 1);
        }
    }

Recursion is to continuously call methods into the stack layer by layer, then assign the value to the previous method through the exit, and pop out the stack layer by layer to get the final result.

4.3 Quick Sort

Notice: 

1. You must move the pointer on the right first, because only in this way can you ensure that the number exchanged with the reference number is smaller than the reference number, so that you can ensure that the numbers on the left are smaller than the reference number, and the numbers on the right are larger than the reference number. 

2. The pointer on the left needs to start from the starting position, that is, the position of the reference number, otherwise there may be a situation where j = i+1 is not exchanged.

public static void main(String[] args) {
        int[] arr = {6,2,1,7,9,3,4,5,10,8};
        quickSort(arr, 0, arr.length - 1);

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

    private static void quickSort(int[] arr, int i, int j) {
        int start = i;
        int end = j;

        if (start > end){
            return;
        }

        int baseNumber = arr[i];

        while (start != end){
            while (true){
                if (end <= start || arr[end] < baseNumber){
                    break;
                }
                end--;
            }

            while (true){
                if (end <= start || arr[start] > baseNumber){
                    break;
                }
                start++;
            }

            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
        }
        int temp = arr[i];
        arr[i] = arr[end];
        arr[end] = temp;

        quickSort(arr, i, start - 1);

        quickSort(arr, start + 1, j);
    }

 

Guess you like

Origin blog.csdn.net/Orange_sparkle/article/details/129302227