当前位置:网站首页>Quick sort & merge sort
Quick sort & merge sort
2022-06-30 03:55:00 【Cod_ ing】
Quick sort
#include<iostream>
#define MAXSIZE 10005
using namespace std;
int a[MAXSIZE];
// Partition operation
int paritition(int a[],int low,int high){
int pivot=a[low];
while(low<high){
// From right to left pivot Small value
while(low<high&&a[high]>=pivot) --high;
a[low]=a[high];
// From left to right pivot Big value
while(low<high&&a[low]<=pivot) ++low;
a[high]=a[low];
}
// Reference value partition
a[low]=pivot;
// return pivot The location of , As a boundary
return low;
}
void Quicksort(int a[],int low,int high){
if(low<high){
int pivot=paritition(a,low,high);
Quicksort(a,low,pivot-1);
Quicksort(a,pivot+1,high);
}
}
int main(){
int N,i;
cin>>N;
for(i=0;i<N;i++)
cin>>a[i];
Quicksort(a,0,N-1);
//for(i=0;i<N;i++)
cout<<a[i/2]<<endl;
}
Merge sort
#include<iostream>
#define MAXSIZE 100
using namespace std;
/* Will array a Two consecutive ordered sequences of : The first s To the first m Elements , The first m+1 To the first t Elements , Merge to produce a s To the first t An ordered sequence of elements : */
void Merge(int a[],int s,int m,int t){
int temp[MAXSIZE];
// i、j The starting position of two consecutive ordered sequences
int i=s;
int j=m+1;
// k Temporary array temp The subscript
int k=i;
// Take the smallest one from the two ordered sequences and put it into the temporary array
while(i<=m&&j<=t)
if(a[i]<=a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
// The rest is put into the temporary array in turn , In fact, only one of them will be executed while
while(i<=m)
temp[k++]=a[i++];
while(j<=t)
temp[k++]=a[j++];
// Copy the contents of the temporary array back to the original array
for(i=s;i<=t;i++)
a[i]=temp[i];
}
/* decompose : hold n A sequence of elements is decomposed into a sequence with a length of 1 An ordered subsequence of */
void Mergesort(int a[],int s,int t){
// Split into length 1 The ordered sub table of
if(s==t) return;
int m=(t+s)/2;
// Recursion on the left sequence
Mergesort(a,s,m);
// Recursion of the sequence on the right
Mergesort(a,m+1,t);
// Merge
Merge(a,s,m,t);
}
int main(){
int N,a[MAXSIZE];
cin>>N;
for(int i=0;i<N;i++)
cin>>a[i];
Mergesort(a,0,N-1);
for(int i=0;i<N;i++)
cout<<a[i]<<endl;
}
边栏推荐
- [punch in - Blue Bridge Cup] day 3 --- slice in reverse order list[: -1]
- Half a year after joining the company, I was promoted to a management post
- 【作业】2022.5.23 MySQL入门
- 【个人总结】学习计划
- Wang Shuang - assembly language learning summary
- (04).NET MAUI实战 MVVM
- 学校实训要做一个注册页面,要打开数据库把注册页面输入的内容存进数据库但是
- 利用反射整合ViewBinding和ViewHolder
- 尝试链接数据库时出现链接超时报错,如何解决?
- Node-RED系列(二八):基于OPC UA节点与西门子PLC进行通讯
猜你喜欢

Mysql性能优化(6):读写分离

laravel9本地安裝

Interface test tool postman

在大厂外包呆了三年,颠覆了我的认知!

DO280私有仓库持久存储与章节实验

Litjson parses the generated JSON file and reads the dictionary in the JSON file

Chapter 2 control structure and function (programming problem)
![[punch in - Blue Bridge Cup] day 3 --- slice in reverse order list[: -1]](/img/c2/13693dcb51aab565957b6c5e686b7c.jpg)
[punch in - Blue Bridge Cup] day 3 --- slice in reverse order list[: -1]

【图像融合】基于交叉双边滤波器和加权平均实现多焦点和多光谱图像融合附matlab代码

Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
随机推荐
Wang Shuang - assembly language learning summary
云原生——Web实时通信技术之Websocket
Introduction to cloud native + container concept
【作业】2022.5.23 MySQL入门
124 articles in total! Motianlun "high availability architecture" dry goods document sharing (including Oracle, mysql, PG)
各位大佬,flink 1.13.6,mysql-cdc2.2.0,抽取上来的datetime(6)类
matplotlib. pyplot. Hist parameter introduction
Solve the problem of Navicat connecting to the database
[note] Introduction to data analysis on June 7, 2022
【笔记】2022.5.28 从网页获取数据并写入数据库
Feign 坑
Linear interpolation of spectral response function
dotnet-exec 0.5.0 released
Grasp grpc communication framework in simple terms
SDS understanding in redis
[operation] getting started with MySQL on May 23, 2022
毕业设计EMS办公管理系统(B/S结构)+J2EE+SQLserver8.0
第十二天 进阶编程技术
dbt产品初体验
【笔记】2022.5.27 通过pycharm操作MySQL