当前位置:网站首页>C语言:归并排序
C语言:归并排序
2022-07-28 12:18:00 【念迟鱼学编程】
#include <stdio.h>
void showArr(int arr[] , int length);
void mergeSort(int a[] , int alen , int b[] , int blen , int *temp);
int main()
{
int a[5] = {
1,3,5,7,9};
int b[3] = {
2,8,10};
int temp[8];
mergeSort(a,5,b,3,temp);
printf("归并排序后的序列为:\n");
showArr(temp,8);
return 0;
}
void mergeSort(int a[] , int alen , int b[] , int blen , int *temp)
{
int i = 0;
int j = 0;
int k = 0;
while(i < alen && j < blen){
if(a[i]<b[j]){
temp[k] = a[i];
i++;
k++;
}else{
temp[k] = b[j];
k++;
j++;
}
}
while(i < alen){
temp[k] = a[i];
k++;
i++;
}
while(j < blen){
temp[k] = b[j];
k++;
j++;
}
}
void showArr(int arr[] , int length)
{
for(int i = 0 ; i < length ; i++){
printf("%4d",arr[i]);
}
printf("\n");
}
边栏推荐
猜你喜欢

How to design a second kill system?

Map tiles: detailed explanation of vector tiles and grid tiles

MySQL practice -- master-slave replication

Shell basic concepts and variables

Automatic light sensing arm lamp touch chip-dlt8sa15b-jericho

Definition of option basis

One track education, PHP training, unity of knowledge and practice, popular

持续(集成--&gt;交付--&gt;部署)

How does kotlin help you avoid memory leaks?

Why is crypto game changing the game industry?
随机推荐
docker部署mysql 实现远程连接[通俗易懂]
Protobuf data exchange format
How to design a second kill system?
Bull spread portfolio
Cesium pit -- pit used by various API calls and API itself
Rust 从入门到精通01-简介
屈辱、抗争、逆转,三十年,中国该赢微软一次了
如何配置adb环境变量(环境变量在哪打开)
Definition of option basis
Black Scholes Merton European option pricing formula
Jenkins--持续集成服务器
Leetcode notes 566. Reshaping the matrix
Beyond Istio OSS——Istio服务网格的现状与未来
二舅能治好年轻人的精神内耗吗?
[FPGA]: AD sampling problem
蓝桥集训(附加面试题)第七天
Basic exercises of DQL in MySQL
Chapter 6 promotion
Tidb 6.x in action was released, a summary of 6.x practices that condense the collective wisdom of the community!
8、 Kubernetes network and load balancing