C程序實現冒泡排序

Bubble Sort is a simple, stable, and in-place sorting algorithm.

氣泡排序是一種簡單,穩定且就地的排序算法。

  1. 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.

    一種穩定的排序算法是一種具有相等值的鍵在排序后的輸出數組中以與輸入未排序數組中存在的鍵相同的順序出現的算法

  2. 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

  1. Worst case: O(n^2)

    最壞的情況:O(n ^ 2)

  2. Average Case: O(n^2)

    平均情況:O(n ^ 2)

  3. Best case: O(n^2), since the loops run even if the array is sorted

    最佳情況:O(n ^ 2),因為即使數組已排序循環也會運行

  4. 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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377615.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377615.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377615.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

一、爬蟲基本概念

一、爬蟲根據使用場景分類 爬蟲&#xff1a; 通過編寫程序&#xff0c;模擬瀏覽器上網&#xff0c;讓其去互聯網上抓取數據的過程。 ① 通用爬蟲&#xff1a;抓取系統重要的組成部分&#xff0c;抓取的是一整張頁面的數據 ② 聚焦爬蟲&#xff1a;建立在通用爬蟲的基礎之上&am…

經營你的iOS應用日志(二):異常日志

如果你去4S店修車&#xff0c;給小工說你的車哪天怎么樣怎么樣了&#xff0c;小工有可能會立即搬出一臺電腦&#xff0c;插上行車電腦把日志打出來&#xff0c;然后告訴你你的車發生過什么故障。汽車尚且如此&#xff0c;何況移動互聯網應用呢。 本文第一篇&#xff1a;經營你的…

Discuz 升級X3問題匯總整理

最近一段時間公司的社區垃圾帖數量陡然上漲&#xff0c;以至于社區首頁的推薦版塊滿滿都是垃圾帖的身影&#xff0c;為了進一步解決垃圾帖問題我們整整花了1天時間刪垃圾貼&#xff0c;清除不良用戶&#xff0c;刪的手都酸了&#xff0c;可見垃圾帖的數量之多&#xff01;可恥的…

【C++grammar】格式化輸出與I/O流函數

目錄1、格式化輸出1. setw manipulator(“設置域寬”控制符)2. setprecision manipulator(“設置浮點精度”控制符)3. setfill manipulator(“設置填充字符”控制符)4. Formatting Output in File Operation(在文件操作中格式化輸入/輸出)5.小練習2、用于輸入/輸出流的函數1. g…

python 忽略 異常_如何忽略Python中的異常?

python 忽略 異常什么是例外&#xff1f; (What is an Exception?) An exception is an event, which occurs during the execution of a program that interrupts the normal execution of the application. Generally, any application when encountered with a situation t…

三、實戰---爬取百度指定詞條所對應的結果頁面(一個簡單的頁面采集器)

在第一篇博文中也提及到User-Agent&#xff0c;表示請求載體的身份&#xff0c;也就是說明通過什么瀏覽器進行訪問服務器的&#xff0c;這一點很重要。 ① UA檢測 門戶網站服務器會檢測請求載體的身份。如果檢測到載體的身份表示為某一款瀏覽器的請求&#xff0c;則說明這是一…

Spring MVC攔截器實現分析

SpringMVC的攔截器不同于Spring的攔截器&#xff0c;SpringMVC具有統一的入口DispatcherServlet&#xff0c;所有的請求都通過DispatcherServlet&#xff0c;所以只需要在DispatcherServlet上做文章即可&#xff0c;DispatcherServlet也沒有代理&#xff0c;同時SpringMVC管理的…

碩士畢業后去國外讀法學博士_法學碩士的完整形式是什么?

碩士畢業后去國外讀法學博士法學碩士&#xff1a;豆科大法師(拉丁)/法學碩士 (LLM: Legum Magister (Latin)/ Master of Law) LLM is an abbreviation of Legum Magister. It is in term of Latin which states the masters degree of Law. In the majority, LLM is generally …

android:layout_weight屬性的簡單使用

效果&#xff1a; style.xml <style name"etStyle2"><item name"android:layout_width">match_parent</item><item name"android:layout_height">wrap_content</item><item name"android:background"…

一、環境配置安裝

