当前位置:网站首页>Double recursion in deep analysis merge sort
Double recursion in deep analysis merge sort
2022-06-25 05:18:00 【Halya】
Deep analysis of recursion in merge sort :
This article is the author's reading notes , Please do not reprint without permission . If it helps you, remember to praise it (●’◡’●)
This paper mainly shows the abstract details of double recursion in the form of graph .
Source code is as follows :
main.cpp
// 1 Sort .cpp : Defines the entry point for the console application .
#include "stdafx.h"
void random(vector<int>& vec)// Random number generator
{
random_device r;
for (auto& e : vec)
{
e = r() % vec.size() + 1;
}
}
void out(const vector<int>& vec)// Output function
{
for (const auto& e : vec)
{
cout << e << " ";
}
cout << endl;
}
void sortTest(function<void(vector<int>&)> sortFn)// Test functions
{
vector<int> vec(100);
random(vec);
out(vec);
auto start = clock();// Test time
sortFn(vec);
auto end = clock();
out(vec);
auto diff = (float)(end - start) / 1000;
cout << diff << endl;
}
int _tmain(int argc, _TCHAR* argv[])// The main function , Precompiled headers are used ( Personal habits )
{
sortTest(sort::mergeSort);
system("pause");
return 0;
}
// Results show
//97 76 21 39 77 89 4 87 26 17 59 54 22 7 64 3 38 70 9 45 24 98 89 2 85 60 41 13 26 89 68 31 68 49 92 27 31 76 8 58 84 57 10 87 30 62 68 92 60 9 7 58 19 4 52 23 64 41 60 51 63 39 58 90 79 71 93 6 61 17 44 19 61 91 71 26 51 97 84 19 80 19 64 39 45 27 44 55 90 21 27 96 46 71 91 95 1 84 88 41
//1 2 3 4 4 6 7 7 8 9 9 10 13 17 17 19 19 19 19 21 21 22 23 24 26 26 26 27 27 27 30 31 31 38 39 39 39 41 41 41 44 44 45 45 46 49 51 51 52 54 55 57 58 58 58 59 60 60 60 61 61 62 63 64 64 64 68 68 68 70 71 71 71 76 76 77 79 80 84 84 84 85 87 87 88 89 89 89 90 90 91 91 92 92 93 95 96 97 97 98
//0.001s
sort.h
#pragma once
#include<vector>
using std::vector;
class sort
{
private:
static void merge(vector<int>& vec,int l,int mid,int r);
static void reSolve(vector<int>& vec,int l,int r);
public:
static void mergeSort(vector<int>& vec);
};
sort.cpp
#include "stdafx.h"
#include "sort.h"
#include<algorithm>
using namespace std;
void sort::mergeSort(vector<int>& vec)
{
reSolve(vec, 0, vec.size() - 1);
}
void sort::reSolve(vector<int>& vec,int l,int r)
{
if (l >= r) return;
int mid = (r - l) / 2 + l;// Avoid arithmetic overflow
reSolve(vec, l, mid); // Split the data on the left
reSolve(vec, mid + 1, r); // Split the data on the right
merge(vec, l, mid, r); // Merge
}
void sort::merge(vector<int>& vec,int l,int mid,int r)
{
// Create a temporary vector
vector<int> temp(vec.begin() + l, vec.begin() + r + 1); // Pay attention to distinguish between L and 1
int lk = l; // The starting subscript of the data on the left
int rk = mid + 1;// The starting subscript of the data on the right
for (size_t i = l; i <= r; i++)// Here is the core of the code , Reference notes are easy to understand .
{
if (lk>mid)// When the left has been sorted , Directly add the data on the right
{
vec[i] = temp[rk - l];
rk++;
}
else if (rk>r)// When the right has been sorted , Directly add the data on the right
{
vec[i] = temp[lk - l];
lk++;
}
else if (temp[lk-l]<temp[rk-l])// Compare copy Over here vec, Compare the data size of the left and right sides ; Here the left is greater than the right .
{
vec[i] = temp[lk - l];
lk++;
}
else
{
vec[i] = temp[rk - l];
rk++;
}
}
}
Here is resolve Detailed explanation of function
It's using 5 Examples of data , More data and so on
in general , Understand a path to the end and come back to almost understand recursion ( Big brother's original words )
边栏推荐
- 2021-04-02
- TeeChart Pro ActiveX 2022.1
- Read the general components of antd source code
- Ranorex Studio 10.1 Crack
- Try block and exception handling
- Visual studio 2022 interface beautification tutorial
- What is Ethernet and how to connect the computer
- How to make colleagues under the same LAN connect to their own MySQL database
- Fun CMD command line~
- Japanese fifty tone diagram
猜你喜欢
The article is on the list. Welcome to learn
H5 native player [learn video]
Everything is an object
渗透测试-目录遍历漏洞
Introduction to the hardest core PWN in the whole network_ Graphic analysis
Notes on non replacement elements in the line (padding, margin, and border)
Small sample learning data set
Prototypical Networks for Few-shot Learning
Reading and writing of nodejs excel (.Xlsx) files
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
随机推荐
Working principle of asemi three-phase rectifier bridge
Flex flexible layout for mobile terminal page production
Creation and use of MySQL index
February 19 CTF exercise
Handwritten promise all
What is Ethernet and how to connect the computer
电子协会 C语言 1级 28 、字符菱形
A brief talk on media inquiry
In depth understanding of line height and vertical align
Small sample learning data set
Ranorex Studio 10.1 Crack
Teach you to write non maintainable PHP code step by step
File upload vulnerability (III)
There is 404 in the laravel visit, except the home page is redirected; Index php
CUDA compilation error
Array: force deduction dichotomy
How to open the DWG file of the computer
Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
CTFHUB SSRF
[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear