1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-02-03 02:23:38 +08:00
Files
408CSFamily/code/ds/BubbleSort.cpp
喜欢芝士的妹妹 f989110774 feat: update
2023-08-30 23:08:37 +08:00

44 lines
832 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 冒泡排序
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
}