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.mzph.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…