当前位置:网站首页>Ideas and examples of divide and conquer
Ideas and examples of divide and conquer
2022-06-25 01:16:00 【Lutrra】
Divide and conquer method
Quick sort
decompose a[s…t] Decompose into a[s…i-1] and a[i+1…t], among i Is the basis of division
Solving subproblems : If the length of the subsequence is 0 Or for 1, Then he is orderly , Go straight back to ; Otherwise, each subproblem is solved recursively .
Merge : Because the whole sequence is stored in an array a in , The sorting process is in place , The merge step does not require any action .
example :
int Partition(int a[],int s,int t){ // Partition algorithm int i=s,j=t; int tmp=a[s]; // Use the first record of the sequence as the benchmark while(i!=j){ // Scan alternately from both ends of the sequence to the middle , until i=j until while(j>i&&a[j]>=tmp) j--; // Scan from right to left for keywords less than tmp Of a[j] a[i]=a[j]; // take a[j] Migrate to a[i] Location while(i<j&&a[i]<=tmp) i++; // From left to right , The first keyword found is greater than tmp Of a[i] a[j]=a[i]; // take a[i] Move back to a[j] The location of } a[i]=tmp; return i; } /*{2,5,1,7,10,6,9,4,3,8} With 2 Cardinal number Two pointers : Left sub needle left, Right sub needle right Search from right to left for numbers less than cardinality , Search from left to right for the number whose prime is greater than the benchmark a[i]=a[j]; // take a[j] Migrate to a[i] Location a[j]=a[i]; // take a[i] Move back to a[j] The location of 1<2 i=0,j=2 1,5,1,7,10,6,9,4,3,8 And then from left,5>2,j=3,i=1 1,5,5,7,10,6,9,4,3,8 Loop search i=1,j=3 There is no less than cardinal number in the rear 2 The number of will i=1 Set as 2 1,2,5,7,10,6,9,4,3,8 perform QuikSort(a,s,i-1) i The left side of the does not change i To the right of 5 Benchmarking perform QuickSort(a,i+1,t);// */ void QuikSort(int a[],int s,int t){ if(s<t){ int i=Partition(a,s,t); QuikSort(a,s,i-1);// Recursively sort the left subsequence QuickSort(a,i+1,t);// Recursive sorting of right subsequences } }Seek before k individual
void QuickSort(T a[],int s,int t,int k){ int i; if(s<t){ i=Partition(a,s,t); if(k<i+1) QuickSort(a,s,i-1,k); else if(k>i+1) QuickSort(A,i+1,k) } } T solve(T a[],int n,int k){ QuickSort(a,0,n-1,k); for(int i=0;i<k;i++) printf("%d",a[i]); printf("\n"); }
Merge sort
take a[0…n-1] as n A length of 1 The order sheet , Will be adjacent to k(k>=2) An ordered list is merged in pairs , obtain k/n A length of k The ordered sub table of ; Then we continue to merge the western Zhejiang ordered subsequences , obtain n/k Ordered sub table of the square of , It goes on and on , Finally, we get a length of n The order sheet ,k by 2 It is a two-way merge sort
example :
2,5,1,7,10,6,9,4,3,8 2,5 1,7 10,6 9,4 3,8 1,2,5,7 4,6,9,10 3,8 1,2,4,5,6,7,9,10 3,8 1,2,3,4,5,6,7,8,9,10The divide and conquer strategy is as follows :
- loop log2 n,length Take... In turn 1、2、4、.... Each time, execute the following
- decompose : Decompose the original sequence into length Some subsequences of length
- Merge : Call two adjacent subsequences Merge Algorithm sorting produces an ordered subsequence
Algorithm design :
- Bottom up
void Merge(int a[],int low ,int mid ,int high) { int *tmpa; int i=low,j=mid+1,k=0; tmpa=(int *)malloc((hight-low+1)*sizeof(int)); while(i<=mid&&j<=high) { if(a[i]<=a[j])// Put the elements in the first child table into tmpa { tmpa[k]=a[i]; i++; k++; } else { tmpa[k]=a[j];// Put the elements in the second sub table into tmpa j++; k++; } } while(i<=mid)// Copy the rest of the first sub table to tmpa { tmpa[k]=a[i];i++;k++} while(j<=high)// Copy the rest of the second sub table to tmpa { tmpa[k]=a[j];j++;k++} for(k=0;i=low;i<=high;k++,i++)// take tmpa Copy back a in a[i]=tmpa[k]; free(tmpa);// Release tmpa Occupied memory space } void MergePass(int a[],int length,int n) { int i; for(int i=0;i+2*length-1<n;i=i+2*length)// Merger length Long two-phase adjacent sub table Merge(a,i,i+length-1,i+2*length-1); if(i+length-1<n) // The remaining two sub tables , The latter is less than length Merge(a,i,i+length-1,n-1);// Merge these two sub tables } void MergeSort(int a[],int n) { int length; for(length=1;length<n;length=2*lenth) MergePass(a,length,n); }- The top-down
/*2,5,1,7,10,6,9,4,3,8 2,5,1,7,10 6,9,4,3,8 2,5,1 7,10 6,9,4 3,8 2,5 1 7,10 6,9 4 3 8 2 5 7 10 6 9 2,5 6,9 1,2,5 4,6,9 1,2,5,7,10 3,4,6,8,9 1,2,3,4,5,6,7,8,9 */ void MergeSort(int a[],int low,int high) { int mid; if(low<high)// A subsequence has two or more elements { mid=(low+high)/2;// Take the middle position MergeSort(a,low,mid);// Yes a[low..mid] Subsequence sorting MergeSort(a,mid+1,high);// Yes a[mid+1...high] Subsequence sorting Merge(a,low,mid,high);// Merge two subsequences , See the previous algorithm } }Find the maximum and the second largest
decompose : Press the middle position mid=(low+high)/2 Divided into a[low…mid] and a[mid+1…high] Left and right intervals ( Note that the left section contains a[mid])
Solve the decomposition subproblem : Find the maximum value of the left interval lmax1 And the second largest value lmax2, Find the maximum value of the right interval rmax1 And the second largest value rmax2
Merge : if lmax1>rmax1, be max1=lmax1,max2=MAX{lmax2,rmax1}m otherwise max1=rmax1,max2={
lmax1,rmax2}
#include<algorithm> #include<iostream> #include<stdio.h> using namespace std; #define INF 600000; // stay vs6 in max want _cpp_max void solve(int a[],int low,int high,int &max1,int &max2) { if(low==high) // There is only one element in the interval { max1=a[low]; max2=-INF; } else if(low==high-1) // The interval has only two elements { max1=_cpp_max(a[low],a[high]); max2=_cpp_min(a[low],a[high]); } else { int mid=(low+high)/2;// An interval has more than two elements int lmax1,lmax2; solve(a,low,mid,lmax1,lmax2);// Left interval lmax1 and lmax2 int rmax1,rmax2; solve(a,mid+1,high,rmax1,rmax2);// Find the right interval rmax1 and rmax2 if(lmax1>rmax1) { max1=lmax1; max2=_cpp_max(lmax2,rmax1);//lmax2,rmax1 Find the next largest element in } else { max1=rmax1; max2=_cpp_max(lmax1,rmax2); //lmax1、rmax Find the next largest element in } } }Binary search


边栏推荐
- Bi-sql - join
- Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
- Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
- Yasea APK Download Image
- Cobalt strike installation tutorial
- Convolution and transpose convolution
- 期望与方差
- Scala trait construction mechanism
- Picture rotation move zoom gradient
- 对技术的乐观,正让戴尔取得比想象中更多的成就
猜你喜欢

51 single chip microcomputer multi computer communication

I brush the question I - copy the linked list with random pointer

If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?

2022 melting welding and thermal cutting recurrent training question bank simulated examination platform operation

Deep learning LSTM model for stock analysis and prediction

Bi SQL alias

程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?

Preliminary understanding of qtoolbutton

汇编语言(2)基础知识-debug

使用 Loki 微服务模式部署生产集群
随机推荐
JS Chapter 1 Summary
Bi-sql - join
Tencent cloud wecity solution
[untitled]
纹理增强
4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
EVM Brief
The optimism about technology is making Dell achieve more than expected
Text editor for QT project practice - Episode 10
C语言边界计算和不对称边界
程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?
天书夜读笔记——反汇编引擎xde32
Preliminary understanding of qtoolbutton
Leetcode 1248. Statistics of "graceful subarray" (harm, suddenly found that it can only enumerate violently)
利用 Redis 的 sorted set 做每周热评的功能
中金财富证券开户佣金多少呢?股票开户交易安全靠谱吗?
Lenovo tongfuyao: 11 times the general trend, we attacked the city and pulled out the stronghold all the way
I brush the question I - copy the linked list with random pointer
mysql查询时间戳转换成日期格式
戴尔为何一直拒绝将商用本的超薄推向极致?