当前位置:网站首页>Sorting method: bubble sorting (use an array to arrange a string of numbers in order (from large to small or from small to large))
Sorting method: bubble sorting (use an array to arrange a string of numbers in order (from large to small or from small to large))
2022-07-24 13:09:00 【Cording..】
The basic idea of bubble sorting : By treating the sort sequence from front to back , Compare the sorting codes of adjacent elements in turn
If reverse order is found, exchange , Make the elements with larger sorting code gradually move from the front to the back .
package com.atcording.java;
/*
* Implementation of bubble sorting of array
* The basic idea of bubble sorting : By treating the sort sequence from front to back , Compare the sorting codes of adjacent elements in turn
* If reverse order is found, exchange , Make the elements with larger sorting code gradually move from the front to the back .
*/
public class BubbleSort {
public static void main(String[] args){
int[] arr=new int[]{2,450,63,12,78,92,438,-32,21,79};// Define an unordered array to be sorted ;
/*
* Set up a two-layer loop , The inner loop finds out the maximum number participating in the loop once every cycle, and arranges it in the last position ,
* In the second cycle, find the second largest number and arrange it in the penultimate position , By analogy , The purpose of outer circulation is to ensure
* The number of times the inner loop is carried out ( For example, ten numbers are sorted from small to large , Each time the inner loop is executed, find a maximum number and rank last ,
* In the second cycle, find the second largest number and arrange it in the penultimate position , By analogy , In order to arrange the size of ten numbers, we need to carry out this inner loop
* therefore i<arr.length It refers to the inner circulation to be carried out arr.length-1 Time ,arr.length Is to get an array arr The length of is 10)
*/
for(int i=0;i<arr.length;i++){//i<arr.length It refers to the inner circulation to be carried out arr.length-1 Time
for( int j=0;j<arr.length-1;j++){
if(arr[j]>arr[j+1]){
// Compare two adjacent numbers , If the number in front is greater than the number in the back, exchange positions
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
// Each time the inner loop is run, the result is output , Convenient for observation , To comment out
System.out.print(" The inner layer circulates for the first "+(i+1)+" The results of the round are as follows ");
System.out.println();// Line break
for(int b=0;b<arr.length;b++){
System.out.print(arr[b]+" ");
}
System.out.println();// Line break
}
// Output final results
System.out.print(" The final results are as follows :");
System.out.println();// Line break
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
The results are shown in the following figure , Through each round of inner circulation After that, output the arrangement results to observe the principle of bubble sorting

边栏推荐
- 26. Reverse linked list II
- Solutions to problems in IE6 browser
- [datasheet] interpretation of phy lan8720 network chip
- C code specification
- This is how the permission system is designed, yyds
- Static attribute, super()
- Data + AI summit 2022 PPT download
- 30. Rearrange the linked list
- 登临科技联合创始人王平:创新+自研“双核”驱动,GPU+赋能AI落地生根|量子位·视点分享回顾...
- I 用c I 实现 大顶堆
猜你喜欢

Vscode solves the problem of terminal Chinese garbled code

深圳地铁12号线第二批工程验收通过 预计7月28日试运行

手把手教你用 Power BI 实现 4 种可视化图表

Data + AI summit 2022 PPT download
![[stm32] internal independent watchdog iwdg](/img/7f/752528e9a76097b51c8f9463c33db3.png)
[stm32] internal independent watchdog iwdg
![[datasheet] interpretation of cs5480 data book of metering chip](/img/1a/e8a4ce5c393a6634b6dc8bf6d687e2.png)
[datasheet] interpretation of cs5480 data book of metering chip

The setting float cannot float above the previous Div

C language course design -- hotel management system
This is how the permission system is designed, yyds

FinClip 「小程序导出 App 」功能又双叒叕更新了
随机推荐
An example of how to save various data types by using esp32 EEPROM library functions under Arduino framework
[datasheet] PHY ksz9031 gigabit network chip interpretation
The seventh topic of ape Anthropology
Activity start (launchactivity/startactivity)_ (1)_ WMS of flow chart
Case summary of SSH service suddenly unable to connect
LeadTools 22 kit LeadTools super set
2022.07.21
24. Merge K ascending linked lists
Custom scroll bar
Use of PageHelper
Data + AI Summit 2022 PPT 下载
C代码规范
Analysis of ISP one click download principle in stm32
Leetcode's 302 weekly rematch
Cluster construction based on kubernetes v1.24.0 (I)
如何使用autofs挂载NFS共享
The MySQL select delay scenario corresponds to that all database query statements will be delayed. After the scenario injection, I executed one
Raspberry pie self built NAS cloud disk -- automatic data backup
Redis(13)----浅谈Redis的主从复制
Teach you how to use power Bi to realize four kinds of visual charts