JAVA array sorting, objects and classes

array sorting

Array sorting can be divided into three types
1. Selection sort
2. Bubble sort
3. Insertion sort

selection sort

Selection Sort: Choose the smallest (largest) number in each round and put it first (behind).
code show as below:

import java.util.Arrays;

public class XuanZe {
    public static void main(String[] args) {
        int[] arr = {8, 2, 5, 7, 6};
        xz(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void xz(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int index = 0;
            for (int j = 1; j < arr.length - i; j++) {
                if (arr[j] > arr[index]) {
                    index = j;
                }
            }
            int temp = arr[index];
            arr[index] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = temp;
        }
    }
}

Bubble Sort

Bubble sort: Compare two adjacent elements in each round, put the smaller one in the front, and the larger one in the back.
code show as below:

import java.util.Arrays;

public class MaoPao {
    public static void main(String[] args) {
        int[] arr = {8, 2, 5, 7, 6};
        mp(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void mp(int[] arr) {
        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;
                }
            }
        }
    }
}

Insertion sort

Insertion sort: Divide the array into two parts, one part is already sorted, the other part is accessed one by one, and inserted into the appropriate place in the previous part.
Insertion sort uses subscripts like selection sort, so I won’t talk about it here.

Introduction to Objects and Classes

Java is an object-oriented language, and the basic unit of Java operation is the object.
Class/Type: Features and behaviors common to all objects of the same kind, abstracted into classes.
Object: An instantiated individual conforming to the class.
The relationship between classes and objects: first define/declare the class, and then create the object.

object usage

1. Define/declare a class

public class xxx {
    xxxx
}

2. Create object - main - method - exists in class

3. After you have an object, you can use and call related data. The object contains member variables and member methods.

this introduction

this represents the current object - this object
Which object calls the method, which represents which object
this can call its own member variable, when the member variable and the temporary variable have the same name, this cannot be omitted
this can also call its own member methods, this can be omitted
this can also call its own constructor, this (parameter), this cannot be omitted
Can only be placed on the first line of the constructor

Tags: Java

Posted by andrewtayloruk on Mon, 30 May 2022 15:19:42 +0530