Optimization of Bubble Sort and Quick Sort Process and Optimization

Enter image description

Optimization of bubble sort:

1. Join Sentinel. 2. Remember the last position of each exchange, this position is in order and does not need to be changed.

1. The basic idea of ​​quick sort:

Quick sort uses the idea of ​​divide and conquer, and divides the sequence to be sorted into two parts by one pass sorting, and the keywords of one part of the records are smaller than the keywords of the other part of the records. Afterwards, the two parts of records are continued to be sorted, so as to achieve the purpose of ordering the entire sequence.

2. The three steps of quick sort:

(1) Select the benchmark: in the to-be-sorted column, pick out an element in a certain way as a "pivot"

(2) Divide operation: Divide the sequence into two subsequences based on the actual position of the reference in the sequence. At this point, the elements to the left of the datum are smaller than the datum, and the elements to the right of the datum are larger than the datum

(3) Quicksort the two sequences recursively until the sequence is empty or has only one element.

3. How to choose a benchmark

For the divide-and-conquer algorithm, if the algorithm can be divided into two subsequences of equal length each time it is divided, then the efficiency of the divide-and-conquer algorithm will reach the maximum. That said, the choice of benchmark is important. The way of choosing the benchmark determines the length of the two subsequences after the two splits, which in turn has a decisive impact on the efficiency of the entire algorithm.

Ideally, choose a benchmark that divides the sequence to be sorted into two equal-length subsequences

We introduce three methods for selecting benchmarks: 1. Fixed location. 2. Randomly select benchmarks. 3. Take the middle of the three numbers.Enter image description

Optimization of quicksort:

1. The first method is to use insertion sort when the length of the sequence to be sorted is divided to a certain size. The reason is because for very small and partially ordered arrays, quicksort is not as good as swap. When the length of the sequence to be sorted is divided to a certain size, the efficiency of continuing to divide is worse than that of insertion sort, so we can use insertion sort instead of quick sort at this time.

2. The second optimization method is to group the elements equal to the pivot key together after one division, and do not divide the elements equal to the key in the next division. During processing, there are two steps:

The first step is to put elements equal to the key into both ends of the array during the division process. . . . . . The second step is to move the elements equal to the key around the pivot (the base number) after the division is over.

3. There is also the selection of the number of three numbers, that is, the selection of the number of the central axis, because the best division is to divide the sequence to be sorted into subsequences of equal length. In the best state, we can use the value in the middle of the sequence, that is The N/2th number. However, this is hard to figure out, and can significantly slow down quicksort. This median estimate can be obtained by randomly picking three elements and using their median as the pivot element. But in fact, randomness doesn't help much, so the general practice is to use the median of the three elements on the left, right, and center positions as the pivot element. (Using the three-number median split eliminates the bad case of presorted input) Reduces the number of comparisons in quicksort by about 14%

4. Optimize recursive operation The quicksort function has two recursive operations at the end of the function, and we can use tail recursion optimization for it. . . . . Advantages: If the division of the sequence to be sorted is extremely unbalanced, the depth of recursion will approach n, and the size of the stack will be very limited. Each recursive call will consume a certain amount of stack space. The more space is consumed. After optimization, the stack depth can be reduced from the original O(n) to logn. That is, as follows low = provit+1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325065967&siteId=291194637