冒泡排序、直接插入排序、选择排序速度比较

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <Mmsystem.h>
 
#define MAXSIZE 10000
 
typedef struct
{
    int r[MAXSIZE + 1];
int length;
} SqList;
 
void swap(SqList *pL,int i,int j)
{
int temp = 0;
 
temp = pL->r[i];
pL->r[i] = pL->r[j];
pL->r[j] = temp;
}
 
void putOut(SqList *pL)
{
int i;
 
for(i=0;i<pL->length;i++)
{
printf(" %d",pL->r[i]);
}
printf("\n");
}
 
void BubbleSort1(SqList *pL)
{
    int i,j,c,s;
    
c = 0;
s = 0;
for(i =0; i<pL->length-1; i++)
{
    for(j=i+1;j<pL->length;j++)   //这种方式比较,从a[i]开始,依次和a[i++]后的元素比较,有大的则交换,
{                             //每个元素都与它后面的所有元素进行比较,不是严格意思的冒泡算法
            //c ++;
    if(pL->r[i] > pL->r[j])
{
    swap(pL,i,j);
//putOut(pL);
//s ++;
}
}
}
 
}
 
void BubbleSort2(SqList *pL)
{
    int i,n;
    int flag;
 
flag = 1;
    n = 1;
while(flag)
{
flag = 0;
for(i=pL->length-1;i>=n;i--) //从最后一个元素开始,每个元素和它前一个元素比较,大则交换,这样一次循环,最小的
{                            //元素置于第一位,第二次循环仅需要比较至第二个元素(第一个元素已是最小),如果一轮
//c ++ ;                   //比较,没有元素交换,说明已经完成排序。
if (pL->r[i-1]>pL->r[i])
{
flag = 1;
                swap(pL,i-1,i);
//s ++;
}
}
n ++;
}
 
}
 
void BubbleSort3(SqList *pL)
{
    int i,n,k;
    int flag;
 
flag = 1;
    n = 1;
while(flag)
{
flag = 0;
for(i=pL->length-1;i>=n;i--)  //这个和前面的BubbleSort2类似,改进处在于,记录在一轮比较中,发生了交换的下标;
{                             //那么这个下标前的记录,已经是有序的了。不需要再次比较了。
if (pL->r[i-1]>pL->r[i])
{
flag = 1;
k = i;
                swap(pL,i-1,i);
}
}
n = k+1;
}
 
}
/************************************************************************/
/*          直接插入排序方法                                            */
/************************************************************************/
void InsertSort(SqList *pL)
{
int i,j;
int temp;
 
for(i =1 ;i<pL->length;i++)
{
temp = pL->r[i];
for (j=i-1;j>=0 && temp < pL->r[j];j--)
{
pL->r[j+1] = pL->r[j];
}
pL->r[j+1] = temp; //这里j+1是因为前面的for循环的最后j--被执行了
}
}
/************************************************************************/
/*                      简单选择排序                                    */
/************************************************************************/
void selectSort(SqList *pL)
{
int i,j;
    int min; //最小元素的小标
 
for (i=0;i<pL->length;i++)
{
min = i;
        for (j=i+1;j<pL->length;j++)
        {
if (pL->r[j]<pL->r[min])
{
min = j;
}
        }
        if (min != i)
        {
swap(pL,i,min);
        }
}
}
 
//排序方法耗时比较
void pk(SqList *pL)
{
    long dwStart,dwEnd;
    SqList sqList1,sqList2,sqList3,sqList4;
SqList sqList5;
    
memcpy(&sqList1,pL,sizeof(SqList));
memcpy(&sqList2,pL,sizeof(SqList));
    memcpy(&sqList3,pL,sizeof(SqList));
memcpy(&sqList4,pL,sizeof(SqList));
memcpy(&sqList5,pL,sizeof(SqList));
 
 
    dwStart  = timeGetTime();
    BubbleSort1(&sqList1);
    dwEnd = timeGetTime();
printf("BubbleSort1:%d\n",dwEnd-dwStart);
    
dwStart  = timeGetTime();
BubbleSort2(&sqList2);
dwEnd = timeGetTime();
printf("BubbleSort1:%d\n",dwEnd-dwStart);
 
dwStart  = timeGetTime();
    BubbleSort2(&sqList3);
dwEnd = timeGetTime();
printf("BubbleSort2:%d\n",dwEnd-dwStart);
 
dwStart  = timeGetTime();
    InsertSort(&sqList4);
dwEnd = timeGetTime();
printf("InsertSort:%d\n",dwEnd-dwStart);
 
dwStart  = timeGetTime();
    selectSort(&sqList5);
dwEnd = timeGetTime();
printf("selectSort:%d\n",dwEnd-dwStart);
 
 
}
 
void main()
{
int i;
 
    SqList sqList;
    
sqList.length = MAXSIZE;
srand(time(0));
    for(i=0;i<sqList.length;i++)
{
        sqList.r[i] = rand()%MAXSIZE + 1;
}
    
pk(&sqList);
    //putOut(&sqList);
//InsertSort(&sqList);
//selectSort(&sqList);
    //putOut(&sqList);
}
==========================
BubbleSort1:1987
BubbleSort1:1266
BubbleSort2:1265
InsertSort:188
selectSort:297
以上是执行结果,总结:
1,冒泡排序,无论如何优化,其效率都无显著提高;
2,直接插入排序和选择排序,明显比冒泡排序要快;
3,直接插入排序最快,其次是选择排序,冒泡排序效率最低。
posted on 2013-06-30 16:50  转身的老虎  阅读(752)  评论(0编辑  收藏  举报