// 冒泡排序,bubbleSort
// O(n^2),稳定
#include <cstdio>
void bubbleSort(int array[], int length)
{
for(int i = 0; i < length-1; i++)
{
for(int j = i+1; j < length; j++)
{
if(array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
int main()
{
int array[] = {1,3,4,5,2,6,9,7,8,0};
int n = sizeof(array)/sizeof(array[0]);
bubbleSort(array, n);
for(int i = 0; i < n; i++)
printf("%d ",array[i]);
}
冒泡排序,bubbleSort
最新推荐文章于 2022-04-13 15:17:52 发布