“This is the 12th day of my participation in the Gwen Challenge.

This is the first data structure and algorithm novice entry, come to experience the charm of the algorithm!

Prints the 32-bit binary code corresponding to an int

The int type takes up four bytes, a total of 32 bits. Int The value ranges from -2^31 to 2^ 31-1

Integer.MIN_VALUE = -2147483648The corresponding binary is:10000000000000000000000000000000
Integer.MAX_VALUE = 2147483647The corresponding binary is:01111111111111111111111111111111
Copy the code

The highest digit identifies positive and negative numbers, with 1 representing a negative number and 0 representing an integer

For example:

5 the corresponding binary 00000000000000000000000000000101

101 = 1 * 2^2 + 1 * 2^0 = 4 + 1 = 5

What about positive integers versus negative integers? Take a positive and take a negative plus 1

If you have a positive number 5, you have a negative number -5.

00000000000000000000000000000101 5Corresponding binary11111111111111111111111111111010The not11111111111111111111111111111011Gal.Copy the code

– 5 corresponding binary code is: 11111111111111111111111111111011

The code is as follows:

    private static void print(int num) {
        for (int i = 31; i >= 0; i--) {
            System.out.print((num & (1 << i)) == 0 ? "0" : "1");
        }
        System.out.println();
    }
Copy the code

Selection sort

Selection sort is to select the smallest number and place it in the specified position.

0 to n-1 From the 0th to n-1 bits, select a minimum number and place it in the 0th position.

1 to n-1 From the first digit to the n-1 digit, select a minimum number and place it in the first digit.

2 to n-1 From the second to n-1 bits, select a minimum number and place it in the second position.

public class Code03_SelectSort {

    /** ** 0 to n-1 Select a minimum number from the 0 to n-1 bits and place it to the 0 * 1 to n-1 select a minimum number from the 1 to n-1 bits and place it to the 1 * 2 to n-1 Select a minimum number from the 2 to n-1 bits and place it to the 2 */
    public static void selectSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        // The outer loop controls the number of loops
        for (int i = 0; i < N; i++) {
            // Assume that the first number of each loop is the smallest
            int mixValueIndex = i;
            // find the minimum number
            for (int j = i + 1; j < N; j++) {
                mixValueIndex = arr[j] < arr[mixValueIndex] ? j : mixValueIndex;
            }
            // Switch placesswap(arr, i, mixValueIndex); }}public static void swap(int[] arr, int i, int j) {
        int temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }

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

    public static void main(String[] args) {
        int[] arr = {10.5.8.2.1.4.9.3.2.0}; print(arr); selectSort(arr); print(arr); }}Copy the code

Bubble sort

Ideas:

Each time you compare the size of two adjacent numbers, switch them in pairs, and on the first trip, the largest number is at the far right of the array, and so on.

0 ~ N-1

0 ~ N-2

0 ~ N-3

0 ~ end

public class Code04_BubbleSort {

    * 0 ~ n-1 * 0 ~ n-2 * 0 ~ n-3 * 0 ~ end */
    public static void bubbleSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        // The outer loop controls how many times to loop
        for (int end = N - 1; end >= 0; end--) {
            // The memory loop controls pairwise swapping
            // 0 1 1 2 2 3...
            for (int second = 1; second <= end; second++) {
                if (arr[second - 1] > arr[second]) {
                    swap(arr, second - 1, second); }}}}public static void swap(int[] arr, int i, int j) {
        int temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }

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

    public static void main(String[] args) {
        int[] arr = {10.5.8.2.1.4.9.3.2.0}; print(arr); bubbleSort(arr); print(arr); }}Copy the code

Insertion sort

It’s like playing poker, the left side is sorted, each time a card comes, compare it from right to left, and then compare it from left to right.

0 ~ 0 is sorted (the first card is sorted)

Sequence 0 to 1

Sequence 0 to 2

Order 0 to 3

Sequence 0 to N

public class Code04_InsertSort {

    /** * Insert sort is similar to playing cards, each time the cards are inserted into the previous order, similar to insert. * /
    public static void insertSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        for (int end = 1; end < N; end++) {
            int newNumIndex = end;
            while ((newNumIndex - 1) > =0 && arr[newNumIndex - 1] > arr[newNumIndex]) {
                swap(arr, newNumIndex - 1, newNumIndex); newNumIndex--; }}}public static void insertSort2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        for (int end = 1; end < N; end++) {
            for (int pre = end - 1; pre >= 0 && arr[pre] > arr[pre + 1]; pre--) {
                swap(arr, pre, pre + 1); }}}public static void swap(int[] arr, int i, int j) {
        int temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }

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

    public static void main(String[] args) {
        int[] arr = {10.5.8.2.1.4.9.3.2.0}; print(arr); insertSort(arr); print(arr); }}Copy the code

Welcome everyone to pay attention to the public account (MarkZoe) to learn from each other and communicate with each other.