/*************************************************************************
> File Name: bubble_sort.cpp
> Author:
> Mail:
> Created Time: 六 5/ 5 07:54:07 2018
************************************************************************/
#include <iostream>
using namespace std;
/*
* 冒泡排序算法
* @param int *nums 待排序的数组
* @param int length 待排序的数组的长度
*/
void bubble_sort(int *nums, int length) {
for(int pos = 0; pos < length -1; pos++) {
if (nums[pos] > nums[pos + 1]) {
int temp = nums[pos + 1];
nums[pos + 1] = nums[pos];
nums[pos] = temp;
}
}
}
int main() {
int length = 10;
int nums[10] = {12, 1, 6, 4, 5, 8, 7, 34, 25, 67};
bubble_sort(nums, length);
for(int pos = 0; pos < length; pos++) {
cout << nums[pos] << endl;
}
return 0;
}
冒泡排序
最新推荐文章于 2023-06-06 09:14:05 发布