Article directory
foreword
There are ten commonly used sorting methods, namely:
-
Bubble Sort;
-
selection sort;
-
insertion sort;
-
Hill sort;
-
merge sort;
-
quick sort;
-
heap sort;
-
count sort;
-
bucket sort;
-
Radix sort.
Let's talk about bubble sort today
1. The principle of bubble sort
Bubble sorting is a relatively simple sorting algorithm, which is called Bubble Sort in English. It traverses all the data, and compares adjacent elements each time. If the order is inconsistent with the predetermined order, the position is exchanged; such a traversal will float the largest or smallest data to the top, and then repeat the same operation until all the data is in order.
2. Programming
int[] arr = {
1, 2, 4, 3, 28, 23, 12, 14, 99, 34, 55 };
arr.ToList().ForEach(x => Console.Write(x + " "));
for(int i = 0;i<arr.Length-1;i++)
{
Console.WriteLine();
Console.WriteLine("===========================================");
for (int k = arr.Length - 1; k > i; k--)
{
Console.Write($"第{
arr.Length - k}次");
if (arr[k] < arr[k - 1])
{
int temp = arr[k];
arr[k] = arr[k - 1];
arr[k - 1] = temp;
}
arr.ToList().ForEach(x => Console.Write(x + " "));
Console.WriteLine();
}
Console.WriteLine("===========================================");
Console.Write($"第{
i+1}轮:");
arr.ToList().ForEach(x => Console.Write(x + " "));
}
Console.WriteLine();
arr.ToList().ForEach(x => Console.Write(x + " "));
print result
1 2 4 3 28 23 12 14 99 34 55
===========================================
第1次:1 2 4 3 28 23 12 14 99 34 55
第2次:1 2 4 3 28 23 12 14 34 99 55
第3次:1 2 4 3 28 23 12 14 34 99 55
第4次:1 2 4 3 28 23 12 14 34 99 55
第5次:1 2 4 3 28 12 23 14 34 99 55
第6次:1 2 4 3 12 28 23 14 34 99 55
第7次:1 2 4 3 12 28 23 14 34 99 55
第8次:1 2 3 4 12 28 23 14 34 99 55
第9次:1 2 3 4 12 28 23 14 34 99 55
第10次:1 2 3 4 12 28 23 14 34 99 55
===========================================
第1轮:1 2 3 4 12 28 23 14 34 99 55
===========================================
第1次:1 2 3 4 12 28 23 14 34 55 99
第2次:1 2 3 4 12 28 23 14 34 55 99
第3次:1 2 3 4 12 28 23 14 34 55 99
第4次:1 2 3 4 12 28 14 23 34 55 99
第5次:1 2 3 4 12 14 28 23 34 55 99
第6次:1 2 3 4 12 14 28 23 34 55 99
第7次:1 2 3 4 12 14 28 23 34 55 99
第8次:1 2 3 4 12 14 28 23 34 55 99
第9次:1 2 3 4 12 14 28 23 34 55 99
===========================================
第2轮:1 2 3 4 12 14 28 23 34 55 99
===========================================
第1次:1 2 3 4 12 14 28 23 34 55 99
第2次:1 2 3 4 12 14 28 23 34 55 99
第3次:1 2 3 4 12 14 28 23 34 55 99
第4次:1 2 3 4 12 14 23 28 34 55 99
第5次:1 2 3 4 12 14 23 28 34 55 99
第6次:1 2 3 4 12 14 23 28 34 55 99
第7次:1 2 3 4 12 14 23 28 34 55 99
第8次:1 2 3 4 12 14 23 28 34 55 99
===========================================
第3轮:1 2 3 4 12 14 23 28 34 55 99
===========================================
第1次:1 2 3 4 12 14 23 28 34 55 99
第2次:1 2 3 4 12 14 23 28 34 55 99
第3次:1 2 3 4 12 14 23 28 34 55 99
第4次:1 2 3 4 12 14 23 28 34 55 99
第5次:1 2 3 4 12 14 23 28 34 55 99
第6次:1 2 3 4 12 14 23 28 34 55 99
第7次:1 2 3 4 12 14 23 28 34 55 99
===========================================
第4轮:1 2 3 4 12 14 23 28 34 55 99
===========================================
第1次:1 2 3 4 12 14 23 28 34 55 99
第2次:1 2 3 4 12 14 23 28 34 55 99
第3次:1 2 3 4 12 14 23 28 34 55 99
第4次:1 2 3 4 12 14 23 28 34 55 99
第5次:1 2 3 4 12 14 23 28 34 55 99
第6次:1 2 3 4 12 14 23 28 34 55 99
===========================================
第5轮:1 2 3 4 12 14 23 28 34 55 99
===========================================
第1次:1 2 3 4 12 14 23 28 34 55 99
第2次:1 2 3 4 12 14 23 28 34 55 99
第3次:1 2 3 4 12 14 23 28 34 55 99
第4次:1 2 3 4 12 14 23 28 34 55 99
第5次:1 2 3 4 12 14 23 28 34 55 99
===========================================
第6轮:1 2 3 4 12 14 23 28 34 55 99
===========================================
第1次:1 2 3 4 12 14 23 28 34 55 99
第2次:1 2 3 4 12 14 23 28 34 55 99
第3次:1 2 3 4 12 14 23 28 34 55 99
第4次:1 2 3 4 12 14 23 28 34 55 99
===========================================
第7轮:1 2 3 4 12 14 23 28 34 55 99
===========================================
第1次:1 2 3 4 12 14 23 28 34 55 99
第2次:1 2 3 4 12 14 23 28 34 55 99
第3次:1 2 3 4 12 14 23 28 34 55 99
===========================================
第8轮:1 2 3 4 12 14 23 28 34 55 99
===========================================
第1次:1 2 3 4 12 14 23 28 34 55 99
第2次:1 2 3 4 12 14 23 28 34 55 99
===========================================
第9轮:1 2 3 4 12 14 23 28 34 55 99
===========================================
第1次:1 2 3 4 12 14 23 28 34 55 99
===========================================
第10轮:1 2 3 4 12 14 23 28 34 55 99
1 2 3 4 12 14 23 28 34 55 99
Summarize
Properties: 1. Time complexity: O(n2) 2. Space complexity: O(1) 3. Stable sorting 4. In-place sorting
Bubble sort optimization
if (arr == null || arr.length < 2) {
return arr;
}
int n = arr.length;
for (int i = 0; i < n; i++) {
boolean flag = true;
for (int j = 0; j < n -i - 1; j++) {
if (arr[j + 1] < arr[j]) {
flag = false;
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
//一趟下来是否发生位置交换
if(flag)
break;
}