def BubbleSort(): Array = [1,4,6,8,3] for i in range(len(Array)-1): for j in range(len(Array)-i-1): if Array[j]>Array[j+1]: #两两比较,谁小放前面 Array[j],Array[j+1] = Array[j+1],Array[j] print Array C++实现:
void SimpleBubbleSort()
{
int v1[] = { 1,4,7,2,8,4,5 };
int Temp = 0;
int Length = (sizeof(v1) / sizeof(int));
int i = 0;
int j = 0;
for (i; i < Length; i++)
{
for (j = i + 1; j < Length; j++)
{
if (v1[i] > v1[j])
{
Temp = v1[i];
v1[i] = v1[j];
v1[j] = Temp;
}
}
}
for (i = 0; i < Length; i++)
{
printf("%d\r\n", v1[i]);
}
}
时间: 2024-10-26 12:51:18