起泡排序(Bubble Sort),依次比较相邻的两个记录的关键字,逆序时交换,一趟比较后最大的记录被安置到最后一个记录的位置上。时间复杂度为O(n^2)。
void CExchangeSort::BubbleSort(void)
{
const int count = 9, length = count -1;
int L[count] = {0, 49, 38, 65, 97, 76, 13, 27, 49};
bool change = true;
for (int i = count - 1; i > 0 && change; -- i)
{
change = false;
for (int j = 0; j < i; ++ j)
if (L[j] > L[j + 1])
{
int temp = L[j];
L[j] = L[j + 1];
L[j + 1] = temp;
change = true;
}
}
//打印排序结果。
for (int i = 0; i <= length; ++ i)
{
cout << L[i] << "\t";
}
cout << endl;
}