C程序实现冒泡排序
Bubble Sort is a simple, stable, and in-place sorting algorithm.
氣泡排序是一種簡單,穩(wěn)定且就地的排序算法。
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.
一種穩(wěn)定的排序算法是一種具有相等值的鍵在排序后的輸出數(shù)組中以與輸入未排序數(shù)組中存在的鍵相同的順序出現(xiàn)的算法 。
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.
就地排序算法具有各種定義,但使用更廣泛的定義是:–就地排序算法不需要額外的空間,并且使用常量內(nèi)存來對就地輸入進(jìn)行操作。 雖然,它可能需要一些允許變量使用的額外常量空間。
Due to its simplicity, it is widely used as a sorting algorithm by computer programmers.
由于其簡單性,它被計(jì)算機(jī)程序員廣泛用作排序算法。
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.
冒泡排序的基本工作原理是,如果相鄰元素的順序錯誤,它將反復(fù)交換。 因此,在每次完整的迭代之后,最大元素將到達(dá)其在排序數(shù)組中的位置。
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 forExample:
例:
Input Array:
輸入數(shù)組:
5 8 1 2 9
5 8 1 2 9
Here, I will run from 0 to 3
在這里,我將從0運(yùn)行到3
Since, i < n-1 => i < 5-1 => i < 4
因?yàn)?#xff0c;我<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)無交換,因?yàn)? <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),交換是因?yàn)? <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),交換因?yàn)? <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 ),因?yàn)? <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,因?yàn)樽詈笠粋€元素會自動排序。
Time Complexity: The time complexity of Binary Search can be described as: T(n) = T(n/2) + C
時間復(fù)雜度:二進(jìn)制搜索的時間復(fù)雜度可描述為: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),因?yàn)榧词箶?shù)組已排序循環(huán)也會運(yùn)行
Space Complexity: O(1)
空間復(fù)雜度: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的這種簡單實(shí)現(xiàn)只是為了說明算法的概念。 在現(xiàn)實(shí)生活中,無論何時使用氣泡排序,都比該版本更優(yōu)選優(yōu)化版本。 這是因?yàn)樵谧罴亚闆r下,優(yōu)化版本會給O(n)時間帶來復(fù)雜性。
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.
在優(yōu)化的冒泡排序中,如果已對數(shù)組進(jìn)行排序,則內(nèi)部循環(huán)不會運(yùn)行。 然而,在簡單的實(shí)現(xiàn)中,沒有這樣的規(guī)定。
Bubble Sort Implementation:
氣泡排序?qū)嵤?#xff1a;
#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
總結(jié)
- 上一篇: 醉玲珑剧情介绍
- 下一篇: 硕士毕业后去国外读法学博士_法学硕士的完