In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.