Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Merge sort and quick sort (32)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

In the last section, we learned about bubble sorting and Hill sorting, and in this section we continue to learn about merge sorting and quick sorting.

1. Merge sorting: merge two or more ordered sequences into a new ordered sequence. As follows

Then since there are two ways to merge, there will be multiple ways to merge. Three ordered sequences are merged into a new ordered sequence, which is called 3-way merging; N ordered sequences are merged into a new ordered sequence, which becomes N-path merging; and multiple ordered sequences are merged into a new ordered sequence, which is called multipath merging.

Let's take a look at an example of a two-way merger, as shown in the following figure

Let's see how it is implemented, as shown below

It is compared one by one by comparing the size of the two sequences. Let's take a look at the specific implementation of merge sorting. The specific source code is as follows

# ifndef SORT_H#define SORT_H#include "Object.h" namespace DTLib {class Sort: public Object {private: Sort (); Sort (const Sort&); Sort& operator= (const Sort&); template static void Swap (T & a, T & b) {TC (a); a = b; b = c;} template

< typename T >

Static void Merge (T src [], T helper [], int begin, int mid, int end, bool min2max) {int I = begin; int j = mid + 1; int k = begin; while ((I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report