Java programming: sorting algorithm

Introduction to sorting algorithm

Sorting is also called Sort Algorithm. Sorting is the process of arranging a set of data in a specified order.

Sorted categories

  1. Internal sorting:
    refers to loading all the data to be processed into the internal memory for sorting.
  2. External sorting method: The
    amount of data is too large to load all of it into the memory, and it needs to be sorted with the help of external storage.
  3. Common sorting algorithm classification
    Insert picture description here

Algorithm time complexity

Two methods of measuring the execution time of a program (algorithm)


  1. This method of post-statistics is feasible, but there are two problems: one is to evaluate the performance of the designed algorithm, you need to actually run the program; the other is that the time statistics depend on the computer's hardware, software, etc. Environmental factors, in this way, you must run on the same computer in the same state to compare that algorithm faster.

  2. The method of pre-estimation
    judges which algorithm is better by analyzing the time complexity of an algorithm.

Time frequency

basic introduction

Time frequency: The time spent by an algorithm is directly proportional to the number of executions of the statement in the algorithm. The more the number of statement executions in the algorithm, the more time it takes. The number of executions of statements in an algorithm is called statement frequency or time frequency. Denoted as T(n).

for example

For example, to calculate the sum of all numbers from 1 to 100, we design two algorithms:
![Insert picture description here](https://img-blog.csdnimg.cn/20200924084621999.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4VFF_center,ckcolor#FF_tick_color#70,

Features

  • Ignore constant terms
    Insert picture description here
  • Ignore low-order terms
    Insert picture description here
  • Ignore coefficient
    Insert picture description here

Time complexity definition

  1. In general, the number of repeated executions of the basic operation statement in the algorithm is a function of the problem scale n, represented by T(n), if there is a certain auxiliary function f(n), so that when n approaches infinity, The limit value of T(n) / f(n) is a constant that is not equal to zero, so f(n) is a function of the same order of magnitude of T(n). Denoted as T(n)=O(f(n)), call O(f(n)) as the progressive time complexity of the algorithm, referred to as time complexity.
  2. T(n) is different, but the time complexity may be the same. For example, T(n)=n²+7n+6 and T(n)=3n²+2n+2 have different T(n), but the time complexity is the same, both are O(n²).
  3. The method of calculating the time complexity:
    1) Replace all the additive constants in the running time with a constant 1 T(n)=n²+7n+6 => T(n)=n²+7n+1
    2) The modified running frequency function , Only keep the highest order term T(n)=n²+7n+1 => T(n) = n²
    3) Remove the coefficient of the highest order term T(n) = n² => T(n) = n² => O (n²)

Common time complexity

  • Constant order O(1)
  • Logarithmic order O(log2n)
  • Linear order O(n)
  • Linear logarithmic order O(nlog2n)
  • Square order O(n^2)
  • Cubic order O(n^3)
  • k-th order O(n^k)
  • Exponential order O(2^n)

Description:

The time complexity of common algorithms in descending order is: Ο(1)<Ο(log2n)<Ο(n)<Ο(nlog2n)<Ο(n2)<Ο(n3)< Ο(nk) <Ο( 2n), as the problem scale n continues to increase, the above-mentioned time complexity continues to increase, and the execution efficiency of the algorithm is lower
Insert picture description here

It can be seen from the figure that we should avoid using exponential order as much as possible

  1. Constant order O(1)

    No matter how many lines of code are executed, as long as there is no complex structure such as loops, the time complexity of this code is O(1)
    Insert picture description here

    When the above code is executed, the time it consumes does not increase with the growth of a certain variable, so no matter how long this type of code is, even if there are tens of thousands of hundreds of thousands of lines, it can be represented by O(1) Time complexity.

  2. Logarithmic order O(log2n)
    Insert picture description here
    description: In the while loop, i is multiplied by 2 each time. After the multiplication, i is getting closer and closer to n. Suppose that after the loop x times, i is greater than 2, and the loop exits at this time, that is to say, the x power of 2 is equal to n, then x = log2n, that is to say, after the loop log2n times, the code ends. Therefore, the time complexity of this code is: O(log2n). This 2 of O(log2n) changes in time according to the code, i = i * 3, it is O(log3n).
    Insert picture description here

  3. Linear order O(n)
    Insert picture description here
    description: This code, the code in the for loop will be executed n times, so the time it consumes varies with the change of n, so this type of code can be represented by O(n) Its time complexity

  4. Linear logarithmic order O(nlogN)
    Insert picture description here
    Description: Linear logarithmic order O(nlogN) is actually very easy to understand. If the code with time complexity of O(logn) is looped N times, then its time complexity is n * O( logN), which is O(nlogN)


  5. Insert picture description here
    Explanation of square order O(n²) : The square order O(n²) is easier to understand. If the O(n) code is nested and looped again, its time complexity is O(n²), this code is actually Nested with 2 levels of n loops, its time complexity is O(n n), which is O(n²). If you change the n of one of the loops to m, then its time complexity becomes O(m n)

  6. Cubic order O(n³), K-th order O(n^k)
    Description: Refer to the above O(n²) to understand, O(n³) is equivalent to three layers of n cycles, and the others are similar

Average time complexity and worst time complexity

The average time complexity refers to the running time of the algorithm when all possible input instances appear with equal probability.
The time complexity in the worst case is called the worst time complexity. The time complexity discussed in general is the worst-case time complexity. The reason for this is: the worst-case time complexity is the limit of the algorithm's running time on any input instance, which ensures that the algorithm's running time will not be longer than the worst-case.
Whether the average time complexity and the worst time complexity are the same is related to the algorithm (as shown in the figure:).
Insert picture description here

Space complexity

basic introduction

  1. Similar to the discussion of time complexity, the space complexity of an algorithm is defined as the storage space consumed by the algorithm, which is also a function of the problem size n.
  2. Space Complexity (Space Complexity) is a measure of the amount of storage space that an algorithm temporarily occupies during its operation. The number of temporary work units that some algorithms need to occupy is related to the scale n of the problem. It increases with the increase of n. When n is larger, it will occupy more storage units, such as quick sort and merge sort algorithms This is the case
  3. When doing algorithm analysis, the main discussion is time complexity. From the perspective of user experience, the speed of program execution is more important. Some caching products (redis, memcache) and algorithms (cardinality sorting) essentially use space for time.

Commonly used

  1. Bubble Sort
  2. Select sort
  3. Insertion sort
  4. Hill sort
  5. Quick sort
  6. Merge sort
  7. Base sort
  8. Heap sort

Comparison of commonly used sorting algorithms

Insert picture description here

Explanation of related terms:

  1. Stable: If a is originally in front of b, and a=b, a is still in front of b after sorting;
  2. Unstable: If a is originally in front of b, and a=b, a may appear behind b after sorting;
  3. Inner sorting: all sorting operations are completed in memory;
  4. Outer sorting: Because the data is too large, the data is placed on the disk, and the sorting can be performed only through the data transmission between the disk and the memory;
  5. Time complexity: The time it takes to execute an algorithm.
  6. Space complexity: the amount of memory required to run a program.
  7. n: data size
  8. k: the number of "buckets"
  9. In-place: Does not take up additional memory
  10. Out-place: Take up extra memory

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/108763069