冒泡排序,一种交换排序,它的基本思想是:两两比较关键字,如果反序则交换,直到没有反序的记录为止。
(1).最简单的排序实现
//让每一个关键字,都和它后面的每个关键字比较,如果大则交换,这样第一位置的关键字在一次循环后一定变成最小值。
void BubbleSort0(SqList* L)
{
for (int i=0;i<L->iLength;i++)
{
for (int j=i+1;j<L->iLength;j++)
{
if (L->iElem[i]>L->iElem[j])
{
Swap(L,i,j);
PrintList(*L);
}
}
}
}
(2).正宗的冒泡排序
//冒泡排序
void BubbleSort(SqList* L)
{
for (int i=0;i<L->iLength;i++)
{
for (int j=L->iLength-1;j>=i;j--)
{
if (L->iElem[j-1]>L->iElem[j])
{
Swap(L,j-1,j);
PrintList(*L);
}
}
}
}
(3).冒泡法优化方式一
//冒泡排序优化
void BubbleSort2(SqList* L)
{
bool bFlag = true;
for (int i=0;i<L->iLength && bFlag;i++)
{
bFlag = false;
for (int j=L->iLength-1;j>=i;j--)
{
if (L->iElem[j-1]>L->iElem[j])
{
Swap(L,j-1,j);
bFlag = true;
PrintList(*L);
}
}
}
}
(4).冒泡法优化方式二
//类似BubbleSort2,程序一直在两两相邻元素进行比较,如果发生元素交换则进行下一次比较,直到没有元素间交换则证明排序完成
void BubbleSort3(SqList* L)
{
int i= MAX_SIZE -1; //初始时,最后位置保持不变
while ( i> 0)
{
int pos= 0; //每趟开始时,无记录交换
for (int j= 0; j< i; j++)
if (L->iElem[j]> L->iElem[j+1])
{
pos= j; //记录交换的位置
Swap(L,j,j+1);
PrintList(*L);
}
i= pos; //为下一趟排序作准备
}
}
(5).冒泡法优化方式三
//传统冒泡排序中每一趟排序操作只能找到一个最大值或最小值,我们考虑利用在每趟排序中进行正向和
//反向两遍冒泡的方法一次可以得到两个最终值(最大者和最小者) , 从而使排序趟数几乎减少了一半。
void BubbleSort4(SqList* L)
{
int low = 0;
int high= L->iLength -1; //设置变量的初始值
int j;
while (low < high)
{
for (j= low; j< high; ++j) //正向冒泡,找到最大者
if (L->iElem[j]> L->iElem[j+1]) {
Swap(L,j,j+1);
PrintList(*L);
}
--high; //修改high值, 前移一位
for ( j=high; j>low; --j) //反向冒泡,找到最小者
if (L->iElem[j]<L->iElem[j-1]) {
Swap(L,j,j-1);
PrintList(*L);
}
++low; //修改low值,后移一位
}
}
完整程序及运行结果:
#include <iostream>
#include <time.h>
using namespace std;
#define MAX_SIZE 5
typedef struct
{
int iElem[MAX_SIZE];
int iLength;
}SqList;
void PrintList(SqList L)
{
for (int i=0;i<L.iLength;i++)
{
cout<<L.iElem[i]<<" ";
}
cout<<endl;
}
void CreateList(SqList* L)
{
int i = 0;
L->iLength = 0;
//srand((unsigned)time(NULL));
int iData[MAX_SIZE]={19,4,84,38,26};
for (i=0; i<MAX_SIZE; i++)
{
//L->iElem[i] = rand()%100;
L->iElem[i] = iData[i];
L->iLength++;
}
PrintList(*L);
}
void Swap(SqList* L,int i,int j)
{
int temp=0;
temp = L->iElem[i];
L->iElem[i] = L->iElem[j];
L->iElem[j] = temp;
}
//让每一个关键字,都和它后面的每个关键字比较,如果大则交换,这样第一位置的关键字在一次循环后一定变成最小值。
void BubbleSort0(SqList* L)
{
for (int i=0;i<L->iLength;i++)
{
for (int j=i+1;j<L->iLength;j++)
{
if (L->iElem[i]>L->iElem[j])
{
Swap(L,i,j);
PrintList(*L);
}
}
}
}
//冒泡排序
void BubbleSort(SqList* L)
{
for (int i=0;i<L->iLength;i++)
{
for (int j=L->iLength-1;j>=i;j--)
{
if (L->iElem[j-1]>L->iElem[j])
{
Swap(L,j-1,j);
PrintList(*L);
}
}
}
}
//冒泡排序优化
void BubbleSort2(SqList* L)
{
bool bFlag = true;
for (int i=0;i<L->iLength && bFlag;i++)
{
bFlag = false;
for (int j=L->iLength-1;j>=i;j--)
{
if (L->iElem[j-1]>L->iElem[j])
{
Swap(L,j-1,j);
bFlag = true;
PrintList(*L);
}
}
}
}
//类似BubbleSort2,程序一直在两两相邻元素进行比较,如果发生元素交换则进行下一次比较,直到没有元素间交换则证明排序完成
void BubbleSort3(SqList* L)
{
int i= MAX_SIZE -1; //初始时,最后位置保持不变
while ( i> 0)
{
int pos= 0; //每趟开始时,无记录交换
for (int j= 0; j< i; j++)
if (L->iElem[j]> L->iElem[j+1])
{
pos= j; //记录交换的位置
Swap(L,j,j+1);
PrintList(*L);
}
i= pos; //为下一趟排序作准备
}
}
//传统冒泡排序中每一趟排序操作只能找到一个最大值或最小值,我们考虑利用在每趟排序中进行正向和
//反向两遍冒泡的方法一次可以得到两个最终值(最大者和最小者) , 从而使排序趟数几乎减少了一半。
void BubbleSort4(SqList* L)
{
int low = 0;
int high= L->iLength -1; //设置变量的初始值
int j;
while (low < high)
{
for (j= low; j< high; ++j) //正向冒泡,找到最大者
if (L->iElem[j]> L->iElem[j+1]) {
Swap(L,j,j+1);
PrintList(*L);
}
--high; //修改high值, 前移一位
for ( j=high; j>low; --j) //反向冒泡,找到最小者
if (L->iElem[j]<L->iElem[j-1]) {
Swap(L,j,j-1);
PrintList(*L);
}
++low; //修改low值,后移一位
}
}
int main()
{
SqList L;
char opp='-1';
cout<<"Input the bubble style number(0-4):";
while (cin>>opp != '\0' && cin.get() == '\n')
{
CreateList(&L);
switch (opp)
{
case '0':
BubbleSort0(&L);
break;
case '1':
BubbleSort(&L);
break;
case '2':
BubbleSort2(&L);
break;
case '3':
BubbleSort3(&L);
break;
case '4':
BubbleSort4(&L);
default:
break;
}
cout<<"Input the bubble style number(0-4):";
}
return 0;
}
参考书籍:大话数据结构
网页参考:http://blog.csdn.net/hguisu/article/details/7776068/