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

How to realize recursive and non-recursive sorting in c language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to realize recursive and non-recursive sorting in c language". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "c language recursion and non-recursive sorting how to achieve" it!

Recursive code flow

Merging is the merging of two or more sequences, which only introduces two-way merging, that is, constantly dividing the sequences into two groups until each group has an element, and then comparing and merging until it is combined into one sequence.

Non-recursive code flow

In contrast to recursively decomposing arrays, non-recursion merges directly from a subsequence of length 1 to a full sequence, reusing the merge function.

Comparison between the two

The code is more efficient in a non-recursive way:

Space complexity: from O (log2n) to a temporary array O (n)

Time complexity: less recursive time

Time complexity

O (nlogn)

Code (recursive) # include # include # define MAXSIZE 9typedef struct {int r [MAXSIZE+1]; / / first index used as tmp, not real data int len;} SqList;void swap (SqList * L, int I, int j) {int tmp = L-> r [I]; L-> r [I] = L-> r [j]; L-> r [j] = tmp } void merge (int sr [], int tr [], int s, int m, int t) {/ / the task of this function is to compare the size of the elements of the two groups (s. M, m) in sr and merge them into tr int jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj / / the cursors used in the tr array correspond to the starting position in the sr while (slen); / / because the first parameter sr array in msort is just read, there is no problem passing here} int main (void) {SqList list = {{99950pr.

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