在本教程中,您将学习冒泡排序的工作原理。此外,您还将找到使用C语言进行冒泡排序的示例。
冒泡排序是一种比较相邻元素的算法,如果它们不符合预定的顺序,则交换它们的位置。顺序可以是升序或降序。
冒泡排序如何工作?
- 从第一个索引开始,比较第一个和第二个元素,如果第一个元素大于第二个元素,则将它们交换。
现在,比较第二个和第三个元素。如果它们顺序不对,请交换它们。
持续上面的过程直到最后一个元素。
- 对于剩余的迭代,同样的过程也在继续。在每次迭代之后,未排序元素中最大的元素放在末尾。(例如,完成上图第一次迭代,已排序元素为45,未排序元素为-2, 0, 11,-9)
在每个迭代中,比较一直进行到最后一个未排序的元素。
当所有未排序的元素都放置在其正确的位置时,数组将被排序。
冒泡排序算法
bubbleSort(array)
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
end bubbleSort
C示例
// Bubble sort in C
#include <stdio.h>
void bubbleSort(int array[], int size) {
// run loops two times: one for walking throught the array
// and the other for comparison
for (int step = 0; step < size - 1; ++step) {
for (int i = 0; i < size - step - 1; ++i) {
// To sort in descending order, change">" to "<".
if (array[i] > array[i + 1]) {
// swap if greater is at the rear position
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
// function to print the array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
// driver code
int main() {
int data[] = {
-2, 45, 0, 11, -9};
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
printf("Sorted Array in Ascending Order:\n");
printArray(data, size);
}
优化冒泡排序
在上面的代码中,即使数组已经排序,也会进行所有可能的比较。它增加了执行时间。
通过引入一个额外的变量swapped,可以优化代码。在每次迭代之后,如果没有发生交换,就不需要执行进一步的循环。
在这种情况下,变量swapped设置为false。因此,我们可以防止进一步的迭代。
优化冒泡排序的算法是:
bubbleSort(array)
swapped <- false
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
swapped <- true
end bubbleSort
C示例
// Optimized bubble sort in C
#include <stdio.h>
void bubbleSort(int arrayay[], int size) {
for (int step = 0; step < size - 1; ++step) {
// Swapped keeps track of swapping
int swapped = 0;
// Run loops two times: one for walking throught the array
// and the other for comparison
for (int i = 0; i < size - step - 1; ++i) {
// To sort in descending order, change > to < in this line.
if (arrayay[i] > arrayay[i + 1]) {
// Swap if greater is at the rear position
int temp = arrayay[i];
arrayay[i] = arrayay[i + 1];
arrayay[i + 1] = temp;
swapped = 1;
}
}
// If there is not swapping in the last swap, then the array is already sorted.
if (swapped == 0)
break;
}
}
// Function to print an array
void printarrayay(int arrayay[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", arrayay[i]);
}
printf("\n");
}
// Driver code
int main() {
int data[] = {
-2, 45, 0, 11, -9};
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
printf("Sorted Array in Ascending Order:\n");
printarrayay(data, size);
}
复杂度
冒泡排序是最简单的排序算法之一。该算法实现了两个循环。
循环 | 比较次数 |
---|---|
第一次 | (n-1) |
第二次 | (n-2) |
第三次 | (n-3) |
… … | … … |
最后一次 | 1 |
比较次数 (n - 1) + (n - 2) + (n - 3) +…+ 1 = n(n - 1) / 2 约等于 n 2 n^2 n2。
复杂度:O( n 2 n^2 n2)
此外,我们可以通过简单地观察循环的数量来分析复杂度。有2个循环,所以复杂度是n*n= n 2 n^2 n2。
时间复杂度
- 最坏情况复杂度:O( n 2 n^2 n2)
如果我们想按升序排序,而数组按降序排序,则会出现最坏的情况。 - 最佳情况复杂度:O(n)
如果数组已经排好,则不需要排序。 - 平均情况复杂度:O( n 2 n^2 n2)
当数组的元素顺序混乱(既不是升序也不是降序)时,就会发生这种情况。
空间复杂度:
空间复杂度为O(1),因为交换时使用了一个额外的变量temp。
在优化算法中,变量 swapped 增加了空间复杂度,使其成为O(2)。
冒泡排序应用
冒泡排序用于以下情况:
- 代码的复杂性并不重要。
- 首选短代码。
参考文档
[1]Parewa Labs Pvt. Ltd.Bubble Sort Algorithm[EB/OL].https://www.programiz.com/dsa/bubble-sort,2020-01-01.