c++
#include <iostream>
using namespace std;
void bubble_sort(int arry[],int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
if (arry[j] > arry[j + 1])
{
int temp = arry[j];
arry[j] = arry[j + 1];
arry[j + 1] = temp;
}
}
}
}
void print_arry(int arry[],int len)
{
for (int i = 0; i < len; i++)
{
cout << arry[i] << endl;
}
}
int main()
{
int arry[] = { 100,1021,3000,4001,21,1212,121};
int len = sizeof(arry) / sizeof(arry[0]);
//cout << len;
print_arry(arry, len);
bubble_sort(arry, len);
cout << "*******after handle********"<<endl;
print_arry(arry, len);
return 0;
}
输出效果:
python
def bubble_sort(li):
for i in range(len(li)-1):
for j in range(len(li)-i-1):
if (li[j]>li[j+1]):
li[j],li[j+1] = li[j+1],li[j]
return li
li = [133,2,4,5,8,3,111,212,299]
print(li)
print("---------------------冒泡处理后---------------------------")
print(bubble_sort(li))
输出效果:
不得不感慨还是python简洁啊
|