mirror of
https://github.com/142vip/408CSFamily.git
synced 2026-04-05 11:38:27 +08:00
feat: 修复文档,新增算法代码
This commit is contained in:
59
code/ds/BubbleSort.cpp
Normal file
59
code/ds/BubbleSort.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* @Description: 冒泡排序
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-03-31 08:24:18
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-06 07:26:15
|
||||
*/
|
||||
|
||||
|
||||
void BubbleSwapSort(ElemType A[], int n){
|
||||
|
||||
for(i=0;i<n-1;i++){
|
||||
|
||||
|
||||
// 当前趟次冒泡,是否发生了元素交换,初始化为false
|
||||
bool flag=false;
|
||||
|
||||
for(j=n-1;j>i;j--){
|
||||
if(A[j-1].key>A[j].key){
|
||||
|
||||
// 将两个元素A[j-1]、A[j]进行交换,有多种方法
|
||||
swap(A[j-1],A[j])
|
||||
// 确认已发生交换
|
||||
flag=true
|
||||
}
|
||||
}
|
||||
|
||||
// 本趟遍历后没有发生交换,说明表已经有序
|
||||
if(flag==false){
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加减法实现两个元素值互换
|
||||
*
|
||||
*/
|
||||
void swap(int a, int b){
|
||||
// 此时a为两值的和
|
||||
a=a+b;
|
||||
// 此时b的值为a
|
||||
b=a-b
|
||||
// 如何实现让a的值为b呢??此时a的值为b
|
||||
a=a-b;
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时变量实现两个元素值的互换
|
||||
*
|
||||
*/
|
||||
void swap(int a,int b){
|
||||
int temp;
|
||||
temp=a;
|
||||
a=b;
|
||||
b=temp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user