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

What is the principle of the three-way quick sorting algorithm implemented by C _

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you what the principle of the three-way quick sorting algorithm is, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Following the book above, we talked about two-way quick sorting last time, which is to exchange numbers equal to v (the number of marks), thus avoiding the imbalance when dealing with array packets with a lot of duplicate data. On the other hand, the three-way quick sorting divides the numbers equal to v into a group, which can also solve the above problems. The principle is as follows:

1. The method of random sorting is used to put a number as a partition number at the beginning of the array, which is defined as v. The index at the beginning of an array less than v is defined as lt, the index of the array to be traversed is defined as I, the index of the array less than v is defined as gt, and the indexes at the beginning and end of the array are l and r, respectively. The schematic diagram is as follows:

2. Maintain the index I and compare the relationship between the number of index I and v one by one. If arr [I] v, swap arr [I] with ARR [GT-1], and then move the index gt forward one bit, but note that index I does not need to be moved at this time, because the number of swaps does not know the size, and the next step needs to be judged.

5. According to this process, gt coincides with I, and the comparison process ends.

6. Finally, don't forget to swap positions between ARR [l] and ARR [lt]. Note: the exchange here is arr [lt]. Sorting complete

The code is as follows:

# ifndef QUICKSORT3_H#define QUICKSORT3_H// three-way quick sorting algorithm # include # include using namespace std;template void _ _ QuickSort3way (T * arr,int lpoint int r) {/ / Recursive termination condition if (l > = r) {return } else {/ / Random quick sorting selected random values, in order to avoid grouping imbalance in the overall order, the algorithm degenerates int RAND = (rand ()% (r-l) + l); swap (arr [l], arr [RAND]); T v = arr [l]; / / initial index int lt = l for packets less than v; / / initial index int gt = r + 1 for packets greater than v / / the initial index of the group equal to v, and this index is the index of the array traversal value int I = l + 1; 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

Development

Wechat

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

12
Report