日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

优化的交换排序(冒泡排序)_C程序实现优化的冒泡排序

發(fā)布時間:2025/3/11 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 优化的交换排序(冒泡排序)_C程序实现优化的冒泡排序 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

優(yōu)化的交換排序(冒泡排序)

Bubble Sort is a simple, stable, and in-place sorting algorithm. Due to its simplicity, it is widely used as a sorting algorithm by computer programmers.

氣泡排序是一種簡單,穩(wěn)定且就地的排序算法。 由于其簡單性,它被計算機程序員廣泛用作排序算法。

The basic working principle of a simple 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ù)組中的位置。

However, this simple bubble sort has time complexity O(n^2) in all cases because the inner loop runs even if the array is sorted. Therefore, we use an optimized version of bubble sort. This version uses a bool variable to check whether any swap took place in the previous iteration. If yes, only then the next iteration takes place. If no, then the loop breaks.

但是,這種簡單的冒泡排序在所有情況下都具有時間復(fù)雜度O(n ^ 2) ,因為即使對數(shù)組進(jìn)行排序,內(nèi)部循環(huán)也會運行。 因此,我們使用冒泡排序優(yōu)化版本 。 此版本使用bool變量來檢查在先前的迭代中是否發(fā)生了任何交換。 如果是,則僅進(jìn)行下一次迭代。 如果否,則循環(huán)中斷。

Pseudo-code:

偽代碼:

1. for i: 0 to n-1 not inclusive do: 2. swapped = false 3. for j: 0 to n-i-1 not inclusive do: 4. If a[j] > a[j+1] then 5. swap a[j] and a[j+1] 6. swapped = true 7. end if 8. end for 9. if swapped == false then 10. break 11. end if 12. end for

Example:

例:

Input Array:

輸入數(shù)組:

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 swaps because 5 < 8
For j = 1, (5 8 1 2 9) -> (5 1 8 2 9), swap because 1 < 8
For j = 2, (5 1 8 2 9) -> (5 1 2 8 9), swap because 2 < 8
For j = 3, (5 1 2 8 9) -> (5 1 2 8 9), no swap

對于j = 0,( 5 8 1 2 9)->( 5 8 1 2 9)沒有交換因為5 <8
對于j = 1,(5 8 1 2 9)->(5 1 8 2 9),交換是因為1 <8
對于j = 2,(5 1 8 2 9)->(5 1 2 8 9),交換因為2 <8
對于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
For j = 1, (1 5 2 8 9) -> (1 2 5 8 9), swap because 2 < 5
For j = 2, (1 2 5 8 9) -> (1 2 5 8 9), no swap

對于j = 0,( 5 1 2 8 9 )->( 1 5 2 8 9 )由于1 <5而沒有交換
對于j = 1,(1 5 2 8 9 )->(1 2 5 8 9 ),因為2 <5
對于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
For j = 1, (1 2 5 8 9) -> (1 2 5 8 9), No swap 2 < 5

對于j = 0,( 1 2 5 8 9 )->( 1 2 5 8 9 ),由于1 <2而沒有交換
對于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

Here, the 4th iteration won't take place since the loop break because there was no swapping in the third iteration.

在這里,由于循環(huán)中斷,因此第4 迭代不會發(fā)生,因為在第3次迭代中沒有交換。

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), if the array is already sorted

    最佳情況:O(n),如果數(shù)組已經(jīng)排序

  • Space Complexity: O(1)

    空間復(fù)雜度:O(1)

  • Optimized Bubble Sort Implementation:

    優(yōu)化的冒泡排序?qū)崿F(xiàn):

    #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;bool swapped;for (i = 0; i < n - 1; i++) {swapped = false;for (j = 0; j < n - i - 1; j++) {if (arr[j] > arr[j + 1]) {swap(&arr[j], &arr[j + 1]);swapped = true;}}if (swapped == false)break;} }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-optimized-bubble-sort.aspx

    優(yōu)化的交換排序(冒泡排序)

    總結(jié)

    以上是生活随笔為你收集整理的优化的交换排序(冒泡排序)_C程序实现优化的冒泡排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。