#include "stdafx.h"
#include <iostream>
#define DATA_SIZE 14
void BubbleSort1(int data[], int iDataSize)
{
int iTemp = 0;
int iCompareCount = 0;
int iMoveCount = 0;
for ( int i = 0; i < iDataSize - 1; i++)
{
for ( int j = i + 1; j < iDataSize; j++)
{
iCompareCount++;
if ( data[i] > data[j] )
{
iMoveCount++;
iTemp = data[j];
data[j] = data[i] ;
data[i] = iTemp;
}
}
}
printf("第一种冒泡排序:\n排序进行了%d次比较,进行了%d次移动\n", iCompareCount, iMoveCount);
}
void BubbleSort2(int data[], int iDataSize)
{
int iTemp = 0;
int iCompareCount = 0;
int iMoveCount = 0;
for ( int i = 0; i < iDataSize - 1; i++)
{
for ( int j = iDataSize - 1; j > i; j--)
{
iCompareCount++;
if ( data[j - 1] > data[j] )
{
iMoveCount++;
iTemp = data[j - 1];
data[j - 1] = data[j] ;
data[j] = iTemp;
}
}
}
printf("第二种冒泡排序:\n排序进行了%d次比较,进行了%d次移动\n", iCompareCount, iMoveCount);
}
void BubbleSort3(int data[], int iDataSize)
{
int iTemp = 0;
int iCompareCount = 0;
int iMoveCount = 0;
bool bFlag = true;
for ( int i = 0; i < iDataSize - 1 && bFlag; i++)
{
for ( int j = iDataSize - 1; j > i; j--)
{
iCompareCount++;
bFlag = false;
if ( data[j - 1] > data[j] )
{
iMoveCount++;
iTemp = data[j - 1];
data[j - 1] = data[j] ;
data[j] = iTemp;
bFlag = true;
}
}
}
printf("第三种冒泡排序:\n排序进行了%d次比较,进行了%d次移动\n", iCompareCount, iMoveCount);
}
void printResult(int data[], int iDataSize)
{
printf("排序后的结果是:");
for ( int i = 0; i < iDataSize; i++)
{
printf("%d ", data[i]);
}
printf("\n\n");
}
void sort1()
{
int data[DATA_SIZE] = {3,6,1,4,2,5,9,33,8,1,22,32,11,18};
BubbleSort1(data, DATA_SIZE);
printResult(data, DATA_SIZE);
}
void sort2()
{
int data[DATA_SIZE] = {3,6,1,4,2,5,9,33,8,1,22,32,11,18};
BubbleSort2(data, DATA_SIZE);
printResult(data, DATA_SIZE);
}
void sort3()
{
int data[DATA_SIZE] = {3,6,1,4,2,5,9,33,8,1,22,32,11,18};
BubbleSort3(data, DATA_SIZE);
printResult(data, DATA_SIZE);
}
int _tmain(int argc, _TCHAR* argv[])
{
sort1();
sort2();
sort3();
getchar();
return 0;
}