#include "stdio.h"
#include "string.h"
#define MAX_LIST 50
typedef struct _SqList {
int data[MAX_LIST];
int length;
}SqList;
void swap( SqList* L, unsigned index1, unsigned index2 )
{
if( index1 >= L->length || index2 >= L->length ) return;
if( index1 == index2 ) return;
L->data[index1] = L->data[index1] ^ L->data[index2];
L->data[index2] = L->data[index1] ^ L->data[index2];
L->data[index1] = L->data[index1] ^ L->data[index2];
}
enum BUBBLE_SORT_METHOD { PLAIN, FLAG };
//original bubble sort algorithm
void BubbleSortPlain(SqList* L)
{
int i, j;
for( i = 0; i < L->length - 1; i++ )
{
//every outer loop pick the smallest from the rear of the array
for( j = L->length-2; j >= i; j-- )
{
//compare adjacent elements and get the smaller move forward
if( L->data[j] > L->data[j+1] )
swap(L, j, j+1 );
}
}
}
//Optimized with a flag indicating should there any swapping occurred in the previous loop
//if there is no swapping, loop should exit prematurely
void BubbleSortFlag(SqList* L)
{
bool flag = true;
int i, j;
for( i = 0; flag && i < L->length - 1; i++ )
{
//every outer loop pick the smallest from the rear of the array
for( j = L->length-2; j >= i; j-- )
{
flag = false;
//compare adjacent elements and get the smaller move forward
if( L->data[j] > L->data[j+1] )
{
swap(L, j, j+1 );
flag = true;
}
}
}
}
void BubbleSort( SqList* L, BUBBLE_SORT_METHOD bs )
{
if( bs == PLAIN )
{
BubbleSortPlain( L );
}
else
{
BubbleSortFlag( L );
}
}
int main()
{
SqList d;
int intarr[] = {1,10,23,48,65,31,-21,9,88,100};
memcpy( d.data, intarr, sizeof(intarr));
d.length = sizeof(intarr)/sizeof(int);
int index = 0;
printf("Original array:\n");
for( ; index < d.length; index++ )
printf(" %d", d.data[index] );
printf("\nBubble sort...\n");
BubbleSort( &d, FLAG );
for( index = 0; index < d.length; index++ )
printf(" %d", d.data[index] );
printf("\n");
return 0;
}
相关推荐
ModusToolbox 实战避坑指南|从环境配置到项目构建
本文是一份详尽的ModusToolbox实战避坑指南,手把手教你从环境配置到项目构建。重点解决了Windows系统下的安装、关键环境变量CY_TOOLS_PATHS的设置、PowerShell执行策略调整以及利用URL Modifier工具优化GitHub资源下载等核心难题,旨在帮助开发者,特别是英飞凌(Infineon)芯片用户,顺利搭建开发环境并成功运行第一个Hello World程序。
6-9 Sort Three Distinct Keys(20 分)
6-9 Sort Three Distinct Keys(20 分)Suppose you have an array of N elements, containing three distinct keys, "true", "false", and "maybe". Given an O(N) algorithm to rearrange the list so that all "fals...
涨姿势|小众建模软件Make human,MakeClothes插件使用详解教程(2)
衣服可以从头开始建模,或者通过改变人体网格或由MakeClothes工具提供的“服装助手”(见下文)来建模。在设计和建模一件衣物时,应该记住两个限制。首先,将服装网格映射到人体网格的算法要求服装网格完全由四面体组成。其次,知道MakeClothes每件衣服只支持一种材料是很重要的。 完成MakeClothes工作流程后,您的HOME / makehuman / v1 / data / clothes中的工具将创建一个新文件夹。该文件夹将采用用户为服装项目提供的唯一名称。该文件夹将填充必要的资源,以使该项目在
数据结构-冒泡排序(含全部代码)
目录 函数分析 代码 全部代码 结果截图 函数分析 下标从小到大,相邻依次比较,直到最后两个被比较,每次比较,若为逆序,则交换,以上为一趟冒泡排序。 使用一个标记,有交换时标记,若一趟下来没有需要交换的,则停止。 void BubbleSort(SqList &L) 参数:顺序表L,时间复杂度O(n^2),空间复杂度O(1),稳定性:稳定 代码 //冒泡排序 升序排序 void BubbleSort(SqList &L) { int m = L.length - 1;
【线粒体基因组数据提交实战】从注释校验到GenBank批量入库的避坑指南
本文详细介绍了线粒体基因组数据从注释校验到GenBank批量上传的全流程避坑指南。重点解析了GB2sequin工具的正确使用方法、TBL注释文件的精修技巧,以及BankIt平台提交的隐藏要点,帮助科研人员高效完成NCBI数据提交,特别适合需要批量上传线粒体基因组数据的研究者。
GD-Link离线烧录全攻略:从配置到一键下载的保姆级教程
本文详细介绍了GD-Link离线烧录的全流程,从硬件准备到软件配置,再到一键下载的实战技巧。通过保姆级教程,帮助工程师快速掌握国产MCU的离线烧录技术,适用于产线批量烧录和现场设备维护等场景,显著提升工作效率。
排序算法——冒泡算法(C语言版)
/*在sort.h文件定义用于排序的顺序表结构,如下:*/ sort.h #define MAXSIZE 10 #define true 1 #define false 0 typedef int Status; typedef struct { int r[MAXSIZE +1];/*r[0]用作哨兵或临时变量(目前还没有用到此变量的作用)*/ int length; }Sq...
舵机PWM信号转互斥继电器开关信号
-----------------本文由“麦粒电子”撰写,并提供相应产品服务。---------------- 叙述 上次的PWM转继电器开关方案得到大家的不少好评(博客链接),但是发现有些玩家需要一路PWM同时去控制两路继电器开关通断,并且两者是互斥的,好去控制两种不同的负载。所以就继续升级了如下的模块。 正常的控制部件顺序这样:遥控器按钮拨动,遥控器接收机相应通道PWM值变化,PWM转开关模块检测PWM值发生变化从而驱动相应机械继电器触点吸合释放。本模块的两个继电器动作是互斥的,也就是不会同时吸合。
冒泡排序(BubbleSort)
两两比较相邻记录,如果反序则交换,直到没有反序的记录。 时间复杂度O(n2) #include <iostream> #include <cstdlib> //顺序表结构 #define MAXSIZE 10 //要排序数组个数最大值 typedef struct { int r[MAXSIZE + 1]; //存储要排序数组,r[0]作为哨兵或临时变量 int le...
【深度学习】手把手教你在远程服务器上跑 YOLOv3 相关代码(含环境配置)
这篇文章主要记录了如何配置项目环境以及如何在远程服务器上跑 YOLOv3 模型。
【PyTorch项目实战】图像分类与识别:手写数字识别(MNIST)、普适物体识别(CIFAR-10)
主要介绍了两个经典的实战项目:(1)基于 MNIST 数据集的手写数字识别(2)基于 CIFAR-10 数据集进行图像分类
为什么Bubble Sort这么慢?用Visual Sorting可视化彻底搞懂O(n²)排序算法(新手友好)
**Visual Sorting**(又名 The Sound Of Sorting)是一款把**排序算法可视化**做到极致的开源工具:它用动态条形图实时演示冒泡排序、快速排序等 20 多种算法的每一步,还能为每根柱子配上音高,让你"听"懂 **O(n²)** 与 **O(n log n)** 的差距。这篇文章将以 **Bubble Sort(冒泡排序)** 为例,带你用最直观的方式搞懂"为什么冒
Java 实现 MP3 音频合并工具类:摆脱 FFmpeg,高效低 CPU 的纯本地方案【需求已解决】
本方案充分发挥了 Java 音频 SPI 的潜力,实现了轻量、跨平台、低 CPU 消耗的 MP3 音频合并工具类。 对于希望在服务器或桌面应用中集成音频处理功能的开发者而言,这无疑是一个可行且优雅的解决方案。
Sorting Algorithm-Bubble Sort
冒泡排序-Bubble Sort Algorithm: 重复地走访过要排序的数列,一次比较两个元素,如果两个元素顺序错误,就把他们交换,知道没有再需要交换的元素为止像水中的泡泡一样,大的先冒出来,小的后冒出来。 Ex: 4,3,1,2 3,1,2,4 最大元素4此时是正确的位置 1,2,3,4 4之前的最大元素3到了正确的位置 1,2,
锂电池充电器电路设计 里面有电路
锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。
迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)
迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)
Sorting: Bubble Sort
#!/bin/python3 import math import os import random import re import sys # Complete the countSwaps function below. def countSwaps(a): count=0 for i in range(n): for j in range(n-1-i)...
温度PID控制PLC程序
温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值
Internal Sorting: Bubble sort: Sorting by Exchanging
Bubble sort:冒泡排序Animation Static visualization of bubble sort An example of bubble sort. Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in t
C language:two sorting algorithms:Bubble Sort and Election Sort
//C language:two sorting algorithms:Bubble Sort and Election Sort #include <stdio.h> #include<stdlib.h> void sort_elect(int* a, int len); void sort_bubbling(int* a, int len); int main(v...
【Data Structures】 7. Simple Sorting—Bubble Sort, Selection Sort, and Insertion Sort
Bubble sort 1. Compare two values at a time. 2. If the one on the left is bigger, swap them to BUBBLE UP the bigger value to the right. 3. Move one position to the right.
Sorting Algorithms--Bubble sort
<br /><br />Sorting Algorithms<br /><br />Have an array you need to put in order? Keeping business records and want to sort them by ID number or last name of client? Then you'll need a sorting algorithm. To understand the more complex and efficient sorting
[Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merge sort and quick sort. I then implement them in C++. All the function takes in...
冒泡排序simple sorting algorithm for bubble sort
【代码】simple sorting algorithm for bubble sort。
C++字符串互转实战:std::string与std::wstring编码转换指南
字符编码是计算机处理文本的基础,UTF-8与UTF-16/UTF-32的转换涉及码点、代理项等核心概念。不同平台wchar_t宽度差异导致跨平台乱码,需通过显式指定编码或手写编解码确保正确性。掌握字符串互转技术能有效解决乱码、截断问题,提升跨平台开发效率。常见应用场景包括配置文件读写、界面渲染、网络传输等。本文深入解析std::string与std::wstring的转换方案,涵盖Windows API、手写UTF-8编解码及std::wstring_convert,并提供性能对比与排查技巧。
排序算法 (sorting algorithm)之 冒泡排序(bubble sort)
http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ https://en.wikipedia.org/wiki/Sorting_algorithm 冒泡排序(Bubble sort) https://en.wikipedia.org/wiki/...
Hack 开源等宽字体演进史:从 CHANGELOG 解读 1.0.0 到 3.003 的版本变迁
Hack 是一款为源代码而生的开源等宽字体,其完整演进轨迹被系统记录在仓库根目录的 [CHANGELOG.md](https://link.gitcode.com/i/6576fbeb8f91120d17fc6c34fb2a1ce3) 中。本篇技术文章以这份变更日志为主线,逐版本拆解 Hack 从 1.0.0 分叉 Bitstream Vera Sans Mono,到 3.003 完成字形全面重构
Python-Flet实现可视化的冒泡排序
1:效果图2:代码解释登录后复制 这段代码实现了一个冒泡排序算法的可视化界面。具体功能如下:1.bubble_sort函数:实现了冒泡排序算法,每次交换元素后调用update_display更新显示,并暂停0.5秒以便观察排序过程。update_display函数:更新界面上的文本显示,显示当前数组的状态。s...
590



被折叠的 条评论
为什么被折叠?



