当前位置:网站首页>Blue Bridge Cup Quick sort (code completion)
Blue Bridge Cup Quick sort (code completion)
2022-07-04 07:11:00 【Woodenman Du】
requirement : Complete the code to achieve fast scheduling
Need to complete the code :
#include <stdio.h>
void swap(int a[], int i, int j)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
int partition(int a[], int p, int r)
{
int i = p;
int j = r + 1;
int x = a[p];
while(1){
while(i<r && a[++i]<x);
while(a[--j]>x);
if(i>=j) break;
swap(a,i,j);
}
_____________________;
return j;
}
void quicksort(int a[], int p, int r)
{
if(p<r){
int q = partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
int main()
{
int i;
int a[] = {50,49,48,48,47,46,1,3,5,2,4,6,30,30,30,30};
int N = 16;
quicksort(a, 0, N-1);
for(i=0; i<N; i++) printf("%d.", a[i]);
printf("\n");
return 0;
}
answer : swap(a,j,p);
analysis :
First
What is the process of quick platoon ?
1. Two points
2. Set benchmark
3. If it is smaller than the reference point, put it to the left of the reference point , If it is larger than the reference point, put it to the right of the reference point , And this process is realized by constantly finding qualified element exchange
Note that it is placed on both sides of the benchmark , One thing must be done after the exchange of qualified elements : Is the benchmark I selected not necessarily in the center of the eligible element , So we must exchange again , Let the benchmark go where it should be , The answer comes out
Sorting algorithm :https://blog.csdn.net/qq_59700927/article/details/122638158
边栏推荐
- 果果带你写链表,小学生看了都说好
- The final week, I split
- 【FreeRTOS】FreeRTOS学习笔记(7)— 手写FreeRTOS双向链表/源码分析
- Download address of the official website of national economic industry classification gb/t 4754-2017
- 响应式——媒体查询
- leetcode825. 适龄的朋友
- BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
- js 常用时间处理函数
- MySQL 45 lecture learning notes (12) MySQL will "shake" for a while
- MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
猜你喜欢
Solution of running crash caused by node error
BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
Introduction to spark core components
【Kubernetes系列】Kubernetes 上安装 KubeSphere
Vulhub vulnerability recurrence 76_ XXL-JOB
The important role of host reinforcement concept in medical industry
Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown
MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
移动适配:vw/vh
Master-slave replication principle of MySQL database
随机推荐
Mobile adaptation: vw/vh
Paddleocr prompt error: can not import AVX core while this file exists: xxx\paddle\fluid\core_ avx
Vulhub vulnerability recurrence 77_ zabbix
Transition technology from IPv4 to IPv6
win10微软拼音输入法输入文字时候下方不出现中文提示
Analysis of tars source code 1
MySQL 45 lecture learning notes (XIII) delete half of the table data, and the table file size remains the same
2022 is probably the best year for the economy in the next 10 years. Did you graduate in 2022? What is the plan after graduation?
【FPGA教程案例8】基于verilog的分频器设计与实现
tars源码分析之7
tars源码分析之4
Mysql 45讲学习笔记(十二)MySQL会“抖”一下
Summary of MySQL common judgment functions!! Have you used it
MySQL 45 lecture learning notes (VII) line lock
Finishing (III) - Exercise 2
jdbc连接es查询的时候,有遇到下面这种情况的大神嘛?
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
MySQL storage engine
《国民经济行业分类GB/T 4754—2017》官网下载地址
移动适配:vw/vh