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 are the classical sorting algorithms commonly used in non-numerical computation in C language

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

Share

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

This article mainly explains "what are the common classical sorting algorithms for non-numerical calculation in C language". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what are the common classical sorting algorithms for non-numerical calculation in C language" together.

sort

Sorting is a computer operation method, its purpose is to adjust a group of "disordered" record sequence to "ordered" record sequence, mainly divided into internal sorting and external sorting.

(1) Bubble sort (bubble sort)

Bubble Sort (Bubble Sort), the basic idea is that for a group of elements to be sorted, compare the adjacent two numbers in turn, put the smaller number in front, and the larger number in the back, and so on, until the last two numbers are compared, put the decimal in front, and the large number in the back, repeat the steps until all sorting is completed.

Advantages: Stability;

Disadvantages: Slow, only two adjacent data can be moved at a time.

Suppose you want to sort a sequence containing n numbers in ascending order, the bubble sort algorithm steps are:

① From the first element to the last element in the array storing the sequence, compare the adjacent two numbers in turn, and if the former is larger than the latter, exchange the positions of the two numbers;

② After the end of the first round, the maximum number is stored in the last element of the array, and then from the first element to the penultimate element, the adjacent two numbers are compared in turn. If the former is larger than the latter, the position of the two numbers is exchanged.

Repeat step 1 n-1 times, each time less than the previous one, you can complete the request.

1. Read in 10 integers arbitrarily and output them in ascending order by bubbling method.

Traditional methods:

(2) Selection method sorting

The selection sorting method is to select the record with the smallest keyword from n-i +1 (i=1, 2,…n-1) records as the ith record in the ordered sequence for each pass. The algorithms based on this idea mainly include simple selection sort, tree selection sort and heap sort.

Advantages: The number of times to move data is known (n-1 times);

Disadvantages: More times of comparison, instability.

Selection sorting is a relatively well-understood sorting algorithm. Assuming that we want to sort a sequence of n numbers in ascending order, the algorithm steps are:

① Find the subscript of the minimum number from the n numbers stored in the array (see "Find the minimum value" below for the algorithm), and then exchange the position of the minimum number with the first number;

(2) In addition to the first number, find the subscript of the smallest number (i.e. the sub-decimal number in n) from the rest n-1 numbers, and exchange the position of this number with the second number;

Repeat step 1 n-1 to complete the request.

1. Read in 10 integers arbitrarily and output them in ascending order by selection method.

Traditional methods:

Function method:

(3) Insertion sort

Advantages: stable, fast;

Disadvantages: The number of comparisons is not certain, the more the number of comparisons, the more the data moves after the insertion point, especially when the total amount of data is huge, but the linked list can solve this problem.

To get a good grasp of this algorithm, first understand the "insertion algorithm for ordered sequences," which is to insert some data into an ordered sequence and the sequence remains ordered. For the insertion algorithm, see "Insertion of Array Elements" below.

After inserting any integer x into an ascending sequence, the sequence is still arranged in ascending order.

The method of insertion sort is to insert a number into the final array immediately after reading it, and each insertion makes the array ordered.

2. Read in 10 integers arbitrarily and output them in descending order by interpolation.

(4) Merging sort

MERGE-SORT is an efficient sort algorithm based on merge operation, which is a typical application of Divide and Conquer. The ordered subsequence is merged to obtain a completely ordered sequence, that is, each subsequence is ordered first, and then the subsequence segments are ordered. If two ordered tables are merged into one ordered table, it is called two-way merging.

That is, two data sequences that are both in ascending (or descending) order are merged into one sequence that is still in the original order.

There is an ascending sequence of 6 data and an ascending sequence of 4 data, and the two are combined into an ascending sequence of 10 data.

(5) Character array: (reverse order) such as:

1, the input string in reverse order, such as input ABCDE, output EDCBA

Thank you for reading, the above is "C language non-numerical calculation of the common classical sorting algorithm what" content, after the study of this article, I believe that we have a more profound understanding of the C language non-numerical calculation of the common classical sorting algorithm, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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