Bubble sort, selection sort, insertion sort (detailed process)

    1. Bubble sorting   

Bubble Sort

        It is a sorting algorithm based on comparison and exchange operations . The process of each round of bubbling starts from the first element, compares and exchanges this element with the next adjacent element , so that the larger element moves to the right (if the element is larger than the next element, then the two elements Swap; if the element is less than or equal to the next element, leave it unchanged). In this way, each round of bubbling process can determine that an element is placed in the correct position, and this element is the largest element among the remaining elements, and the correct position is the rightmost position among the remaining positions. This process is like bubbles floating, so it is called bubble sort.

1. Illustration

2. Code 
package com.kfm.it.Rank;



public class BubleSort {
    public static void main(String[] args) {
//1.先初始化数组
        int[] a = {45, 5, 4, 67, 54};
//外层循环,它决定一共走几趟

        for (int i = 0; i < a.length - 1; i++) {
//2.交换位置//内层循环,它决定每趟走一次
            for (int j = 0; j < a.length - 1; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }

        }
        printArr(a);
    }

    private static void printArr(int[] a) {
        for (int j = 0; j < a.length; j++) {
            System.out.print(a[j] + " ");
        }
        System.out.println();
    }
}
3. Complexity 

Time complexity: Worst case: O(N^2)
      Best case: O(N)
Space complexity: O(1)


2. Select sorting 

Basic idea: Find the smallest (large) element in the unsorted sequence, store it at the beginning of the sorted sequence, then continue to find the smallest (large) element from the remaining unsorted elements, and then put it at the end of the sorted sequence. And so on until all elements are sorted.

  1. Implementation steps for selection sorting:

    • Starting from the first element, compare it with the first element of the unsorted part, if the element is smaller, swap the position of this element with the first element of the unsorted part;
    • If the element is larger, the search continues backward until a smaller element is found or the end of the unsorted section is reached.
    • Swap the smaller element found with the first element of the unsorted part.
    • Repeat the above steps until all elements are sorted.
1. Illustration

2. Code 
package com.kfm.it.Rank;

public class SelectSort {
    public static void main(String[] args) {
        int[] a = {43,56,45,6,457,65,67};


        for (int i = 0; i < a.length - 1; i++) {
//从第二个数开始
            for (int j = i+1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }

        }
        printArr(a);

    }

    private static void printArr(int[] a) {
        for (int j = 0; j < a.length; j++) {
            System.out.print(a[j] + " ");
        }
        System.out.println();
    }
}
3. Complexity 

The time complexity of selection sorting: O(n^2), where n is the number of elements to be sorted. Because each time you need to traverse all the elements in the unsorted part to find the smallest (large) element.

Space complexity of selection sort: O(1), only constant-level extra space is needed to store temporary variables


3. Insertion sort 

Insertion sort and selection sort have the same effect in that they both have the idea of ​​creating a subarray on the original array . Both sorting methods will divide the original array into two parts: the array to be sorted and the sorted array. array.

1. Illustration

 2. Code
package com.kfm.it.Rank;

import java.util.Arrays;

public class chaSort {
    public static void main(String[] args) {
        int[] arr = {34, 3, 54, 5, 56, 5, 76, 45, 4, 5};
        for (int i = 1; i < arr.length; i++) {
            for (int j = i - 1;j >= 0;j--) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                } 
        }
        printArr(arr);

    }

    private static void printArr(int[] arr) {
        for (int j = 0; j < arr.length; j++) {
            System.out.print(arr[j] + " ");
        }
        System.out.println();
    }
}
3. Complexity 

Time complexity: O(N*N) in the worst case. At this time, the columns to be sorted are in reverse order, or close to reverse order. In the best case, it is
      O(N). At this time, the columns to be sorted are in ascending order, or close to ascending order. .
Space complexity: O(1)

Guess you like

Origin blog.csdn.net/pachupingminku/article/details/132163817