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 basic and common sorting algorithms

2025-02-25 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 basic algorithms and common sorting algorithms". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the basic algorithms and common sorting algorithms?"

Basic algorithms and 7 common sorting algorithms

Five features of the algorithm

Finite, exact, input, output, feasibility

Time complexity: the computational effort required to execute the algorithm. Generally speaking, the computer algorithm is a function of the problem size n, so the time complexity of the algorithm is recorded as T (n) = O (f (n)).

The larger the scale of the problem, the growth rate of the execution time of the algorithm is positively correlated with the growth rate of f (n), which is called asymptotic time complexity (Asympotic Time Complexity).

Calculation method:

1. Calculation number formula

1 / 2 / 3 / 3 / 1 / 2 / 3 / 3 / 2 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 2 / 3 / 3 / 2 / 3 / 3 / 2 / 3 / 2 / 3 / 3 / 2 / 3 / 3 / 2 / 3 / 3 / 2 / 3 / 3 / 2 / 2 / 3 / 3 / 2 / 3 / 3 / 2 / 2 / 3 / 3 / 2 / 2 / 3 / 3 / 2 / 2 / 3 / 3 / 2 / 2 / 3 / 3 / 2 / 2 / 3 / 3 / 2 / 2 / 3 / 3 / 2 / 2 / 3 / 3 / 3 / 2 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 3 / 2 / 2 / 2 / 3 / 3 /

Calculate n times, the time complexity is O (n)

two。 Replace all addition constants such as O (3) as O (1) with the constant 1.

O (3) as O (1)

3. In the modified operation number function, only the highest order term is retained.

N ^ 2 + n ^ 1 is recorded as O (n ^ 2)

4. If the highest order exists and is not 1, the constant multiplied by this term is removed.

2n ^ 2 + 3n+1 is marked O (n ^ 2)

Constant order: O (1)

Linear order: O (n)

Flat (square) order: O (n ^ 2), O (n ^ 3)

Two-layer loop O (n ^ 2) three-layer O (n ^ 3)

Special square order: O (n ^ 2 / 2+n/2)-> O (n ^ 2)

For () {for () {}} for () {} echo $astatb

N ^ 2 + n ^ 1-> O (n ^ 2)

Worst-case scenario: worst-case run time, a guarantee that, unless otherwise specified, the time complexity is the worst-case time complexity

Average: expected running time Sanmenxia Gynecology Hospital http://www.smxrlyy.com/

Space complexity: the memory space consumed by the algorithm, denoted as S (n) = O (f (n))

Including the space occupied by the program code

The space occupied by the input data and

The space occupied by auxiliary variables

These three aspects

The method of calculation and representation is similar to the time complexity, which is generally expressed by the gradualness of complexity.

-

Sorting algorithm and its time complexity and space complexity

1. Bubbling sort

Function BubbleSort ($arr) {$len = count ($arr); while ($len > 1) {$changed = false; for ($I = 0; $I

< $len - 1; $i++) { if ($arr[$i] >

$arr [$I + 1]) {$tmp = $arr [$I]; $arr [$I] = $arr [$I + 1]; $arr [$I + 1] = $tmp; $changed = true;}} if (! $changed) {return $arr;} $len--;} return $arr;}

The internal cycle will sink the largest to the tail in each round.

Time complexity: O (n ^ 2), average O (n ^ 2)

Space complexity: O (1)

Belong to stable, in-situ sorting algorithm

two。 Select sort

Each time the smallest or largest elements are taken from the sequence to be sorted and placed at the beginning of the sequence until the data elements to be sorted are finished.

Comparison time: t = (nmur1) + (n-2) + (n-3). + 1; = = > > T = [n * (nMel 1)] / 2

Exchange time: in the best case, all the elements are in order, then the number of exchanges is 0, in the worst case, all the elements are in reverse order, it is necessary to exchange nmurs once.

So the optimal time complexity and the worst time complexity and the average time complexity are both O (n ^ 2).

Space complexity: O (1)

3. Quick sort

Select a cardinality, sort the sorted data into two independent parts, one part is smaller than this cardinality, the other part is larger than this cardinality, and then deal with these two parts according to this method.

Time complexity: worst O (n ^ 2), average O (nlogn)

Space complexity: worst O (n), average O (logn)

4. Insert sort

Divide a group of data into two groups, which are called ordered group and group to be inserted respectively.

Take one element from the group to be inserted each time, compare it with the element of the ordered group, find the appropriate position, and insert the element into the ordered group.

In this way, one element at a time is inserted, the ordered group increases, and the group to be inserted decreases.

Until the number of group elements to be inserted is 0, of course, the exchange of elements is involved in the insertion process.

Time complexity: O (n ^ 2), space complexity: O (1)

$arr= (& $arr) {$len=count ($arr); for ($len=count / $tmp0;$gap=intval ($gap/2)) {for ($iota / $arr [$j] _ intval ($gap/2)) {for ($ipolar gapscape intval ($gap/2)) {return; $tmp= []; $ipherpherl; $jungle midday 1; while ($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