Sorting Algorithm:Bubble+QuickSort+Heapsort

来源:互联网 发布:手机相册数据恢复 编辑:程序博客网 时间:2024/06/07 04:09
说明:gcc 版本 4.1.1 20061011 (Red Hat 4.1.1-30)
系统:2.6.20-1.2952.fc6
配置:P4-2.4B  1G-266Mhz  300G-IDE  MX400

/*========================bubble.c===========================*/
//0:09.90elapsed
//bubble.c该文档没有设置起泡条件标志,因此N个元素,将作满N-1轮起泡查询
#include <stdlib.h>
#include <stdio.h>
#define  COUNTS 40000


int main()
{
  int array[COUNTS];
  int i,j;
  for( i=0;i<COUNTS;i++ )
    array[i]=rand()%100000;

  printf("before sorting:/n");
  for(i=0;i<COUNTS;i++)
    printf("%d ",array[i]);  //test the value of the array[]

  int loopcount=0;
  int temp;
  for( j=COUNTS-1;j>0;j-- ){
    for( i=0;i<j;i++){
     if( array[i] < array[i+1] ){
      temp = array[i+1];
      array[i+1] = array[i];
      array[i] = temp;   
     }
    }
    loopcount++;  //循环次数计数器,测使用
  }

  printf("/n");
  printf("after sorting:/n");
  for( i=0;i<COUNTS;i++)
    printf("%d ",array[i]);
  printf("/n");
  printf("the bubble of loopcount = %d/n",loopcount);

  exit(0);
}
 


/*========================bubble2.c===========================*/
//7.93user 0.00system 0:10.05elapsed 79%CPU
//bubble2.c 设置了起泡条件标志flag,因此N个元素的起泡过程,将<N-1
#include <stdlib.h>
#include <stdio.h>
#define  COUNTS 40000


int main()
{
  int array[COUNTS];
  int i,j;
  for( i=0;i<COUNTS;i++ )
    array[i]=rand()%100000;

  printf("before sorting:/n");
  for(i=0;i<COUNTS;i++)
    printf("%d ",array[i]);  //test the value of the array[]

  int loopcount=0;
  int flag=1;  //flag用以标识一轮起泡过程中,是否有起泡发生,=1表示有起泡,下一轮还要再进行一回起泡“查询”
  int temp;
  for( j=COUNTS-1;j>0;j-- ){
    if(flag == 1){
      flag = 0;       //每一轮新的起泡扫描过程,都先将起泡标志清零
      for( i=0;i<j;i++ ){    //每一轮起泡的for循环

    if( array[i]<array[i+1] ){
      temp=array[i+1];
      array[i+1]=array[i];
      array[i]=temp; 
      flag=1;    //有位置交换,说明有起泡,置位
     
    }

      }
      loopcount++; //循环次数计数器,测使用

    }
    else break;
   
  }
  printf("/n");
  printf("after sorting:/n");
  for( i=0;i<COUNTS;i++)
    printf("%d ",array[i]);
  printf("/n");
  printf("/n");
  printf("the bubble2 of loopcount = %d/n",loopcount);

  exit(0);
}


/*====================quicksort1.c======================*/
//此快速排序,未用三值取中分割法,轴记录只简单取第一个元素
//如果采用三值取中分割法,并进一步结合冒泡,将在一定程度上
//进一步提高算法的平均性能
//0.04user 0.00system 0:01.52elapsed 2%CPU
#include <stdlib.h>
#include <stdio.h>
#define  COUNTS  40000   //待排序元素个数

typedef struct {
  int *elem;
  int length;
  int listsize;
}SqList;       //数据结构定义

int partition(SqList ls, int low ,int high);  //一趟快速分割
void QSort(SqList ls,int low,int high); //利用递归分割,实现快速排序
void quicksort(SqList ls);   //将多个子函数整合在一起的快速排序函数


int main()
{
  int i,j;

  SqList L;
  L.elem = (int *)malloc( (COUNTS+1)*sizeof(int) );
  if(!L.elem) exit(1);
  L.length = COUNTS;
  L.listsize = COUNTS;       //线性表初始化

  for(i=1;i<=COUNTS;i++)
    L.elem[i] = rand()%10000;

  printf("before sorting:/n");
  for(i=1;i<=COUNTS;i++)
    printf("%d ",L.elem[i]);
  printf("/n");             //线性表元素赋值以及元素值打印显示

  quicksort(L);  //快速排序

  printf("/nafter sorting:/n");
  for(i=1;i<=COUNTS;i++)
    printf("%d ",L.elem[i]);
  printf("/n");
 
  exit(0);
}



int partition(SqList ls, int low ,int high){
   
  int tmp;
  int pivotkey = ls.elem[low];

  while( low<high ){
    while( low<high && pivotkey<=ls.elem[high] ) high--;
    ls.elem[low]  = ls.elem[high];
    while( low<high && ls.elem[low]<=pivotkey ) low++;
    ls.elem[high] = ls.elem[low];
  }
  ls.elem[low] = pivotkey;
  return low;
}//每一轮分割

void QSort(SqList ls,int low,int high){
  if( low<high ){
    int pivotloc =  partition(ls,low,high);
    QSort(ls,low,pivotloc-1);
    QSort(ls,pivotloc+1,high);
  }
}//快速排序,总体递归

void quicksort(SqList ls){
   QSort(ls,1,ls.length);
}//用quicksort()主要考虑到:便于主体代码的阅读,也可以直接用QSort

/////////////
 
原创粉丝点击