排序算法
Published:
·
LastMod: August 01, 2022
·
165 words
排序算法样板代码 🔗
冒泡排序 🔗
typescript 🔗
1
2
3
4
5
6
7
8
9
10
11
| function bubble_sort(arr: number[] = []): void {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
let temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
}
}
|
c++ 🔗
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| void swap(int &a1, int &a2)
{
int temp = a1;
a1 = a2;
a2 = temp;
}
void bubble_sort(std::vector<int> &vs)
{
int sz = vs.size();
for (int i = 0; i < sz; i++)
{
for (int j = i + 1; j < sz; j++)
{
if (vs[i] > vs[j])
{
swap(vs[i], vs[j]);
}
}
}
}
|