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 sort and summarize the data structure and algorithm of C language

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

Share

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

This article will explain in detail how to sort and summarize the data structures and algorithms in C language. the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

I. Preface

Learning objectives:

Sorting and searching are inseparable, and after the data to be processed are arranged in order according to the size of the key values, the search is faster and more accurate.

Understand the definition and characteristics of various sorting algorithms, and be able to use the code flexibly

Master the time complexity and space complexity of various sorting methods

Understand the concepts of ordering stability and instability

Key points and difficulties: Hill, fast, heap, merge sort

Second, basic concepts 1. Sort

Definition: the process of rearranging an arbitrary sequence of unordered data elements into an ordered one.

Code:

Typedef struct {int key; / / assume that the keyword is int OtherType other_data;} RecordType;2. Stability of sorting method 0123456789541011228107612

Interpretation: an unordered array such as the previous table wants to sort it from small to large. The corresponding numbers of subscript 2 and 6 above are 10. After sorting, if the red 10 is still before the black 10, then the sorting method is stable, otherwise the sorting method is unstable.

3. Internal and external sorting

Internal sorting: the entire sorting process is in memory

External sorting: the number that needs to be sorted is too large, and external devices are needed.

Third, insert class sort

Insert class: insert a new record into an ordered sequence so that it is still in order

1. Directly insert sort

Dynamic demo:

Algorithm explanation:

The dynamic diagram above can well express the process of direct insertion, but the dynamic diagram is a little long.

First of all, use 0 as an outpost, use a pointer to find the number behind it that is smaller than the previous number, and find it and put it to 0.

The pointer begins to move forward, and if the value pointed to is larger than the value in the guard post, the number moves backward.

If the value pointed to is less than the value in the guard post, then store the value in the watch post after this element

and so on

Code:

Void InsSort (RecordType r [], int length) / * directly inserts the array r of records. Length is the number of records to be sorted in the array * / {int iMagazine j; for (iMag2; ihigh, low is followed by the position of k insertion.

Code:

Void BinSort (RecordType r [], int length) / * sorts the record array r by half, where length is the length of the array * / {int iJournal j; RecordType x; int low,high,mid; for (iDiver2; 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