Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the eight sorting algorithms commonly used in Java

Shulou Source: shulou.com Published: 2022-06-02 19:53:53 09月26日 Update

This article introduces the knowledge of "what are the eight sorting algorithms commonly used in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Directly insert sort

The main problem is to insert new data into already arranged data columns.

Sort the first number and the second number and then form an ordered sequence

Insert the third number to form a new ordered sequence.

For the fourth number, the fifth number. Until the last number, repeat the second step.

Directly insert sort

How to write it into code:

First of all, set the number of inserts, that is, the number of cycles, for (int iinsertNum) {/ / sequence from back to front loop, move the number greater than insertNum backward by a lattice a [insertNum 1] = a [j]; / / the element moves a lattice Jaffe;} a [jimp1] = insertNum;// put the number to be inserted in the position to be inserted. }} 2. Hill ranking

For the direct insertion sorting problem, when the amount of data is huge.

Set the number of numbers to n, take the odd number k=n/2, and divide the books with subscript difference k into a group to form an ordered sequence.

Then take k=k/2 and divide the books with subscript difference k into a group to form an ordered sequence.

Repeat the second step until kroom1 performs a simple insert sort.

Hill ranking

How to write code:

First of all, determine the number of groups.

Then insert and sort the elements in the group.

Then repeat the length/2 2 steps until length=0.

The code is implemented as follows:

Public void sheelSort (int [] a) {int d = a. Duration; while (dystopia 0) {dhampare 2; for (int x = 0; x)

< d; x++) {//分的组数 for (int i = x + d; i < a.length; i += d) {//组中的元素,从第二个数开始 int j = i - d;//j为有序序列最后一位的位数 int temp = a[i];//要插入的元素 for (; j >

= 0 & & temp < a [j]; j-= d) {/ / traverse from back to front. A [j + d] = a [j]; / / move the d bit backward} a [j + d] = temp;} 3. Simple selection sort

It is often used to take the largest and smallest numbers in a sequence.

(if you swap every comparison, it's a swap sort; if you compare one loop at a time and then swap, it's a simple selection sort.)

Traverse the entire sequence, putting the smallest number first.

Go through the rest of the sequence and put the smallest number first.

Repeat step 2 until there is only one number left.

Simple selection sort

How to write code:

First determine the number of cycles, and remember the current number and current location.

Compare all the numbers after the current position with the current number, assign the decimal value to key, and remember the position of the decimal.

When the alignment is complete, swap the smallest value with the value of the first number.

Repeat steps 2 and 3.

The code is implemented as follows:

Public void selectSort (int [] a) {int length = a.cycles; for (int I = 0; I < length; icycles +) {/ / cycles int key = a [I]; int position=i; for (int j = I + 1; j < length) If (a [j] < key) {key = a [j]; position = j;} a [position] = a [I]; / / exchange position a [I] = key;}} 4. Heap sort

Optimization of simple selection sorting.

Build the sequence into a large top heap.

Swap the root node with the last node, and then disconnect the last node.

Repeat steps 1 and 2 until all nodes are disconnected.

Heap sort

The code is implemented as follows:

Public void heapSort (int [] a) {System.out.println ("start sorting"); int arrayLength=a.length; / / cyclic heap building for (int iDeploy0transposition i0vicimuri -) {/ / k saves the node being judged int kumbi; / / if while exists in the child nodes of the current k node

Tags: Sort sequence number node code loop element order number of digits location selection minimum number move maximum large number data common algorithm Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Huawei MariaDB Docker