Java array sort: ratio size + bubble sort + selection sort + insertion sort

Java Daily

Department: **Big Data Development Department 6

Name: cqmfx (Tian Mo Feixu)

Date: 2020.10.25
Remarks: partly transferred from Baidu, CSDN, rookie, infringement deleted

Outline

One, array introduction

Second, sort

Array of things

One, array introduction

1. Basic concepts

Array (Array) is an ordered sequence of elements. If a limited set of variables of the same type is named, then this name is an array name . The variables that make up the array are called the components of the array, also called the elements of the array, and sometimes also called subscript variables. The number numbers used to distinguish the elements of the array are called subscripts (length) . Array is a form that organizes several elements of the same type in an orderly form in order to facilitate processing. These ordered collections of similar data elements are called arrays.

​ Array is a collection used to store multiple data of the same type .

2. The basic characteristics of the array:

​ (1) The length is determined. Once the array is created, its size is not changed in.
​ (2) Its elements must be of the same type , and mixed types are not allowed. The element type can be any type supported by Java
​ (3) The array type can be any data type, including basic types and reference types.
Element (4) of the array is in the heap memory allocated space , and are continuously allocated

​ (5) Use the new keyword to allocate memory to the array. Each element will be assigned a default value by jvm . Default rule: Integer: 0 Floating point: 0.0

	字符:\u0000 		布尔:false     引用数据类型:null。

​ (6) The elements of the array are all numbered , the number starts from 0, and the order is 0. Referred to as an array index, an index, the subscript

3. Array declaration:

(1) No object is instantiated when it is declared. Only when the array object is instantiated, the JVM allocates space, which is related to the length.
(2) No array is actually created when an array is declared.
(3) To construct an array, the length must be specified.

4. Array format:

4.1 , element type [] array name = new element type [number of elements or array length];

int [] arr = new int [5];
dataType[] arrayRefVar = new dataType[arraySize];

[]: Represents this is an array type.

4.2 Array name: a legal identifier, the naming convention is consistent with the local variable specification.

4.3 , new: is the keyword of java. Used to apply for memory from the JVM.
4.4 . Element type [number of elements]: determines the size of the memory space applied to the JVM.
          Size: Number of bytes of element type * Number of elements Number of elements
: As long as it is a valid java expression. Return an int type value

5. Multidimensional array

Multidimensional arrays can be regarded as arrays with arrays as elements . There can be two-dimensional, three-dimensional, or even more dimensional arrays, but they are rarely used in actual development. Up to two-dimensional array

Second, sort

0, find the maximum value (in the ring)

int [] array = {
    
    5,86,3,4,7,2,6};  //定义一个数组
//比大小(打擂主)
int temp = array [0];  //初始化temp
for (int i = 0;i <= 6; i++){
    
       //变量i初始化,运行0-6次,挨个两两比较
    if (temp < array[i]){
    
          //如果前一个(temp)比后一个(array[i]大)
        temp = array [i];      //则交换,最后得出最大(储存在temp里)的那个
    }
}
System.out.println(temp);

Insert picture description here

1. Bubble Sort (Bubble Sort)

1.1. Introduction

​ Bubble Sort (Bubble Sort) is one of the commonly used array sorting algorithms

​ The basic idea of bubble sorting is: compare adjacent element values, exchange element values ​​if the conditions are met , move the smaller element value to the front of the array , and move the larger element value to the back of the array (that is, exchange two the position of the element), such as array elements bubbles as rising from bottom to top

​ The bubble sort algorithm is relatively simple, the sorting result is stable, but the time efficiency is not very high

img

1.2, practical operation

        int bubbleSort [] = {
    
    5,9,3,7,8,22,1,45};  //定义数组
        //使用冒泡排序
        int temp = bubbleSort[0];   //初始化
        for (int i = 0;i < bubbleSort.length -1;i++){
    
       //控制外循环的轮数
            for (int j = 0;j<bubbleSort.length - i - 1;j++){
    
       //控制内循环两两之间比较的次数
                if (bubbleSort [j] > bubbleSort [j + 1]){
    
    
                    //两两比较  将大的冒泡到后面(><可控制大的\小的值放在前面、后面)
                    temp = bubbleSort[j];
                    bubbleSort [j] = bubbleSort [j + 1];    //比较后交换
                    bubbleSort [j + 1] = temp;
                }
            }
        }
        for (int i = 0;i < bubbleSort.length;i++){
    
    
            System.out.println(bubbleSort [i]);
        }

Insert picture description here

2. Selection Sort (SelctionSort)

2.1, the basic idea

​ In an unordered array of length N, first traverse n-1 numbers, find the smallest value and exchange with the first element;
​ traverse n-2 numbers for the second time, find the smallest value and the second Exchange of elements;
​ …
​ The n-1th traverse, find the smallest value and exchange with the n-1th element, the sorting is complete.

img

2.2, practical operation

        int[] selctionSort = {
    
    5, 9, 7, 12, 14, 1, 3};
        //选择排序
        for (int i = 0; i < selctionSort.length - 1; i++) {
    
            //重复排序过程
            int min = i;                                           //每次从第i次比较,这样可以不再和前面的元素比较
            for (int j = i + 1; j < selctionSort.length; j++) {
    
       //i+1是方便第i个元素和后面的比大小,就不会和自身比了
                if (selctionSort[j] < selctionSort[min]) {
    
            //每个数和下标为零的数比较,找到其中最小的数
                    min = j;                                      //然后将值大的下标赋给原来的零下标=得到了最小的值
                }                                                 //到这步就相当于选择好最小的元素了
            }                                                     //下面进行排序,就是一直将最小的数挨个放到最前面
            int temp = selctionSort[i];                           //进行排序:将最小的数放到temp里面
            selctionSort[i] = selctionSort[min];                  //将下标为零的数放到selctionSort [j]里面
            selctionSort[min] = temp;                            //再将数组中最小的数(元素)放到下标为0的元素那儿
        }                                                        //这样循环一直将剩下元素中最小的数放到前面
        for (int i = 0; i < selctionSort.length; i++) {
    
              //将排列好的数遍历打印出来
            System.out.println(selctionSort[i]);
        }

Insert picture description here

3. Insertion Sort (Insertion Sort)

3.1, the basic idea

​ In a group of numbers to be sorted, assuming that the first n-1 numbers are already in order, now insert the nth number into the front ordered number sequence so that these n numbers are also in order. Repeat this cycle until everything is in order.

The sorting is implemented in-situ on the array and the specific algorithm is described as follows:

  • Starting from the first element, the element can be considered to have been sorted;
  • Take out the next element and scan from back to front in the sequence of sorted elements;
  • If the element (sorted) is larger than the new element, move the element to the next position;
  • Repeat step 3 until you find the position where the sorted element is less than or equal to the new element;
  • After inserting the new element at that position;
  • Repeat steps 2~5.

3.2, practical operation

int [] insertionSort ={
    
    5,6,7,1,9,3,2};
for (int i = 0;i < insertionSort.length - 1;i++) {
    
    
    for (int j = i + 1; j > 0; j--) {
    
    
        if (insertionSort[j] < insertionSort[j - 1]){
    
    
            int temp = insertionSort[j - 1];
            insertionSort[j - 1] = insertionSort[j];
            insertionSort[j] = temp;
        } else {
    
    
            break;
        }
    }
}
for (int i = 0;i < insertionSort.length;i++){
    
    
    System.out.println(insertionSort[i]);
}

Insert picture description here

4. To be added

Guess you like

Origin blog.csdn.net/cqmfx/article/details/109307881