当前位置:网站首页>Bubble sort code implementation
Bubble sort code implementation
2022-07-25 06:17:00 【Can't fail I】
Bubble sort


#include <iostream>
using namespace std;
typedef int ElemType;
struct SSTable
{
ElemType* elelm;// The starting address of the storage element
int TableLen;// Element number
};
void ST_Init(SSTable& ST, int len) {
ST.TableLen = len;
ST.elelm = (ElemType*)malloc(sizeof(ElemType) * ST.TableLen);
int i;
srand(time(NULL));// Random number generation , Every time you execute the code, you get random numbers
for (i = 0; i < ST.TableLen; i++) {
ST.elelm[i] = rand() % 100;
}
}
// Exchange function
void swap(ElemType& a, ElemType& b) {
ElemType c = a;
a = b;
b = c;
}
// Bubble sort
void BubbleSort(ElemType A[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = n - 1; j > i; j--) {
if (A[j - 1] > A[j]) {
swap(A[j - 1], A[j]);
}
}
}
}
int main()
{
ElemType A[] = {
10,2,6,5,3,9,8,4,7,1 };
int len = 10;
BubbleSort(A, len);
}

add to flag Variable to control the loop , When the sequence of subsequent numbers is already orderly , There is no need to make redundant cycles to compare . So in the first layer of the loop, add a bool Variable flag == false To judge , If there is an exchange in the second layer of the loop , will flag == true. Judge after exiting the second cycle flag, If it is true The cycle continues , If it is false, Explain that the following numbers are not exchanged , It means that everything is in order , Don't cycle , Just exit the first layer of circulation .
// Bubble sort
void BubbleSort(ElemType A[], int n) {
bool flag;
for (int i = 0; i < n - 1; i++) {
//i Is to control how many orders there are
flag = false;
for (int j = n - 1; j > i; j--) {
// Internal control comparison and Exchange
if (A[j - 1] > A[j]) {
swap(A[j - 1], A[j]);
flag = true;
}
}
if (false == flag)
{
break;
}
}
}

边栏推荐
- Solve the problem of invalid modification of QT 5 interface. Solve the problem of invalid modification of qtdesigner
- Koa2 learning
- Sword finger offer 54. the k-th node of the binary search tree
- The code spell checker plug-in avoids some specific vocabulary errors "XXX": unknown word.cspell
- MFC IniFile Unicode mode reading method
- Seekbar属性参考
- (2022 Niuke multi school) D-Link with game glitch (SPFA)
- Mechanism and principle of multihead attention and masked attention
- 剑指 Offer 36. 二叉搜索树与双向链表
- Ffmpeg notes (I) fundamentals of audio and video
猜你喜欢

HTB-Optimum

Use abp Zero builds a third-party login module (4): wechat applet development

Ffmpeg notes (I) fundamentals of audio and video

Pdf snapshot artifact

【Unity3D】UGUI回调函数

Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
This is how the permission system is designed, yyds

(Niuke multi school I in 2022) i-chiitoitsu (expected DP)
![(14) [driver development] configuration environment vs2019 + wdk10 write XP driver](/img/90/0d94d26be8128d77de65919763fda5.png)
(14) [driver development] configuration environment vs2019 + wdk10 write XP driver

Unity model simplification / consolidation one click plug-in
随机推荐
嵌入式c语言开发之宏定义求两个数的最大值的使用技巧
机器学习 Keras拟合正弦函数
The most comprehensive multi-threaded application tutorial - summary in detail
深度解析:2022年最火的商业模式链动2+1,是合法模式吗?
【Node】服务端口被占用Error: listen EADDRINUSE: address already in use :::9000-如何关闭node启动的端口
Codeforces Round #809 (Div. 2)
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 link with game glitch (SPFA finds positive and negative links)
Req.body in node.express is always undefind
[C language] in depth understanding of pointers and arrays (phase I)
Mechanism and principle of multihead attention and masked attention
Machine learning keras fitting sine function
Sword finger offer 45. arrange the array into the smallest number
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
[QT] solve the problem of Chinese garbled code output from QT console
leetcode/二进制加法
context must be a dict rather解决
Classic cases of static keywords and block blocks
Define usage method and template
EOL offline sequence based on iso13209 (Otx)
ROI pooling and ROI align