排序算法的基本操作

2023-07-19 0 489
////插入排序
//直接插入排序 
void insertSort(int a[],int n){
	int i,j;
	for(i=2;i<=n;i++){
		if(a[i]<a[i-1]){
			a[0] = a[i];
		    for(j=i-1;a[0]<a[j];--j){
			   a[j+1] = a[j];
		    }
		    a[j+1] = a[0];
		} 	
	} 
} 

//二分插入排序
void insertBiSort(int a[],int n){
	int i,j,low,high,mid;
	for(i=2;i<=n;i++){
		a[0] = a[i];
		low = 1; high = i-1;
		while(low<=high){
			mid = (low+high)/2;
			if(a[mid]>a[0]){
				high = mid-1;
			}else{
				low = mid+1;
			}
		}
		for(j=i-1;j>=high+1;j--){
			a[j+1] = a[j];
		}
		a[high+1] = a[0];
	}
} 

//希尔排序
void shellSort(int a[],int n){
	int dk,i,j;
	for(dk=n/2;dk>=1;dk=dk/2){
		for(i=dk+1;i<=n;++i){
			if(a[i]<a[i-dk]){
				a[0] = a[i];
				for(j=i-dk;j>0&&a[0]<a[j];j=j-dk){
					a[j+dk] = a[j];
				}
				a[j+dk] = a[0];
			}
		}
	}
} 

////交换排序 
//冒泡排序
void bubbleSort(int a[],int n){
	//为什么是n-1,因为i=n-1时下方的循环不做操作
	//其实i在这里<n或小于n-1均不影响操作,但使用n-1会少一次比较,推荐使用 
	for(int i=0;i<n-1;i++){
	   bool flag = false;
	   //为什么是n-1,因为下标到n-1 
	   for(int j=n-1;j>i;j--){
	   	 if(a[j-1]>a[j]){
	   	   int temp = a[j-1];
		   a[j-1] = a[j];
		   a[j] = temp;
		   flag = true;	
	     }
	   }
	   if(!flag)return;
	}
} 

//快速排序
int Partition(int a[],int low,int high){
	int pivot = a[low];
	while(low<high){
		while(low<high&&a[high]>pivot)high--;
		a[low] = a[high];
		while(low<high&&a[low]<pivot)low++;
		a[high] = a[low];
	}
	a[low] = pivot;
	return low;
}
void fastSort(int a[],int low,int high){
	if(low<high){
		int pivotpos = Partition(a,low,high);
		fastSort(a,low,pivotpos-1);
		fastSort(a,pivotpos+1,high);
	}
} 

////选择排序 
//堆排序
void headAdjust(int a[],int k,int len){
	a[0] = a[k];
	for(int i=2*k;i<=len;i*=2){
		if(i<len&&a[i]<a[i+1])i++;
		if(a[0]>a[i])break;
		else{
			a[k] = a[i];
			k = i;
		}
	}
	a[k] = a[0]; 
}

void buildMaxHeap(int a[],int len){
	for(int i=len/2;i>0;i--){
		headAdjust(a,i,len);
	}
}

void headSort(int a[],int n){
	buildMaxHeap(a,n);
	for(int i=n;i>1;i--){
		int temp = a[1];
		a[1] = a[i];
		a[i] = temp;
		headAdjust(a,1,i-1);
	}
}
//简单选择排序 
void selectSort(int a[],int n){
	for(int i=0;i<n-1;i++){
		int min = i;
		for(int j=i+1;j<n;j++){
			if(a[j]<a[min])min = j;
		}
		if(min!=i){
			int temp = a[i];
		    a[i] = a[min];
		    a[min] = temp;
		}	
	}
} 
//归并排序
int *b = (int*)malloc((MAXSIZE+1)*sizeof(int));
void merge(int a[],int low,int mid,int high){
	int i,j,k;
	for(k=low;k<=high;k++){
		b[k] = a[k];
	}
	for(i=low,j=mid+1,k=i;i<mid&&j<=high;k++){
		if(b[i]<=b[j])a[k] = b[i++];
		else a[k] = b[j++];
	}
	while(i<=mid)a[k++] = b[i++];
	while(j<=high)a[k++] = b[j++];
}

void mergeSort(int a[],int low,int high){
	if(low<high){
		int mid = (low+high)/2;
		mergeSort(a,low,mid);
		mergeSort(a,mid+1,high);
		merge(a,low,mid,high);
	}
}
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

本站部分资源收集自互联网,我站仅分享内容,仅供用户个人研究,不提供商业使用,如有侵权下架处理,觉得资源内容不错请您在官方渠道购买正版,您在阅读的同时下载的内容本站不承担任何法律责任!Some resources of this website are collected from the Internet. We only share the content, which is only for personal research of users, and not for commercial use. If there is infringement and off shelf processing, please purchase the authentic version of the resource content from the official channel if you think it is good. We will not bear any legal responsibility for the content you download while reading!

橙凰素材-精品源码下载|正版主题更新 学习交流 排序算法的基本操作 https://b.bqzmz.com/2023/07/19/%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c/

为用户提供极致的体验

上一篇: 图的习题
下一篇:

已经没有下一篇了!

常见问题
  • 正版主题是指站长在指定渠道购买的官方主题,提供作者联系,资源支持,长期更新等等内容
查看详情
  • 非常抱歉,请联系网站页脚的客服,我们将在核实后下架处理并返还给您此资源的收益(会扣去网站运营的费用),谢谢理解!
查看详情
  • 网络资源是站长从互联网分享收集而来的内容,本站不会做测试,直接分享,不包源码可用性,运营性,如有需求点击右边的联系正版,谢谢!
查看详情
  • 如果资源不是最新版本,请在资源下方评论区进行催更或提交工单处理,我们会尽快更新并通过邮箱通知您。
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务