当前位置:网站首页>Bubble sort summary
Bubble sort summary
2022-07-05 05:08:00 【ximen502_】
The content and code of this article are from 《 Comics algorithm 》, Dialogue between Xiaohui and rhubarb , A very interesting book . Combining theory with practice , Do a test .
private static final int LEN = 20000;
// The first edition
void bubbleV1(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
int temp = 0;
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
log(" The first " + (i + 1) + " round ");
log(Arrays.toString(arr));
}
}
/** * In the first edition, if the element passes through less than arr.length-1 Round comparison has become orderly , The remaining rounds will still be compared , * To overcome this shortcoming , Add a to judge whether the array has been ordered boolean Variable . The second edition * * @param arr */
void bubbleV2(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
boolean isSorted = true;
for (int j = 0; j < arr.length - 1 - i; j++) {
int temp = 0;
log("compare " + arr[j] + " with " + arr[j + 1]);
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSorted = false;
}
}
if (isSorted) {
break;
}
log(" The first " + (i + 1) + " round ");
log(Arrays.toString(arr));
}
}
/** * The third edition [3,2,1,4,5,6,7,8] [2,1,3,4,5,6,7,8] [1,2,3,4,5,6,7,8] * Suppose such an initial array , from 4 The starting elements are already orderly , But I have made many comparisons for nothing . It is a point that needs to be optimized . The key point of this problem is to define the ordered region in the array . * According to the existing logic , The length of the ordered region and , The number of rounds of sorting is equal . For example, the length of the ordered area after the first round of sorting is 1, The length of the ordered region after the second round of sorting is 2...... * In fact, the length of the ordered region may be greater than this length , In the above example , In the second round of sorting , hinder 5 Elements already belong to the ordered region . Therefore, the following multiple element comparisons are meaningless . * Avoid it : After each round of sorting , Record the location of the last element exchange , This position is the boundary without sequence , And then there's the orderly area . * * @param arr */
void bubbleV3(int[] arr) {
int lastExchangedIndex = 0;
int sortBorder = arr.length - 1;
for (int i = 0; i < arr.length - 1; i++) {
boolean isSorted = true;
for (int j = 0; j < sortBorder; j++) {
int temp = 0;
log("compare " + arr[j] + " with " + arr[j + 1]);
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSorted = false;
lastExchangedIndex = j;
}
}
sortBorder = lastExchangedIndex;
log("border:" + sortBorder);
if (isSorted) {
break;
}
log(" The first " + (i + 1) + " round ");
log(Arrays.toString(arr));
}
}
/** * The Fourth Edition ( Cocktail ordering ) [2,3,4,5,6,7,8,1] The above example , The first three version sorting methods need to be sorted 7 round , However, most of the data are orderly . * To overcome this shortcoming . * * @param arr */
void bubbleV4(int[] arr) {
int temp = 0;
for (int i = 0; i < arr.length / 2; i++) {
// Orderly marking , The initial value of each round is true
boolean isSorted = true;
// Odd round , Compare and exchange from left to right
for (int j = i; j < arr.length - 1 - i; j++) {
log("compare " + arr[j] + " with " + arr[j + 1]);
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSorted = false;
}
}
if (isSorted) {
break;
}
log(" The first " + (i + 1) + " round ");
log(Arrays.toString(arr));
// Even round , Compare and exchange from right to left
isSorted = true;
for (int k = arr.length - 1 - i; k > i; k--) {
if (arr[k] < arr[k - 1]) {
temp = arr[k];
arr[k] = arr[k - 1];
arr[k - 1] = temp;
isSorted = false;
}
}
if (isSorted) {
break;
}
log(" The first " + (i + 2) + " round ");
log(Arrays.toString(arr));
}
}
@Test
public void test() {
// int[] arr={3,2,1,4,5,6,7,8};
int[] arr = {
2, 3, 4, 5, 6, 7, 8, 1 };
// int[] arr={5,8,6,3,9,2,1,7};
// int[] arr={25, 58, 29, 3, 21, 3, 60, 46, 79, 15};
// bubbleV1(arr);
// bubbleV2(arr);
// bubbleV3(arr);
bubbleV4(arr);
log(Arrays.toString(arr));
}
@Test
public void test2() {
int[] arr2 = new int[LEN];
Random random = new Random();
for (int i = 0; i < LEN; i++) {
arr2[i] = random.nextInt(100);
}
// log(Arrays.toString(arr2));
long begin, end;
begin = System.currentTimeMillis();
bubbleV1(arr2);
end = System.currentTimeMillis();
System.out.println(end - begin);
begin = System.currentTimeMillis();
bubbleV2(arr2);
end = System.currentTimeMillis();
System.out.println(end - begin);
begin = System.currentTimeMillis();
bubbleV3(arr2);
end = System.currentTimeMillis();
System.out.println(end - begin);
begin = System.currentTimeMillis();
bubbleV4(arr2);
end = System.currentTimeMillis();
System.out.println(end - begin);
}
void log(String x) {
if (false) {
System.out.println(x);
}
}
Use them separately 1 Ten thousand elements and 2 Test 10000 integer data , give the result as follows
Method version | The length of the array ( ten thousand ) | Time consuming (ms) |
---|---|---|
1 | 1 | 2833 |
2 | 1 | 3 |
3 | 1 | 2 |
4 | 1 | 2 |
1 | 2 | 10475 |
2 | 2 | 4 |
3 | 2 | 3 |
4 | 2 | 3 |
边栏推荐
- 嵌入式数据库开发编程(六)——C API
- 3dsmax2018 common operations and some shortcut keys of editable polygons
- Optimization scheme of win10 virtual machine cluster
- cocos_ Lua loads the file generated by bmfont fnt
- Pdf to DWG in CAD
- AutoCAD - Document Management
- Detailed introduction of OSPF header message
- AutoCAD - graphic input and output
- 用 Jmeter 工具做个小型压力测试
- Research and forecast report on China's solution polymerized styrene butadiene rubber (SSBR) industry (2022 Edition)
猜你喜欢
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
Recherche de mots pour leetcode (solution rétrospective)
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
AutoCAD - scaling
2022/7/2 question summary
Stm32cubemx (8): RTC and RTC wake-up interrupt
stm32Cubemx(8):RTC和RTC唤醒中断
Generate filled text and pictures
小程序直播+电商,想做新零售电商就用它吧!
随机推荐
China as resin Market Research and investment forecast report (2022 Edition)
Leetcode word search (backtracking method)
On-off and on-off of quality system construction
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
Animation
Detailed explanation of the ranking of the best universities
Lua determines whether the current time is the time of the day
Cocos create Jiugongge pictures
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
小程序直播+電商,想做新零售電商就用它吧!
Generate filled text and pictures
win下一键生成当日的时间戳文件
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
UE 虚幻引擎,项目结构
2022 / 7 / 1 Résumé de l'étude
MySQL audit log archiving
PR first time
mysql审计日志归档
Use assimp library to read MTL file data