## Find element index location

**Basic search**

Find the index of the first occurrence of the element in the array based on the array element

`public class TestArray1 {`

The index of element 2 is found in the case, and the index is 7;

The result returned after running is correct

## Binary search

**Use a binary search to find the index where the element first appears in the array**

- Premise: the elements of the array must be ordered
- Idea: every time you look for the middle element, you can reduce the element size by half

Specific code implementation:

`public class TestArray2 {`

In the case, the search element is 40 and the index is 3;

The result returned after running is correct

## Eight sorting methods

## Bubble sort

**Principle: the array elements are compared in pairs, the positions are exchanged, and the large elements are put back. After a round of comparison, the largest element will be ranked at the end of the array**

- Illustration:

**Code part:**

The first round of sorting is carried out to see the effect:

`public class ArraySort1 {`

**The following is a multi round sorting:**

Code part

Stupid method: multiple for loop, more cumbersome, repeated loop, statement no nutrition, just have a look, mainly to be able to think of, to prepare for nested loops

`public class ArraySort1 {`

Using nested loops (statement reduction, no nonsense)

`public class ArraySort1 {`

**That’s all for bubble sorting~~~**

## Select sort

**Principle: start from the 0 index, and then compare with the following elements. Small elements are put forward. After a round of comparison, the smallest element will appear at the minimum index**

Illustration:

Code part: multi round sorting (before optimization)

`public class ArraySort2 {`

Optimization code: nested loop:

`public class ArraySort2 {`

**That’s all for selection sorting~~~**

## Direct insertion sort

**Principle: starting from index 1, the following element is compared with the previous bit. If it is less than the previous bit, it will be exchanged. If it is less than the previous bit, it will be circularly inserted into the previous ordered table to keep it in order**

(method 1): code implementation:

`public class ArraySort3 {`

(method 2) code implementation

`public class ArraySort3 {`

Because many places need to use the exchange of element values before and after, it is encapsulated into a method: the code is as follows:

```
public static void swapValue(int[] arr,int i,int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
```

Use your own packaging method:

`for (int i = 0; i < arr.length ; i++) {`

**That’s all for inserting Sort directly~~~**

## Shell Sort

**Hill sort is also called reduced incremental sort**

- Basic idea: firstly, the original table is grouped by increment HT; each sub file is sorted by direct insertion method. Similarly, the next increment HT / 2 is used to divide the file into self-examination and sort by direct insertion method. Until HT = 1, the whole file is in order.
- Key: choose the appropriate increment.
- Hill sorting algorithm 9-3: it can be realized through triple loop.

Figure:

**The array is divided into two groups according to the interval of 5 steps. Each group has two elements to compare the size. The small one is placed in the front, and the large one is placed in the back;

After sorting once, you can roughly put the smaller elements in the whole array at the front and the larger elements at the back. * *

The length of the following array is 8. The first interval is 4, the second interval is 2, and the third interval is 1

Code implementation (use Knut sequence, select increment reasonably)

`public class ArraySort4 {`

**That’s the Hill sort~~~**

## Quick sort

**Principle: divide and Conquer: compare the size, then partition**

- Take a number from the array as the base number
- Partition: put all books larger than or equal to this number to his right, and all books less than his number to his left.
- Repeat the second step for the left and right intervals until there is only one number in each interval.

**Realization ideas**

- The first pit is formed by excavating the datum number
- Find a number smaller than it from the back to the front, dig out the number and fill it into the previous pit
- Find a number larger or equal to it from the front to the back, and then dig out the number and fill it into the previous pit.
- Repeat the above two steps

Code implementation:

`public class ArraySort5 {`

**So much for quicksort~~~**

## Merge sort

**Merge sort is a method of sorting by using the idea of merging****Principle: if the initial sequence has n records, then it can be regarded as n ordered subsequences. The length of each subsequence is 1, and then merge two by two to get n / 2 ordered subsequences with length 2 or 1, and then merge them in pairs… This kind of sorting method is called 2-way merge sort until an ordered sequence of length n is obtained**

Code implementation:

`public class ArraySort6 {`

**That’s all for merge sort~~~**

## Cardinal sort

**Cardinal sort is different from previous sorts****The previous sort is done more or less by using comparison and moving records****However, the implementation of cardinal sort does not need to compare keywords, only need to “allocate” and “collect” keywords**

The following is illustrated by a diagram:

First sorting: grouping by bits

Results after grouping:

Then take out the elements one by one

The second sort: sort according to the number of ten

Then take out the elements in turn

The third round of sorting: sort according to the number of hundreds

Finally, take out all the elements:

Code implementation:

`public class ArraySort7 {`

**That’s all for cardinal sort~~~**

## Heap sort

**Heap sort is a sort algorithm designed by using the data structure of heap. Heap sort is a kind of selective sort**

- In this case, the maximum value of the whole sequence is the root node of the top of the heap.
- Exchange it with the last element, where the end is the maximum
- Then, the remaining n-1 elements are reconstructed into a heap, so that the next smallest value of n elements can be obtained
- After repeated execution, an ordered sequence can be obtained

Code implementation:

`public class ArraySort8 {`

**That’s all for heap sorting~~~**

## last

Thank you for seeing here. If you think the article is helpful to you, please give me a like. I will share Java related technical articles or industry information every day. You are welcome to pay attention to and forward the article!