Data structure (9)-sorting algorithm

Preface

  The last article introduced you to a more commonly used idea in data structures- recursion . It mainly introduces the related concepts of recursion and further illustrates the usage of recursion through some cases. This article introduces the sorting algorithm in the data structure. In fact, at the beginning of the data structure, we introduced the top ten algorithms commonly used in the data structure . At that time, we all implemented it with C language, java and python, and through various data structure visualization tools . This article mainly introduces the classification of sorting and the introduction of the time complexity of the algorithm and the realization of the major algorithms. First of all, make a corresponding introduction to the related content of the sorting algorithm.

1. Introduction to sorting algorithm

  Sorting is also called Sort Algorithm. Sorting is a process of sorting a set of data in a specified order. The sorting of sorting is mainly divided into internal sorting and external sorting .

  • 1, the internal sorting
      means all the data to be processed are loaded into an internal memory (RAM) for ordering.
  • 2, external sorting
      data is too large to load into memory of all, it needs an external storage (files) to be sorted.
      The classification of common sorting algorithms is shown in the figure below:

Second, the time complexity of the algorithm

  In fact, we have an article in the previous article that introduced the time complexity of the algorithm . There are two ways to measure the time complexity of an algorithm, one is the post-statistic method , and the other is the pre-estimation method . Both of these methods are feasible, but the former has some problems. The main one is that to evaluate the performance of the designed algorithm, it is necessary to actually run the program. In addition, the statistics of the time obtained depend on the computer's hardware, software and other environmental factors. In this way, it is necessary to run in the same state to compare which algorithm is faster. The pre-estimation method is to judge which algorithm is better by analyzing the time complexity of an algorithm.
  Time frequency means that the time spent by an algorithm is proportional to the number of sentence executions in the algorithm. Specifically: the more the number of sentence executions in an algorithm, the more time it takes. The number of statement executions in an algorithm is called statement frequency time frequency . Denoted as T(n). For specific related cases, please see this article , which is introduced in more detail. Common time complexity is as follows:

  1. Constant order O(1)
  2. Logarithmic order O(log2n)
  3. Linear order O(n)
  4. Linear logarithmic order O(nlog2n)
  5. Square order O(n^2)
  6, Cubic order O (n^3)
  7, k-order O(n^k)
  8, exponential order O(2^n)

  The figure corresponding to the commonly used time complexity is shown in the figure: From the

  above figure, we can see that the time complexity of common algorithms is from small to large: O(1) < O(log2n) < O(n) < O(nlog2n) < O(n^2) < O(n^3) < O(n^k) < O(2^n)
  In addition, as can be seen from the figure, we should try to avoid using exponential order algorithms.

  • 1. Constant order O(1)
      No matter how many lines of code are executed, as long as there is no loop structure , the time complexity of this code is O(1).
int i = 1;
int j = 2;
++i;
j++;
int m = i + j;

  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 it is tens of thousands or even hundreds of thousands of lines, you can use O(1 ) To express its time complexity.

  • 2. Logarithmic order O(log2n)
int i = 1;
while(i < n)
{
    
    
	i = i * 2;
}

  In the while loop, i*2 is executed every time. After the multiplication, i is getting closer and closer to n. Suppose that after the loop x times, i is greater than 2. At this time, the loop exits, that is to say, the x square of 2 is equal to n, then x=log2n, that is, when the loop executes log2n, the execution of this code ends , Therefore, this time complexity is: O(log2n). The main thing we need here is: the 2 in O(log2n) changes with the code changes in the code segment. Suppose we change it i = i * 2to i = i * 3, then: its time complexity is O(log3n).

  • 3. Linear order O(n)
for(int i = 1; i <= n; i++){
    
    
	j = i;
	j++;
}

  This code, the code in the for loop will be executed n times, so the time it consumes varies with the change of n, therefore, this type of code can use O(n) to represent its time complexity

  • 4. Linear logarithmic order O(nlogn)
for(m = 1; m < n; m++){
    
    
	i = 1;
	while(i < n){
    
    
		i = i * 2;
	}
}

  Linear logarithmic order O(nlogn) is actually very easy to understand. Loop the code with time complexity of O(logn) n times, then its time complexity is n*O(logn), which is O(nlogn).

  • 5. Square order O(n^2)
for(x = 1; i <= n; x++){
    
    
	for(i = 1; i <= n; i++){
    
    
		j = 1;
		j++;
	}
}

  The square order O(n^2) is easier to understand. If the O(n) code is repeated in a nested loop, its time complexity is O(n2). If the n is changed to m, then its time complexity is O(m*n).

  Next, we will introduce the average time complexity and the 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. It should be noted here that the time complexity we discussed is the worst-case time complexity. The reason why we discuss this is that the time complexity in the worst case is the limit of the running time of the algorithm on any input instance, which ensures that the running time of the algorithm 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. The time complexity of various types of algorithms is shown in the following figure:

  Space complexity is 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. 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 the merge sort and the above figure. This is the case with cardinality sorting . When we generally do algorithm analysis, we mainly discuss time complexity . From the perspective of user experience, the speed of program execution is more important. Some cached database (redis) technologies and algorithms (cardinal sorting) often use space for time . As for the principle of the top ten algorithms and the corresponding code implementation, we have introduced it before in the previous article. Therefore, in view of the length and time, I will not introduce it here. Interested readers can read this article . In that article, the principles of various sorting algorithms were introduced, and the animations demonstrated the sorting process and realization ideas of the major algorithms. Finally, they were realized through C language, java and python. It is strongly recommended that readers understand the corresponding code and its ideas in detail.

3. Explanation of related terms

  When we introduced earlier, it involved the stability of the algorithm sorting algorithm and other professional terms. Next, we will give a detailed explanation:

  • 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: Due to the large amount of data, 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 consumed by an algorithm to execute
  • 6. Space complexity: the amount of memory required to run a program
  • 7. n: The size of the data
  • 8. k: the number of barrels
  • 9. In-place: does not occupy additional memory
  • 10. Out-place: occupy additional memory

to sum up

  The last article introduced you to a more commonly used idea in data structures- recursion . It mainly introduces the related concepts of recursion and further illustrates the usage of recursion through some cases. This article introduces the sorting algorithm in the data structure. This article mainly introduces the classification of sorting and the introduction of the time complexity of the algorithm and the realization of the major algorithms. In fact, data structures and algorithms are particularly important and play a vital role in programming. Therefore, we need special mastery. Life is endless and struggle is endless. We work hard every day, study hard, constantly improve our abilities, and believe that we will learn something. Come on! ! !

Guess you like

Origin blog.csdn.net/Oliverfly1/article/details/112744185