当前位置:网站首页>C语言:随机生成数+快速排序
C语言:随机生成数+快速排序
2022-07-28 12:18:00 【念迟鱼学编程】
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 10
void initArr(int arr[] , int length);
void showArr(int arr[] , int length);
void quickSort(int arr[] , int left , int right);
int main()
{
srand((unsigned int)time(NULL));
int arr[MAXSIZE];
printf("==========排序前的序列=============\n");
initArr(arr,MAXSIZE);
showArr(arr,MAXSIZE);
printf("==========快速排序后的序列=============\n");
quickSort(arr,0,MAXSIZE-1);
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 quickSort(int arr[] , int left , int right)
{
//快速排序
if(left>right){
return;
}
int i = left;
int j = right;
int pivot = arr[i];
while(i<j){
while(i<j && arr[j] >= pivot){
j--;
}
arr[i] = arr[j];
while(i<j && arr[i] <= pivot){
i++;
}
arr[j] = arr[i];
}
arr[i] = pivot;
quickSort(arr,left,i-1);
quickSort(arr,i+1,right);
}
边栏推荐
猜你喜欢

leetcode-190.颠倒二进制位

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

Change password, confirm password verification antd

Using auto.js to realize fifaol3 brush teaching assistant

今日睡眠质量记录75分

Jar package

Auto.js enables Taobao to quickly submit orders

Chapter 6 提升

How does kotlin help you avoid memory leaks?

Guide for using IP phone system and VoIP system
随机推荐
Aragon创建DAO polygon BSC测试网
【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...
JS encapsulation at a glance
[embedded C foundation] Part 7: detailed introduction to C language process control
基于深度学习的超分辨率重建
Single option trading and cross trade
Jar package
PCP parity principle arbitrage
vim常用命令详解(vim使用教程)
Night God simulator packet capturing wechat applet
Databinding+livedata can easily realize skin changing without restart
[ecmascript6] function and its related use
[FPGA] joint simulation of vivado and Modelsim
蓝桥集训(附加面试题)第七天
Complete set of SSM framework online bookstore
Leetcode 笔记 118. 杨辉三角
Automatic light sensing arm lamp touch chip-dlt8sa15b-jericho
【ECMAScript6】Promise
Rust from introduction to mastery 01 introduction
Stepless dimming colorful RGB mirror light touch chip-dlt8s12a-jericho