Algorithm-compare two sort algorithms: Select sort and insert sort, Sort Algorithm

Source: Internet
Author: User

Algorithm-compare two sort algorithms: Select sort and insert sort, Sort Algorithm

Now we have implemented two sorting algorithms. We naturally want to know which sort is faster to choose and insert sort. This is the first time that we use practice to illustrate our solutions to this problem.

Nature:For random sorting arrays Without Duplicate primary keys, the insert sorting and select sorting run time are at the Square level, and the ratio of the two should be a small constant.

Example:This conclusion has been proven on many different types of computers in the last half century. When the first version of the book was completed in 1980, insertion sorting was twice faster than selection sorting, and now it is still the case, even though these algorithms sort 0.1 million pieces of data for several hours and now it takes only a few seconds. Is insert sorting faster on your computer than select sorting? It can be detected through the SortCompare class. It uses the sort () method corresponding to the sorting algorithm name specified by the command line parameter to perform the specified number of experiments (sort the array of the specified size ), and print the ratio of the observed running time of various algorithms.

We use Stopwatch for timing.

The Input model of a random array is implemented by the timeRandom-Input () method in the SortCompare class. This method generates random Double values, sorts them, and returns the total time of the specified test.

The advantage of specifying the number of repeated times with the command line parameter is that it can run a large number of tests (the more times the test is, the closer the average time required for each test is to the real average data) and can reduce the impact of the system itself.

/*** Compare two sorting algorithms * @ author huazhou **/public class SortCompare {public static double time (String alg, Comparable []) {Selection selection = new Selection (); Insertion insertion = new Insertion (); Stopwatch timer = new Stopwatch (); if (alg. equals ("Selection") {selection. sort (a);} if (alg. equals ("Insertion") {insertion. sort (a);} return timer. elapsedTime ();} public static double timeRandomInput (String alg, int N, int T) {// sort arrays with T lengths of N by double total = 0.0 using algorithm 1; double [] a = new Double [N]; for (int t = 0; t <T; t ++) {// perform a test (generate an array and sort it) for (int I = 0; I <N; I ++) {a [I] = StdRandom. uniform () ;}total + = time (alg, a) ;}return total;} public static void main (String [] args) {String alg1 = args [0]; string alg2 = args [1]; int N = Integer. parseInt (args [2]); int T = Integer. parseInt (args [3]); double t1 = timeRandomInput (alg1, N, T); // The total time of algorithm 1 double t2 = timeRandomInput (alg2, N, T ); // The total time of algorithm 2 StdOut. printf ("For % d random Doubles \ n % s is", N, alg1); StdOut. printf ("%. 1f times faster than % s \ n ", t2/t1, alg2 );}}

This regular operation runs the sorting algorithm specified by the first two command line parameters to sort the Double-type random array with a length of N (specified by the third parameter, the element values are between 0.0 and 1.0, repeat T times (specified by the fourth parameter), and then output the ratio of the total running time.

Source code download]

Related Article

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.