How to realize recursive and non-recursive sorting in c language
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.