Java array sorting method _ Java array sorting method detailed

An array is actually a container with a given size and a given type. In this container, there are elements you set. You can arrange these elements in various ascending and descending arrangements, or find out special elements and perform a series of operation. The array itself is an ordered sequence of elements. In this article, we will take a look at several methods for sorting Java arrays.

1. Bubble sort

Take a chestnut: 5 2 0 4 1 3 An array with a capacity of seven is sorted in ascending order (the same is true for descending order, the maximum value becomes the smallest, it is ok)

①Find the bubbling (the largest) from this set of numbers, and kick it to the last 2 0 4 1 3 5

②Repeat the above behavior 2 0 1 3 4 5

③ Repeat~~ 2 0 1 3 4 5

④~~0 1 2 3 4 5

⑤~~0 1 2 3 4 5 Done

Java code example:

dfd7e45036708fb67d699eaa3491c74a.png

Second, the selection sort

Or the chestnut just now: 5 2 0 4 1 3 ascending order

①Find the smallest one in the array and put it in the first position 0 5 2 4 1 3

② Find the smallest one in the array and put it in the second position 0 1 5 2 4 3

③ ~~ 0 1 2 5 4 3

④~~ 0 1 2 3 5 4

⑤~~0 1 2 3 4 5 Done

Java code example:

74c997fe675a1eea48e9855848859193.png

3. Insertion sort

Personally, I think that the pile of cards on the poker table is your array one by one, and the sorting of your array is completed. This time your deck is 5 2 0 4 1 3

, add cards to the hand in turn

①Touch a 5 5

② Touch a 2, put 5 on the left, 2 5

③~~ 0 2 5

④~~ 0 2 4 5

⑤~~ 0 1 2 4 5

⑥~~ 0 1 2 3 4 5

Java code example:

e9cc7cc493959fc145dbc3812d19fce2.png

4. Hill sort

Hill sort is actually a kind of interval exchange. This time I use a long chestnut to let everyone understand my intention. An array with ten elements

43 95 38 30 41 72 60 74 24 32

①First of all

I have intervals of 5. The first is compared with the sixth, the second is compared with the seventh, the third is compared with the eighth... The ascending order does not change, the descending order replaces the position. (For example, the first 43 and the sixth 72 do not change the position in ascending order, and the second 95 and the seventh 60 replace the position in descending order)

get permutation result 43 60 38 24 32 72 95 74 30 41

② Then we use 3 as the interval, that is to say, similar to the first and fifth comparisons, the method is the same as above.

4164b8c47e07ee97ae96f90530bd6c92.png

get permutation result 24 32 30 41 60 38 43 74 72 95

③ Then we use 2 as the interval to get the permutation result:

24 32 30 38 43 41 60 74 72 95

④ Finally, the result is very simple with 1 as an interval. 24 30 32 38 41 43 60 72 74 95

It is not difficult for you to find that the way I take the interval is like the code below, repeating the int

k/2, the interval selection is not unique. For different arrays, you can use different intervals according to your own experience.

The Java code is as follows

a4fe38fc41fc46d83e615202da3793e6.png

Now the program finishes writing a random array of ten elements and prints out the order as follows:

28e39154784fcd82d1fd27ae391b41dc.png

Although Hill sort has high efficiency for most array sorting, its stability is worrying. It is recommended that you use insertion sort when writing files, which is both efficient and algorithmically stable.

The above are the four Java array sorting methods introduced for you, which can quickly sort the array data for us when we process the data in the array. Of course, there are other sorting algorithms in Java. Interested partners can watch the Java basics tutorial on this site to learn other Java array sorting algorithms.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324075413&siteId=291194637