MCPcopy Create free account
hub / github.com/142vip/408CSFamily / BubbleSwapSort

Function BubbleSwapSort

code/ds/BubbleSort.cpp:2–21  ·  view source on GitHub ↗

冒泡排序

Source from the content-addressed store, hash-verified

1// 冒泡排序
2void BubbleSwapSort(ElemType A[], int n){
3 for(i=0;i<n-1;i++){
4 // 当前趟次冒泡,是否发生了元素交换,初始化为false
5 bool flag=false;
6
7 for(j=n-1;j>i;j--){
8 if(A[j-1].key>A[j].key){
9 // 将两个元素A[j-1]、A[j]进行交换,有多种方法
10 swap(A[j-1],A[j])
11 // 确认已发生交换
12 flag=true
13 }
14 }
15
16 // 本趟遍历后没有发生交换,说明表已经有序
17 if(flag==false){
18 return ;
19 }
20 }
21}
22
23/**
24 * 加减法实现两个元素值互换

Callers

nothing calls this directly

Calls 1

swapFunction · 0.70

Tested by

no test coverage detected