当前位置:网站首页>C语言:随机生成数+归并排序
C语言:随机生成数+归并排序
2022-07-28 12:18:00 【念迟鱼学编程】
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#define MAXSIZE 10
void initArr(int arr[] , int length);
void showArr(int arr[] , int length);
void merge(int arr[],int low,int mid,int height,int *temp);
void merge_sort(int arr[],int low,int height,int *temp);
void mergeSort(int arr[],int length);
int main()
{
srand((unsigned int)time(NULL));
int arr[MAXSIZE];
printf("==========排序前的序列=============\n");
initArr(arr,MAXSIZE);
showArr(arr,MAXSIZE);
printf("==========归并排序后的序列=============\n");
mergeSort(arr,MAXSIZE);
showArr(arr,MAXSIZE);
system("pause");
return 0;
}
void initArr(int arr[] , int length)
{
for(int i = 0 ; i < length ; i++){
arr[i] = rand()%20;
}
}
void showArr(int arr[] , int length)
{
for(int i = 0 ; i < length ; i++){
printf("%4d",arr[i]);
}
printf("\n");
}
void merge(int arr[],int low,int mid,int height,int *temp)
{
int i = low;
int j = mid+1;
int k = low;
while(i <= mid && j <= height){
temp[k++] = arr[i]<arr[j]?arr[i++]:arr[j++];
}
while(i <= mid){
temp[k++] = arr[i++];
}
while(j <= height){
temp[k++] = arr[j++];
}
for(i = low ; i <= height ; i++){
arr[i] = temp[i];
}
}
void merge_sort(int arr[],int low,int height,int *temp)
{
if(low>=height){
return;
}
int mid = low + (height - low)/2;
merge_sort(arr,low,mid,temp);
merge_sort(arr,mid+1,height,temp);
merge(arr,low,mid,height,temp);
}
void mergeSort(int arr[],int length)
{
int *temp = (int *)malloc(sizeof(int)*length);
assert(temp);
merge_sort(arr,0,length-1,temp);
free(temp);
}
边栏推荐
- Map tiles: detailed explanation of vector tiles and grid tiles
- paddleClas分类实践记录
- Gamestop bear market entered NFT trading, and established game retailers took advantage of Web3 to make a second spring
- GO语言-栈的应用-表达式求值
- leetcode-136.只出现一次的数字
- Protobuf data exchange format
- Original juice multifunctional Juicer touch chip-dlt8t02s-jericho
- Bear market spread portfolio
- Unity—“合成大西瓜”小游戏笔记
- Mysql中DQL基本练习
猜你喜欢

gicv3 spi register

Have you seen the management area decoupling architecture? Can help customers solve big problems

为什么说Crypto游戏正在改变游戏产业?
![[embedded C foundation] Part 5: original code / inverse code / complement code](/img/85/16d5e9236e13870197d12386e3ab15.png)
[embedded C foundation] Part 5: original code / inverse code / complement code

.NET桌面开发的一些思考

我抄底了被清算的NFT,却被OpenSea上了锁

Complete set of SSM framework online bookstore

butterfly spreads

微念“失去”李子柒的这一年

《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库
随机推荐
夜神模拟器抓包微信小程序
我抄底了被清算的NFT,却被OpenSea上了锁
《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库
[FPGA] joint simulation of vivado and Modelsim
沾上趣店,都得道歉?
[FPGA]: ISE generates MCS file and burning process
Cesium pit -- pit used by various API calls and API itself
Is jetpack compose completely out of view?
Chapter 6 promotion
With 433 remote control UV lamp touch chip-dlt8sa20a-jericho
Automatic light sensing arm lamp touch chip-dlt8sa15b-jericho
Stepless dimming colorful RGB mirror light touch chip-dlt8s12a-jericho
10、 Kubernetes scheduling principle
Leetcode notes 566. Reshaping the matrix
Unity - "synthetic watermelon" small game notes
Risk analysis of option trading
基于深度学习的超分辨率重建
Leetcode-136. numbers that appear only once
UV germicidal lamp chip dlt8p65sa Jericho
[apue] 文件中的空洞