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

A case study of sorting algorithm in C language

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

Share

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

In this article, the editor introduces in detail the "case study of c language sorting algorithm". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "case study of c language sorting algorithm" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

In the merging algorithm, merging two sequences needs to consume the space of mbinn.After sorting, the queue will be copied back. In my version of the algorithm, you can apply for a piece of equal memory at the beginning, and then write the data sequentially without having to write back. After writing once, swap two pieces of memory and continue to write. This reduces the writing overhead by half and avoids the problem of repeated application space.

The number of swaps determines which block of memory the correct data is written to when the sort is complete.

When the number of swaps is even, the original memory is written.

When the number of swaps is odd, temporary memory is written.

If the number of swaps is odd, then copy the data to the original memory. However, in pairwise swapping, no additional memory is needed, because there is only one comparison data, a simple exchange is fine, an odd comparison is made first, and then it is still an even number of times. So there is this code in front of the main part of the algorithm.

Int temp = len,i = 0temp size = 1; int l1pGrayslend L2pLJ L2; int * templist,*listb; printarr (list,len); if (len > 1; item1)} if (I% 2) {for (item0; (item1) list [item1]) {temp = list [I]; list [I] = list [item1]; list [ionom1] = temp;} size = 2 Printarr (list,len);}

Although this code completes the task, there is something particularly uncomfortable about it. It is where the number of exchanges is calculated that an O (n) cycle is used to get the number of times. It feels a little inefficient.

In fact, we only care about where the number of bits of length is, because that one decides the number of exchanges.

The reason for this idea is that in the world of in-place operations, some algorithms are fascinating.

such as

X & (x Mel 1)

The continuous 0 from the rightmost side of X can be filled as 1.

X & (- x)

The rightmost 1 can be extracted separately.

Wait.

So I always feel that our problems can also be solved with O (1).

But these simple and useful magic seems to only touch the right side of the number, because to manipulate the number on the right, you can use the exact number you know, such as 1 or FFFFFF, while you can't operate the left bit like the right bit, unless you know what the log base 2 interger of that number is. If we have a way to fill in the leftmost zero, we can solve it, but this seemingly simple problem is still an unsolved problem, unless the new version of cpu has instructions to find a specific problem, it seems impossible to solve it in steps below logN.

However, for our application, this is not the end, because the ultimate goal is to know whether the bit is in odd or even digits. So the question turns to:

If the length of the array is-1, if the leftmost 1 falls on the even bit, it needs an even number of swaps, and if it falls on the odd bit, it needs an odd number of swaps.

In this way, the problem seems to be easily solved. This is the rewritten code.

Int temp = len-1,i = 0primesize = 1

Int l1p,l1end,l2p,l2end

Int * templist,*listb

Printarr (list,len)

If (len

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