一、Anaconda Ⅰ下載 最新版的anaconda可能會需要各種各樣的問題&#xff0c;python3.6版本比較穩定&#xff0c;建議使用。 老鐵們可以通過&#xff0c;Anaconda以前版本所自帶Python版本&#xff0c;查看Anaconda所帶的python版本 我用的是這個&#xff0c;Anaconda3-5.2.0…

leetcode 35. 搜索插入位置 思考分析

目錄題目暴力二分迭代二分遞歸題目 給定一個排序數組和一個目標值&#xff0c;在數組中找到目標值&#xff0c;并返回其索引。如果目標值不存在于數組中&#xff0c;返回它將會被按順序插入的位置。 你可以假設數組中無重復元素。 示例 1: 輸入: [1,3,5,6], 5 輸出: 2 示例 2:…

java優秀算法河內之塔_河內塔的Java程序

java優秀算法河內之塔Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using the third rod (say auxiliary). The rules are: 河內塔是一個數學難題&a…

轉——C# DataGridView控件 動態添加新行

DataGridView控件在實際應用中非常實用&#xff0c;特別需要表格顯示數據時。可以靜態綁定數據源&#xff0c;這樣就自動為DataGridView控件添加相應的行。假如需要動態為DataGridView控件添加新行&#xff0c;方法有很多種&#xff0c;下面簡單介紹如何為DataGridView控件動態…

分享通用基類庫-C#通用緩存類

1 /************************************************************************************* 2 * 代碼:吳蔣 3 * 時間:2012.03.30 4 * 說明:緩存公共基類 5 * 其他: 6 * 修改人&#xff1a; 7 * 修改時間&#xff1a; 8 * 修改說明&#xff1a; 9 ******************…

二、PyTorch加載數據

一、常用的兩個函數 dir()函數可以理解為打開某個包&#xff0c;help()可以理解為返回如何使用某個具體的方法 例如&#xff1a;若一個A錢包里面有a&#xff0c;b&#xff0c;c&#xff0c;d四個小包&#xff0c;則可通過dir(A)&#xff0c;打開該A錢包&#xff0c;返回a&…

leetcode 1005. K 次取反后最大化的數組和 思考分析

題目 給定一個整數數組 A&#xff0c;我們只能用以下方法修改該數組&#xff1a;我們選擇某個索引 i 并將 A[i] 替換為 -A[i]&#xff0c;然后總共重復這個過程 K 次。&#xff08;我們可以多次選擇同一個索引 i。&#xff09; 以這種方式修改數組后&#xff0c;返回數組可能…

三、TensorBoard

一、安裝TensorBoard 管理員身份運行Anaconda Prompt&#xff0c;進入自己的環境環境 conda activate y_pytorch&#xff0c;pip install tensorboard 進行下載&#xff0c;也可以通過conda install tensorboard進行下載。其實通俗點&#xff0c;pip相當于菜市場&#xff0c;c…

IT資產管理系統SQL版

你難道還在用Excel登記IT資產信息嗎&#xff1f; 那你一定要好好考慮如何面對以下問題 1&#xff1a;IT人員需要面對自身部門以下問題用戶申請了資產it部未處理的單還有哪些?庫存里面還有哪些資產?有多少設備在維修?有多少設備已經報廢了?哪些資產低于安全庫存需要采購?使…

詳細講解設計跳表的三個步驟(查找、插入、刪除)

目錄寫在前面跳表概要查找步驟插入步驟刪除步驟完整代碼寫在前面 關于跳表的一些知識可以參考這篇文章,最好是先看完這篇文章再看詳細的思路->代碼的復現步驟: Redis內部數據結構詳解(6)——skiplist 關于跳表的插入、刪除基本操作其實也就是鏈表的插入和刪除&#xff0c;所…

php 類靜態變量 和 常量消耗內存及時間對比

在對類執行100w次循環后&#xff0c; 常量最快&#xff0c;變量其次&#xff0c;靜態變量消耗時間最高 其中&#xff1a; 常量消耗&#xff1a;101.1739毫秒 變量消耗&#xff1a;2039.7689毫秒 靜態變量消耗&#xff1a;4084.8911毫秒 測試代碼&#xff1a; class Timer_profi…