Bubble Sort is a simple, stable, and in-place sorting algorithm.
氣泡排序是一種簡單,穩定且就地的排序算法。
A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is present in the input unsorted array.
一種穩定的排序算法是一種具有相等值的鍵在排序后的輸出數組中以與輸入未排序數組中存在的鍵相同的順序出現的算法 。
An in-place sorting algorithm has various definitions but a more used one is – An in-place sorting algorithm does not need extra space and uses the constant memory for manipulation of the input in-place. Although, it may require some extra constant space allowed for variables.
就地排序算法具有各種定義,但使用更廣泛的定義是:–就地排序算法不需要額外的空間,并且使用常量內存來對就地輸入進行操作。 雖然,它可能需要一些允許變量使用的額外常量空間。
Due to its simplicity, it is widely used as a sorting algorithm by computer programmers.
由于其簡單性,它被計算機程序員廣泛用作排序算法。
The basic working principle of bubble sort is that it repeatedly swaps the adjacent elements if they are in the wrong order. Hence, after every full iteration, the largest element reaches its position as in the sorted array.
冒泡排序的基本工作原理是,如果相鄰元素的順序錯誤,它將反復交換。 因此,在每次完整的迭代之后,最大元素將到達其在排序數組中的位置。
Pseudo-code:
偽代碼:
1. for i: 0 to n-1 not inclusive do:
2. for j: 0 to n-i-1 not inclusive do:
3. If a[j] > a[j+1] then
4. swap a[j] and a[j+1]
5. end if
6. end for
7. end for
Example:
例:
Input Array:
輸入數組:
5 8 1 2 9
5 8 1 2 9
Here, I will run from 0 to 3
在這里,我將從0運行到3
Since, i < n-1 => i < 5-1 => i < 4
因為,我<n-1 =>我<5-1 =>我<4
Iteration 1 (i = 0):
迭代1(i = 0):
For j = 0, (5 8 1 2 9) -> (5 8 1 2 9) No swap because 5 < 8
對于j = 0,( 5 8 1 2 9)->( 5 8 1 2 9)無交換,因為5 <8
For j = 1, (5 8 1 2 9) -> (5 1 8 2 9), swap because 1 < 8
對于j = 1,(5 8 1 2 9)->(5 1 8 2 9),交換是因為1 <8
For j = 2, (5 1 8 2 9) -> (5 1 2 8 9), swap because 2 < 8
對于j = 2,(5 1 8 2 9)->(5 1 2 8 9),交換因為2 <8
For j = 3, (5 1 2 8 9) -> (5 1 2 8 9), no swap
對于j = 3,(5 1 2 8 9 )->(5 1 2 8 9 ),沒有交換
1st Pass gives – 5 1 2 8 9
1 次通給出- 5 1 2 8 9
Iteration 2 (i = 1):
迭代2(i = 1):
For j = 0, (5 1 2 8 9) -> (1 5 2 8 9) No swap because 1 < 5
對于j = 0,( 5 1 2 8 9 )->( 1 5 2 8 9 )由于1 <5而沒有交換
For j = 1, (1 5 2 8 9) -> (1 2 5 8 9), swap because 2 < 5
對于j = 1,(1 5 2 8 9 )->(1 2 5 8 9 ),因為2 <5
For j = 2, (1 2 5 8 9) -> (1 2 5 8 9), no swap
對于j = 2,(1 2 5 8 9 )->(1 2 5 8 9 ),沒有交換
2nd Pass gives – 1 2 5 8 9
第二遍給– 1 2 5 8 9
Iteration 3 (i = 2):
迭代3(i = 2):
For j = 0, (1 2 5 8 9) -> (1 2 5 8 9), No swap because 1 < 2
對于j = 0,( 1 2 5 8 9 )->( 1 2 5 8 9 ),由于1 <2而沒有交換
For j = 1, (1 2 5 8 9) -> (1 2 5 8 9), No swap 2 < 5
對于j = 1,(1 2 5 8 9 )->(1 2 5 8 9 ),無交換2 <5
3rd Pass gives – 1 2 5 8 9
第三張通過– 1 2 5 8 9
Iteration 4 (i = 3):
迭代4(i = 3):
For j = 0, (1 2 5 8 9) -> (1 2 5 8 9), No swap because 1 < 2
對于j = 0,( 1 2 5 8 9 )->( 1 2 5 8 9 ),由于1 <2而沒有交換
4th Pass gives – 1 2 5 8 9 because, last element is automatically sorted.
第 4 次通過給出– 1 2 5 8 9,因為最后一個元素會自動排序。
Time Complexity: The time complexity of Binary Search can be described as: T(n) = T(n/2) + C
時間復雜度:二進制搜索的時間復雜度可描述為:T(n)= T(n / 2)+ C
Worst case: O(n^2)
最壞的情況:O(n ^ 2)
Average Case: O(n^2)
平均情況:O(n ^ 2)
Best case: O(n^2), since the loops run even if the array is sorted
最佳情況:O(n ^ 2),因為即使數組已排序循環也會運行
Space Complexity: O(1)
空間復雜度:O(1)
This simple implementation of Bubble Sort is just to explain the concept of the algorithm. In real life, whenever bubble sort is used, the optimized version is preferred over this one. That is because the optimized version gives O(n) time complexity in the best case.
Bubble Sort的這種簡單實現只是為了說明算法的概念。 在現實生活中,無論何時使用氣泡排序,都比該版本更優選優化版本。 這是因為在最佳情況下,優化版本會給O(n)時間帶來復雜性。
In the optimized bubble sort, the inner loop doesn't run if the array is already sorted. Whereas, in the simple implementation, no such provision is there.
在優化的冒泡排序中,如果已對數組進行排序,則內部循環不會運行。 然而,在簡單的實現中,沒有這樣的規定。
Bubble Sort Implementation:
氣泡排序實施:
#include <stdio.h>
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble_sort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(&arr[j], &arr[j + 1]);
}
int main()
{
int arr[] = { 12, 46, 34, 82, 10, 9, 28 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("\nInput Array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
bubble_sort(arr, n);
printf("\nSorted Array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
輸出:
Input Array:
12 46 34 82 10 9 28
Sorted Array:
9 10 12 28 34 46 82
翻譯自: https://www.includehelp.com/c-programs/implement-bubble-sort.aspx