当前位置:网站首页>Sort method -- bubble sort (use an array to sort a string of numbers from large to small or from small to large)
Sort method -- bubble sort (use an array to sort a string of numbers from large to small or from small to large)
2022-07-24 13:08: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 .( Most comments are in the code )
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 , The output result of each round through the inner circulation is convenient to observe its principle

边栏推荐
- The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28
- 24. Merge K ascending linked lists
- Step of product switching to domestic chips, stm32f4 switching to gd32
- EAS approval process related table
- ESP32ADC
- English语法_不定代词 - 概述
- FinClip 「小程序导出 App 」功能又双叒叕更新了
- Raspberry pie self built NAS cloud disk -- raspberry pie built network storage disk
- Roller_ Block default behavior_ Zero roll event compatible
- 七月集训(第24天) —— 线段树
猜你喜欢
随机推荐
Static attribute, super()
开山之作造假!Science大曝Nature重磅论文学术不端,恐误导全球16年
The price of domestic flagship mobile phones is nearly 6000, but they can't even beat iphone12. It's clear who users choose
Why does 2.tostring() report an error
LeadTools 22 kit LeadTools super set
SSM online campus album management platform
New applications of iSCSI and separation of storage services of NFS
26. Reverse linked list II
SSM online examination system including documents
【论文阅读】Mean teachers are better role models
An example of how to save various data types by using esp32 EEPROM library functions under Arduino framework
SSM online rental and sales platform multi city version
I realize large top stack with C I
树莓派自建 NAS 云盘之——树莓派搭建网络存储盘
Teach you how to use power Bi to realize four kinds of visual charts
猿人学第六题
【C语言】详细的文件操作相关知识
24. Merge K ascending linked lists
Raspberry pie self built NAS cloud disk -- raspberry pie built network storage disk
深圳地铁12号线第二批工程验收通过 预计7月28日试运行








