Introduction to Algorithms 7.4-5

Source: Internet
Author: User
Tags time interval

Title:

When the input data is already "almost in order", the insertion sort is fast, and in practical applications we can use this feature to improve the speed of fast sorting. When a quick sort is called on a sub-array with a length less than k, let it return without any sort. When the quick sort call on the previous layer returns, run the Insert sort on the entire array to complete the sorting process. Proof: The expected time complexity of this sorting algorithm is O (NK+NLG (n/k)).

Solution:

Quicksort begins the method of inserting sorting when recursion to an array of only a few element sizes. The improved fast sorting method is

Expected time = expected time of original Fast + expected time of insert sort method.

The analysis method of 7.4 (chapter 7th of the algorithm introduction) is still used here. The expected comparison count is also calculated for the fast row.

Because the elements that are divided in the same decimal group, K, are not compared in the fast row. So xij as long as the calculation of those I and J difference k-1 more than the comparison of the elements can be.

Define an element collection zij={zi,zi+1,,,,,,,, ZJ}

Define an indicator random variable xij=i{zi compared to ZJ}

E[xij]=pr[xij]=pr{zi and ZJ are compared}=pr{zi is zij the main element}+pr{zj is the zij of the main}=2/(j-i+1)//Because in the fast row, the two can be compared, then one of them must be the main element

The number of expected comparisons for a quick sort E[xij] is
Then the desired time of the NLG is O (n/k), assuming that the optimized fast row produces the fractional group size O (k), in each size O (k) of the decimal group using the insertion sort, the time complexity of O (k^2), a total of O (n/k) decimal groups, the insertion sort time is O (NK),. Then add up these times to O (Nk+nlog (n/k)).
The value of K is then experimented with: An algorithm for optimizing fast sorting using Insert sorting
Experimental Purpose:Fast sorting can be optimized according to the following algorithm, and speed up the fast sorting: that is, when the length of the sub-sequence divided by the fast sorting is less than a certain value K, the sub-sequence is basically orderly, the sequence can be sorted by inserting sort, so that the expectation of time complexity of the whole algorithm drops to O (nk+nlg (n/k)). The purpose of this experiment is to realize the optimization algorithm of fast sequencing, and to explore the reasonable value range of K.Problem DefinitionA total of two questions to solve in the experiment 1, the optimization of the fast sorting algorithm can indeed accelerate the speed of fast sorting. 2. Discuss the reasonable value range of K. For a given int[random array of size n, it is proved by experiments that the optimized fast ordering is higher than the traditional fast-scheduling efficiency, and discusses the range of K values.Experimental Ideas1, the method of optimizing the fast sorting: a) in the fast sorting method to add the judgment statement, when the high-low<k is called the insertion sorting algorithm, otherwise continue to use fast sorting. b) When k<=2, the insertion order is essentially not called, so that the time required to use the quick sort directly at the same size can be obtained. So as to facilitate comparison 2, through the investigation of time consumption to determine the performance of a) each calculation data are taken to a certain size of the INT 32 unordered random number to sort, in order to avoid errors, the size of n must be, different K value, all calculate 5 time of operation, take the average record. b) Use the Stopwatch timer object provided by the. NET Framework 2.0, which allows the time interval to be accurate to 0.0001ms. c) Theoretically obtaining a reasonable value range of K value is k<=logn. Therefore, when validating the range of values, take the values on both sides of log n. D) By comparing the time complexity to calculate the performance increase, so as to get faster sequencing of the conclusion, and reasonable K value range.test data and resultsThe hardware and software environment of this test is as follows CPU:PM 1.5G; Memory: 768M; operating system: Windows XP SP2; software platform:. NET 2.0; 1. When the array size is 50, the following data is obtained.   The value of the first behavior K, and the time required for the second Action program to run, in Ms. 2. When the array size is 500, the following data is obtained. The value of the first behavior K, and the time required for the second Action program to run, in Ms. Figure 3, the size of the array is 5000 when the following data is obtained. The value of the first behavior K, the second action program to run the time required, the unit is Ms 4, the array size is 50000 when the following data is obtained. The value of the first behavior K, the second action program to run the time required, the unit is Ms 5, the array size is 1,000,000 when the following data is obtained. The value of the first behavior K, and the time required for the second Action program to run, in MSResults Analysis:1, by all the figures can be seen, after the optimization of the rapid sorting faster than the traditional fast sorting speed, through the data calculation can be sorted by 10%-20%. 2, through the diagram can also know that when the K=LOGN, its efficiency is not the largest, in a small size (for example, 5 orders of magnitude below) the best value of k is often greater than log n, generally 8-12 is the smallest. When the scale is large, it is generally advisable to take logn, even if the best value exceeds Logn, its performance is not obviously improved. 3, after the increase of the value of K, especially after more than 50, the time complexity is significantly improved, even faster than the algorithm of the order to be poor. 4, in the large-scale sorting process, the efficiency of quick sorting is better than the insertion sort. Factors that may affect the conclusion: 1, a fast algorithm for random numbers: a) In this experiment, the random number used in the 10n integer to take n non-repeating random number, the algorithm may affect the analysis of experimental results have a slight impact. 2, small-scale array of analysis a) for small-scale arrays may be obtained after the rapid ordering of sub-sequence length is less than 8, so direct fetch 8 is debatable.Experimental Conclusion1, after the optimization of the rapid sorting faster than the traditional fast sorting speed, through the data calculation can be sorted by 10%-20%. 2, in a small scale (for example, 5 orders of magnitude below) the best value of k is usually greater than log n, generally with the best efficiency between 8-12. When the scale is large, the value of K is generally taken logn advisable, even if the best value exceeds Logn, its performance is not obvious.Source CodeOptimized sorting algorithm source code (C # Description) class Sortop
{//Insert Sort
private static void Insertsort (int[] list,int low,int High)
{
for (int i = low, i < high; i++)
{
int t = list[i];
int j = i;
while ((J > 0) && (list[j-1] > t))
{
LIST[J] = list[j-1];
--j;
}
LIST[J] = t;
}
}



private static void Swap (ref int i, ref int J)
Direct exchange with only two elements
{
int t;
t = i;
i = j;
j = t;
}
Quick Sort Optimization
public static void QuickSort (int[] list, int. low, int high,int k)
{
There's only one left, no need to exchange

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.