当前位置:网站首页>C language: random generated number + merge sort
C language: random generated number + merge sort
2022-07-28 13:36:00 【Nianchi ichthyology programming】
#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("========== The sequence before sorting =============\n");
initArr(arr,MAXSIZE);
showArr(arr,MAXSIZE);
printf("========== Sequence after merging and sorting =============\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);
}
边栏推荐
- Dry goods -- encapsulated anti shake and throttling method in the project
- [FPGA]: Joint Simulation of FPGA and MATLAB
- [FPGA]: ise and Modelsim joint simulation
- Rust 从入门到精通01-简介
- How to design a second kill system?
- 什么是事务及数据库的优化方法
- 《如何打一场数据挖掘赛事》入门版
- [FPGA]: ISE generates MCS file and burning process
- leetcode-136.只出现一次的数字
- I miss the year of "losing" Li Ziqi
猜你喜欢

Jenkins -- continuous integration server

Night God simulator packet capturing wechat applet

Protective bearish strategy

Half wave rectification light LED

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

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

Le transaction

Risk analysis of option trading

Aragon创建DAO polygon BSC测试网

从手机厂高位“出走”的三个男人
随机推荐
Jenkins--持续集成服务器
C语言学生成绩管理系统详解[通俗易懂]
MySQL practice -- master-slave replication
I copied the bottom of the liquidated NFT, but was locked by opensea
How to design a second kill system?
力扣 剑指 Offer 51. 数组中的逆序对
Change password, confirm password verification antd
JS encapsulation at a glance
蓝桥集训(附加面试题)第七天
Single option trading and cross trade
Can second uncle cure young people's spiritual internal friction?
C语言:随机生成数+快速排序
Chapter 6 提升
Aragon creates Dao polygon BSC test network
The form select in antd is received before it is selected
PHP生成随机数(昵称随机生成器)
Blue Bridge Training (additional interview questions) day 7
Bull spread portfolio
Tidb 6.x in action was released, a summary of 6.x practices that condense the collective wisdom of the community!
Compare the new and old data to find the added and deleted ones