BubbleSort
单向冒泡排序
void BubbleSort(int Iarray[], int n)
{
int i = 0, j = 0;
int flag = 1;
for (i = 0; i < n - 1; i++)
{
flag = 1;
for (j = 0; j < n - i - 1; j++)
{
if(Iarray[i] > Iarray[i+1])
{
int temp = 0;
temp = Iarray[j];
Iarray[j] = Iarray[j+1];
Iarray[j+1] = temp;
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
双向冒泡
void BubbleSort2(int num[], int low, int high)
{
int i = 0;
while(low < high)
{
for (i = low; i < high; i++)
{
if (num[i] > num[i+1])
{
swap(num[i], num[i+1]);
}
}
high--;
for (i = high; i > low; i--)
{
if (num[i] <num[i-1])
{
swap(num[i], num[i-1]);
}
}
low++;
}
}