#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;
}
相关推荐
libnice和coturn这两个库有交叉实现的地方,具体有啥区别?
TurnAdmin:是一个TURN的管理工具,此工具能被用来管理用户账号的增删改查。 TurnServer:是TURN和STUN的服务器实现。 Turnutils_uclient: 模拟多个UDP、TCP、TLS或DTLS客户端 Turnutils_peer: 一个简单的无状态UDP-ECHO服务器,用作中继模式中的最终服务器 Turnutils_stunclient: 一个简单的STUN客户端例子 Turnutils_rfc5769check: 检查STUN/TURN协议实现的正确性。 我们..
冒泡排序(BubbleSort)
两两比较相邻记录,如果反序则交换,直到没有反序的记录。 时间复杂度O(n2) #include <iostream> #include <cstdlib> //顺序表结构 #define MAXSIZE 10 //要排序数组个数最大值 typedef struct { int r[MAXSIZE + 1]; //存储要排序数组,r[0]作为哨兵或临时变量 int le...
ESP32锂电池电量检测实战:从引脚选择到低功耗优化(附完整电路图)
本文详细介绍了ESP32锂电池电量检测的实战方案,从ADC引脚选择到低功耗优化,提供完整的电路图和代码实现。通过MOS管智能开关和深度睡眠技术,显著降低静态功耗至0.5μA,提升设备续航能力。适合物联网开发者解决电量检测中的常见问题。
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...
matplotlib保存图片设置分辨率总是96dpi,好烦躁,教你一招轻松解决
matplotlib保存图片设置分辨率总是96dpi,好烦躁,教你一招轻松解决 目录 matplotlib保存图片设置分辨率总是96dpi,好烦躁,教你一招轻松解决 #使用plt.savefig("cm.png",dpi = 300)为什么不管用 #plt.rcParams也不管用 #解决方案 在做论文的过程中,许多期刊或者会议对图片的分辨率有明确的要求,例如,dpi=300。 之前画曲线图都是采取保存方式为savefig(dpi=300),然后查看保存图片的属性信息,然而你看到的现象是
详解AUTOSAR:什么是AUTOSAR?(理论篇—1)
AUTOSAR是由全球各大汽车整车厂、汽车零部件供应商、汽车电子软件系统公司联合建立的一套标准协议,是对汽车技术开发一百多年来的经验总结。
数据结构-冒泡排序(含全部代码)
目录 函数分析 代码 全部代码 结果截图 函数分析 下标从小到大,相邻依次比较,直到最后两个被比较,每次比较,若为逆序,则交换,以上为一趟冒泡排序。 使用一个标记,有交换时标记,若一趟下来没有需要交换的,则停止。 void BubbleSort(SqList &L) 参数:顺序表L,时间复杂度O(n^2),空间复杂度O(1),稳定性:稳定 代码 //冒泡排序 升序排序 void BubbleSort(SqList &L) { int m = L.length - 1;
【技术科普】破解政务跨系统数据壁垒!亘川一网统管平台多源数据同步技术解析
本文解析亘川一网统管平台多源数据同步技术,针对政务跨系统数据壁垒难题,介绍组件的数据采集、校验、审计留痕等核心能力,结合营商环境项目落地案例,对比传统开发模式差异,讲解开源组件的实践价值,为政务项目开发者提供落地参考。
排序算法——冒泡算法(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...
24、交通信号协调算法与模型解析
本文深入解析了交通信号协调中的关键算法与模型,包括多路径进展模型、MAXBAND网络系统模型和松弛启用带模型。这些模型通过合理安排相位序列、优化绿波带宽等方式,提高了交通运行的效率。文章还探讨了交通控制从单点控制向网络协调的发展趋势,分析了当前面临的挑战与机遇,并提出了未来研究方向,如借助信息技术和机器学习,深入研究交通网络的基本构成,以实现智能化和精准化的信号协调。
Prompt Engineering 学习指南:从入门到精通的最佳路径与资源
随着大型语言模型(LLM)能力的飞速发展,Prompt Engineering(提示工程)已成为人与AI协作的核心技能。本报告旨在为不同水平的学习者提供一条清晰、高效的学习路径,整合了当前社区公认的最佳实践和顶级开源资源,帮助您系统地掌握 Prompt Engineering,释放 LLM 的全部潜力。保持好奇心,不断尝试,您将能驾驭语言模型这一强大的工具,创造出前所未有的价值。超越简单的“一问一答”,学习如何将 Prompt Engineering 与外部工具和数据结合,构建强大的 AI 应用。
为什么Bubble Sort这么慢?用Visual Sorting可视化彻底搞懂O(n²)排序算法(新手友好)
**Visual Sorting**(又名 The Sound Of Sorting)是一款把**排序算法可视化**做到极致的开源工具:它用动态条形图实时演示冒泡排序、快速排序等 20 多种算法的每一步,还能为每根柱子配上音高,让你"听"懂 **O(n²)** 与 **O(n log n)** 的差距。这篇文章将以 **Bubble Sort(冒泡排序)** 为例,带你用最直观的方式搞懂"为什么冒
IDEA的使用指南
IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.
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,
计算机控制技术及应用
《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。
pscad IEEE标准系统搭建说明
pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。
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)...
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...
1325




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



