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
/////////////
系统: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
/////////////
//堆排序:
#include <stdlib.h>
#include <stdio.h>
#define MAXSIZE 60
void adjust(int a[],int top,int bottom)
{
int j,tmp;
int s,m;
s = top;
m = bottom;
tmp = a[top];
for(j=2*s;j<=m;j*=2){
if( j<m && a[j]<a[j+1] ) j++;
if( a[j]<= tmp) break;
a[s] = a[j];
s = j;
}
a[s] = tmp;
}
int main()
{
int i,arr[MAXSIZE+1],sorted;
for(i=1;i<=MAXSIZE;i++){
arr[i] = rand()%(MAXSIZE*10);
printf("%7d",arr[i]);
if(i%7 == 0) printf("/n");
}
printf("/nCreat the heap:/n");
//建堆:
for(i=MAXSIZE/2;i>0;--i)
adjust(arr,i,MAXSIZE);
printf("建好的堆:/n");
for(i=1;i<=MAXSIZE;i++){
printf("%7d",arr[i]);
if(i%7 == 0) printf("/n");
}
//堆排序过程:
for(i=MAXSIZE;i>0;--i){
sorted = arr[1];
arr[1] = arr[i];
arr[i] = sorted;
adjust(arr,1,i-1);
}
printf("/nthe array after sorting:/n");
for(i=1;i<=MAXSIZE;i++){
printf("%7d",arr[i]);
if(i%7 == 0) printf("/n");
}
printf("/n");
}
- Sorting Algorithm:Bubble+QuickSort+Heapsort
- Sorting Algorithm-Bubble Sort
- Heapsort algorithm
- bubble sorting
- Heapsort, Quicksort, and Entropy
- QuickSort and HeapSort
- Analysis and implement some simple sorting algorithm.(2)——heapsort
- Quicksort Algorithm
- the heapsort algorithm
- Sorting Algorithm
- Sorting Algorithm
- Sorting Algorithm
- The Bubble Sorting
- Sorting - bubble sort
- Classical Classification:Bubble Sorting
- Internal Sorting: Heapsort-2:Sorting by Selection
- Internal Sorting: Heapsort-1: Sorting by Selection
- Csharp Algorithm - Bubble Sort
- 生活在Emacs中(zz)
- Tomcat5.5数据源的配置
- hippie-expand(zz)
- Gcc的编译流程分为了四个步骤:
- 绕过Hibernate API,直接通过JDBC API批量更新和插入数据
- Sorting Algorithm:Bubble+QuickSort+Heapsort
- linux下yum的使用
- VoIP的原理及技术
- windows遍历目录下所有文件
- Windows CE OAL层的结构与开发
- zz-程序员的emacs配置大全(cedet+ecb+cscope+gdb-ui)
- zz-Emacs常用快捷键
- 用java实现从文本文件批量导入数据至数据库
- linux内核编译详解- -文摘No.1(zz)