Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the eight sorting algorithms in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what are the eight sorting algorithms in Java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First, directly insert sorting

Basic ideas:

Insert the new data into the already arranged data column.

Sort the first and second numbers to form an ordered sequence

Then insert the third number to form a new ordered sequence, and the following numbers repeat this step

Algorithm description

1. Set the number of inserts, that is, the number of cycles, for (int item1 / temp) {/ / determine the last number, move back one lattice of data [jumb1] = data [j] greater than the number to be inserted; / / move the element one lattice of Jmurf;} data [juli1] = temp / / put the number to be inserted into the position where 1 is inserted} for (int I = 0; I

< data.length; i++) { System.out.print(data[i]+" "); } }}二、希尔排序 基本思想: 对于直接插入的数,数据量巨大: 1.将数的个数设置为n,取奇数k = n/2,将下标的差值k的数分为一组,构成有序数列。 2.再取k = k/2,将下标差值为k的数构成一组,构成有序数列, 3.重复第二步,直到k=1执行简单的插入排序 算法描述 1.首先确定分组的数字 2.然后对组中的数字进行插入排序 3.然后将length/2,重复1,2步骤。直到length=0为止。 代码实现 public class Demo02 { public static void main(String[] args) { int [] data = {2,5,14,34,12,4,87,21,1,6}; int d = data.length; while (d!=0){ d = d/2; for (int x = 0; x < d; x++) { for (int i = d+x; i < data.length; i += d) { int j = i - d; //j为有序序列最后一位的位数 int temp = data[i]; //要插入的元素 for (;j>

= 0&&temp < data [j]; j-= d) {data [jargd] = data [j]; / / move backward d bit} data [jargd] = temp;} for (int I = 0; I < data.length) I choose +) {System.out.print (data [I] + ");} III. Simple selection sorting

Basic ideas:

It is often used to take the largest and smallest trees in the 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.)

1. Traverse the whole sequence, putting the smallest number first

two。 Traverse the rest of the sequence, putting the smallest number first

3. Repeat step 2 to know the last number left.

Algorithm description

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

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

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

4. Repeat steps 2 and 3

Code implementation

Public class Demo03 {public static void main (String [] args) {int [] data = {2phay 6, 123, 56, 23); for (int I = 0; I < data.length; icycles +) {/ / number of cycles int key = data [I]; / minimum int position=i; / / current position for (int j = iCo1; j < data.length) Int +) {/ / Select the minimum value while (data [j] = 0bot iMui -) {/ / k to save the currently judged node while k = I witch / if there is a child node in 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report