mirror of
https://github.com/hao14293/2021-Postgraduate-408.git
synced 2026-07-17 11:40:47 +08:00
Create bubbleSort.cpp
This commit is contained in:
22
Data-Structure/Sort/sorts/bubbleSort.cpp
Normal file
22
Data-Structure/Sort/sorts/bubbleSort.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
int a[] = {6, 5, 2, 8, 4, 1, 3, 7};
|
||||
int len = sizeof(a) / sizeof(a[0]);
|
||||
int m = len - 1, flag = 1;
|
||||
while((m > 0) && (flag == 1)){
|
||||
flag = 0;
|
||||
for(int i = 1; i <= m; i++){
|
||||
if(a[i - 1] > a[i]){
|
||||
flag = 1;
|
||||
int temp = a[i - 1];
|
||||
a[i - 1] = a[i];
|
||||
a[i] = temp;
|
||||
}
|
||||
}
|
||||
m--;
|
||||
}
|
||||
for(int i = 0; i < len; i++)
|
||||
cout << a[i];
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user