- 2021-03-18 21:26
*views 5*- Java

<> Bubble sort

This is the bubble sort process . We can understand it as a bunch of numbers at first, for example ：4,1,7,9,6,8,5,3,2. So first we'll take the first number 4 And the second number 1 Compare , Place the big one in the original second digit position . Compare the second number with the third , Place the big one in the third position . The same goes back and forth , Compare the size to the last number , After the first round of comparative adjustment , The last number will become the largest of these numbers , mean 9.

But at the same time, we will find that , After the first round of comparison , Just take the largest of these numbers 9 Find and place in the last digit position of , But the other locations are still messy , Then repeat the first round , Until you find the largest number one at a time and put it last , In the end, all the sorting will be done .

The detailed code is as follows ：

public class Mp { int[] Arr={4,1,7,9,6,8,5,3,2}; public void mp() { int a; for(

int i=0;i<Arr.length-1;i++) { // The second round of cycle comparison only needs to compare the number of all numbers minus one for(int j=0;j<Arr.

length-1;j++) { // In the first round, you only need to compare the number of all numbers minus one if(Arr[j]>Arr[j+1]) {

// After comparison, place the larger number in the following number position a=Arr[j]; Arr[j]=Arr[j+1]; Arr[j+1]=a; } } } for(int i=0;i<

9;i++) { // Output the final sorting result System.out.println(Arr[i]); } } public static void main(

String[] args) { // Create an object to call the bubble sort method Mp zzz=new Mp(); zzz.mp(); } }

<> Insert sort

Compared to bubble sort , Insert sort is a little bit more complicated . We can see from the dynamic diagram above , The principle of insertion sort is to start with the second number , Compare the size with the first number , Place the small ones in the front number position , Go from the second number , Each number is compared with all the previous numbers , Stop until the comparison is larger than the previous number , Connect the number to this location .

Let's assume that our number combination is 4,1,7,9,6,8,5,3,2. So it's from 1 Start to compare forward , And then there's the 7, And then there is 9 wait , Until the last one 2 End of comparison with previous figures . The principle of comparison is that the selected numbers are compared with the previous numbers in turn , Until there are no numbers in front , Or larger than the previous number , stop , Place the number here .

The detailed code is as follows ：

public class Cr { int[] Arr=new int[] {4,1,7,9,6,8,5,3,2}; public void cr() {

for(int i=1;i<Arr.length;i++) {

// Because the comparison in the sequence starts from the second number 1 Start to compare forward , So the starting value of the cycle judgment i Set from 1 start , Not zero , Convenient for follow-up j Normal use of int j=i-1; int num=

Arr[i]; for(;j>=0;j--) { // The number will be taken out , Keep comparing with all the previous figures in turn , There are no numbers until the front , Or smaller than the previous number if(num<Arr[

j]) { Arr[j+1]=Arr[j]; }else break; } if(num != Arr[i]) {

// If the selected number is smaller than the previous one , It moves forward , The new position falls after the number that is smaller than it when compared in size Arr[j+1]=num; } } for(int

i=0;i<Arr.length;i++) { // Output the final sorting result System.out.println(Arr[i]); } } public

static void main(String[] args) { // Create an object to call the insert sort method Cr cr=new Cr(); cr.cr(); } }

Insertion sorting needs to be able to sort out the process of selecting a number and continuously comparing it forward , Any number smaller than it moves one bit backward , The number selected for forward comparison will eventually fall behind the smaller number in the comparison , You can figure out the logic of the code . Look at the top dynamic chart , It is helpful to understand the principle of insertion sort .

Technology

- Python156 blogs
- Java139 blogs
- Vue91 blogs
- Flow Chart80 blogs
- javascript44 blogs
- C++43 blogs
- algorithm41 blogs
- programing language40 blogs
- more...

Daily Recommendation

views 5

views 2

views 2

©2020-2021 ioDraw All rights reserved